[ 
https://issues.apache.org/jira/browse/LOG4J2-1922?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16028747#comment-16028747
 ] 

Kohei Tamura commented on LOG4J2-1922:
--------------------------------------

Steps to reproduce:

(1) Create a client:
{code:java}
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TcpSocketClient {
    public static void main(String[] args) {
        Logger logger = LogManager.getLogger("test.logger");
        logger.info("InfoMessage");
    }
}
{code}

(2) Create log4j2.xml for the client:
{code:xml}
<Configuration status="info" name="example" packages="">
    <Appenders>
        <Socket name="socket" host="localhost" port="1111">
        <SerializedLayout />
        </Socket>
    </Appenders>
    <Loggers>
        <Root level="INFO">
        </Root>
        <Logger name="test.logger" level="info">
            <AppenderRef ref="socket"/>
        </Logger>
    </Loggers>
</Configuration>
{code}

(3) Create log4j2.xml for TcpSocketServer:
{code:xml}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] 
%c{1} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info" additivity="false">
            <AppenderRef ref="console"/>
        </Root>
    </Loggers>
</Configuration>
{code}

(4) Run TcpSocketServer 
{noformat}
java -cp "log4j-core-2.3.jar;log4j-api-2.3.jar" 
org.apache.logging.log4j.core.net.server.TcpSocketServer 1111 log4j2.xml
{noformat}

(5) Run the client again and again:
{noformat}
java TcpSocketClient 
{noformat}

(6) Run netstat command
{noformat}
netstat -aon | find "1111"
  TCP    0.0.0.0:1111           0.0.0.0:0              LISTENING       8748
  TCP    127.0.0.1:1111         127.0.0.1:62928        CLOSE_WAIT      8748
  TCP    127.0.0.1:1111         127.0.0.1:62929        CLOSE_WAIT      8748
  TCP    127.0.0.1:1111         127.0.0.1:62931        CLOSE_WAIT      8748
  TCP    127.0.0.1:1111         127.0.0.1:62932        CLOSE_WAIT      8748
  TCP    127.0.0.1:62928        127.0.0.1:1111         FIN_WAIT_2      7572
  TCP    127.0.0.1:62929        127.0.0.1:1111         FIN_WAIT_2      8652
  TCP    127.0.0.1:62931        127.0.0.1:1111         FIN_WAIT_2      8320
  TCP    127.0.0.1:62932        127.0.0.1:1111         FIN_WAIT_2      9136
  TCP    [::]:1111              [::]:0                 LISTENING       8748
{noformat}

> SocketHandler leaks connection if EOFException is thrown
> --------------------------------------------------------
>
>                 Key: LOG4J2-1922
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1922
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 2.7
>            Reporter: Kohei Tamura
>
> SocketHandler should close the input stream in the finally block.
> {code:title=TcpSocketServer.java|borderStyle=solid}
>     /**
>      * Thread that processes the events.
>      */
>     private class SocketHandler extends Thread {
>         private final T inputStream;
>         private volatile boolean shutdown = false;
>         public SocketHandler(final Socket socket) throws IOException {
>             this.inputStream = 
> logEventInput.wrapStream(socket.getInputStream());
>         }
>         @Override
>         public void run() {
>             boolean closed = false;
>             try {
>                 try {
>                     while (!shutdown) {
>                         logEventInput.logEvents(inputStream, 
> TcpSocketServer.this);
>                     }
>                 } catch (final EOFException e) {
>                     closed = true;
>                 } catch (final OptionalDataException e) {
>                     logger.error("OptionalDataException eof=" + e.eof + " 
> length=" + e.length, e);
>                 } catch (final IOException e) {
>                     logger.error("IOException encountered while reading from 
> socket", e);
>                 }
>                 if (!closed) {
>                     try {
>                         inputStream.close();
>                     } catch (final Exception ex) {
>                         // Ignore the exception;
>                     }
>                 }
>             } finally {
>                 handlers.remove(Long.valueOf(getId()));
>             }
>         }
>         public void shutdown() {
>             this.shutdown = true;
>             interrupt();
>         }
>     }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to