Author: remm
Date: Fri Dec 21 13:23:26 2018
New Revision: 1849473

URL: http://svn.apache.org/viewvc?rev=1849473&view=rev
Log:
63022: Do not use the socket open state when using the wrapper isClosed method 
for NIO and NIO2, as it will disable all further processing. Fix socket close 
discrepancies for NIO2, now the wrapper close is used everywhere except for 
socket accept problems.

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
    tomcat/trunk/webapps/docs/changelog.xml

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java?rev=1849473&r1=1849472&r2=1849473&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java Fri Dec 
21 13:23:26 2018
@@ -1302,6 +1302,12 @@ public abstract class AbstractEndpoint<S
 
     protected abstract boolean setSocketOptions(U socket);
 
+    /**
+     * Close the socket when the connection has to be immediately closed when
+     * an error occurs while configuring the accepted socket, allocating
+     * a wrapper for the socket, or trying to dispatch it for processing.
+     * @param socket The newly accepted socket
+     */
     protected abstract void closeSocket(U socket);
 
     protected void destroySocket(U socket) {

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java?rev=1849473&r1=1849472&r2=1849473&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java Fri Dec 21 
13:23:26 2018
@@ -226,7 +226,7 @@ public class Nio2Endpoint extends Abstra
                     // Then close all active connections if any remain
                     try {
                         for (Nio2Channel channel : 
getHandler().getOpenSockets()) {
-                            closeSocket(channel.getSocket());
+                            channel.getSocket().close();
                         }
                     } catch (Throwable t) {
                         ExceptionUtils.handleThrowable(t);
@@ -378,47 +378,6 @@ public class Nio2Endpoint extends Abstra
     }
 
 
-    private void closeSocket(SocketWrapperBase<Nio2Channel> socket) {
-        if (log.isDebugEnabled()) {
-            log.debug("Calling [" + this + "].closeSocket([" + socket + "],[" 
+ socket.getSocket() + "])",
-                    new Exception());
-        }
-        if (socket == null) {
-            return;
-        }
-        try {
-            getHandler().release(socket);
-        } catch (Throwable e) {
-            ExceptionUtils.handleThrowable(e);
-            if (log.isDebugEnabled()) log.error("",e);
-        }
-        Nio2SocketWrapper nio2Socket = (Nio2SocketWrapper) socket;
-        try {
-            synchronized (socket.getSocket()) {
-                if (!nio2Socket.closed) {
-                    nio2Socket.closed = true;
-                    countDownConnection();
-                }
-                if (socket.getSocket().isOpen()) {
-                    socket.getSocket().close(true);
-                }
-            }
-        } catch (Throwable e) {
-            ExceptionUtils.handleThrowable(e);
-            if (log.isDebugEnabled()) log.error("",e);
-        }
-        try {
-            if (nio2Socket.getSendfileData() != null
-                    && nio2Socket.getSendfileData().fchannel != null
-                    && nio2Socket.getSendfileData().fchannel.isOpen()) {
-                nio2Socket.getSendfileData().fchannel.close();
-            }
-        } catch (Throwable e) {
-            ExceptionUtils.handleThrowable(e);
-            if (log.isDebugEnabled()) log.error("",e);
-        }
-    }
-
     protected class Nio2Acceptor extends Acceptor<AsynchronousSocketChannel>
         implements CompletionHandler<AsynchronousSocketChannel, Void> {
 
@@ -928,14 +887,50 @@ public class Nio2Endpoint extends Abstra
 
 
         @Override
-        public void close() throws IOException {
-            getSocket().close();
+        public void close() {
+            if (log.isDebugEnabled()) {
+                log.debug("Calling [" + getEndpoint() + "].closeSocket([" + 
this + "])", new Exception());
+            }
+            try {
+                getEndpoint().getHandler().release(this);
+            } catch (Throwable e) {
+                ExceptionUtils.handleThrowable(e);
+                if (log.isDebugEnabled()) {
+                    log.error("Channel close error", e);
+                }
+            }
+            try {
+                synchronized (getSocket()) {
+                    if (!closed) {
+                        closed = true;
+                        getEndpoint().countDownConnection();
+                    }
+                    if (getSocket().isOpen()) {
+                        getSocket().close(true);
+                    }
+                }
+            } catch (Throwable e) {
+                ExceptionUtils.handleThrowable(e);
+                if (log.isDebugEnabled()) {
+                    log.error("Channel close error", e);
+                }
+            }
+            try {
+                SendfileData data = getSendfileData();
+                if (data != null && data.fchannel != null && 
data.fchannel.isOpen()) {
+                    data.fchannel.close();
+                }
+            } catch (Throwable e) {
+                ExceptionUtils.handleThrowable(e);
+                if (log.isDebugEnabled()) {
+                    log.error("Channel close error", e);
+                }
+            }
         }
 
-
         @Override
         public boolean isClosed() {
-            return closed || !getSocket().isOpen();
+            return closed;
         }
 
 
@@ -1775,7 +1770,7 @@ public class Nio2Endpoint extends Abstra
                     }
                     if (state == SocketState.CLOSED) {
                         // Close socket and pool
-                        closeSocket(socketWrapper);
+                        socketWrapper.close();
                         if (running && !paused) {
                             if (!nioChannels.push(socketWrapper.getSocket())) {
                                 socketWrapper.getSocket().free();
@@ -1785,7 +1780,7 @@ public class Nio2Endpoint extends Abstra
                         launch = true;
                     }
                 } else if (handshake == -1 ) {
-                    closeSocket(socketWrapper);
+                    socketWrapper.close();
                     if (running && !paused) {
                         if (!nioChannels.push(socketWrapper.getSocket())) {
                             socketWrapper.getSocket().free();
@@ -1797,7 +1792,7 @@ public class Nio2Endpoint extends Abstra
             } catch (Throwable t) {
                 log.error(sm.getString("endpoint.processing.fail"), t);
                 if (socketWrapper != null) {
-                    closeSocket(socketWrapper);
+                    ((Nio2SocketWrapper) socketWrapper).close();
                 }
             } finally {
                 if (launch) {

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=1849473&r1=1849472&r2=1849473&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Fri Dec 21 
13:23:26 2018
@@ -1168,7 +1168,7 @@ public class NioEndpoint extends Abstrac
 
         @Override
         public boolean isClosed() {
-            return closed || !getSocket().isOpen();
+            return closed;
         }
 
 

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1849473&r1=1849472&r2=1849473&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Fri Dec 21 13:23:26 2018
@@ -86,6 +86,15 @@
         header in HTTP/2 responses where an appropriate value is available.
         (markt)
       </add>
+      <fix>
+        <bug>63022</bug>: Do not use the socket open state when using the
+        wrapper isClosed method for NIO and NIO2, as it will disable all
+        further processing. (remm)
+      </fix>
+      <fix>
+        Fix socket close discrepancies for NIO2, now the wrapper close
+        is used everywhere except for socket accept problems. (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Tribes">



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to