Author: markt
Date: Wed Dec 17 18:19:00 2014
New Revision: 1646304

URL: http://svn.apache.org/r1646304
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=57340
Ensure that Socket and SocketWrapper are only returned to the cache once on 
socket close (it is possible for multiple threads to call close concurrently).

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java

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=1646304&r1=1646303&r2=1646304&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Wed Dec 17 
18:19:00 2014
@@ -918,10 +918,11 @@ public class NioEndpoint extends Abstrac
             addEvent(r);
         }
 
-        public void cancelledKey(SelectionKey key) {
+        public NioSocketWrapper cancelledKey(SelectionKey key) {
+            NioSocketWrapper ka = null;
             try {
-                if ( key == null ) return;//nothing to do
-                NioSocketWrapper ka = (NioSocketWrapper) key.attach(null);
+                if ( key == null ) return null;//nothing to do
+                ka = (NioSocketWrapper) key.attach(null);
                 if (ka!=null) handler.release(ka);
                 else handler.release((SocketChannel)key.channel());
                 if (key.isValid()) key.cancel();
@@ -961,6 +962,7 @@ public class NioEndpoint extends Abstrac
                 ExceptionUtils.handleThrowable(e);
                 if (log.isDebugEnabled()) log.error("",e);
             }
+            return ka;
         }
         /**
          * The background thread that listens for incoming TCP/IP connections 
and
@@ -1741,13 +1743,20 @@ public class NioEndpoint extends Abstrac
                     if (state == SocketState.CLOSED) {
                         // Close socket and pool
                         try {
-                            socket.getPoller().cancelledKey(key);
-                            if (running && !paused) {
-                                nioChannels.push(socket);
-                            }
-                            socket = null;
-                            if (running && !paused) {
-                                keyCache.push(ka);
+                            if (socket.getPoller().cancelledKey(key) != null) {
+                                // SocketWrapper (attachment) was removed from 
the
+                                // key - recycle both. This can only happen 
once
+                                // per attempted closure so it is used to 
determine
+                                // whether or not to return socket and ka to
+                                // their respective caches. We do NOT want to 
do
+                                // this more than once - see BZ 57340.
+                                if (running && !paused) {
+                                    nioChannels.push(socket);
+                                }
+                                socket = null;
+                                if (running && !paused) {
+                                    keyCache.push(ka);
+                                }
                             }
                             ka = null;
                         } catch (Exception x) {



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

Reply via email to