When a read or write times out, make sure the key is deregistered with the poller
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 23 Feb 2007 12:58:49 +0000 (12:58 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 23 Feb 2007 12:58:49 +0000 (12:58 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@510933 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/tomcat/util/net/NioBlockingSelector.java

index 16ee507..a60b0a8 100644 (file)
@@ -41,7 +41,7 @@ public class NioBlockingSelector {
      * @throws IOException if an IO Exception occurs in the underlying socket logic\r
      */\r
     public static int write(ByteBuffer buf, NioChannel socket, long writeTimeout) throws IOException {\r
-        final SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());\r
+        SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());\r
         int written = 0;\r
         boolean timedout = false;\r
         int keycount = 1; //assume we can write\r
@@ -86,18 +86,22 @@ public class NioBlockingSelector {
             if (timedout) \r
                 throw new SocketTimeoutException();\r
         } finally {\r
-//            if (key != null) {\r
-//                socket.getPoller().addEvent(\r
-//                    new Runnable() {\r
-//                    public void run() {\r
-//                        key.cancel();\r
-//                    }\r
-//                });\r
-//            }\r
+            if (timedout && key != null) {\r
+                cancelKey(socket, key);\r
+            }\r
         }\r
         return written;\r
     }\r
 \r
+    private static void cancelKey(final NioChannel socket, final SelectionKey key) {\r
+        socket.getPoller().addEvent(\r
+            new Runnable() {\r
+            public void run() {\r
+                key.cancel();\r
+            }\r
+        });\r
+    }\r
+\r
     /**\r
      * Performs a blocking read using the bytebuffer for data to be read\r
      * If the <code>selector</code> parameter is null, then it will perform a busy read that could\r
@@ -149,14 +153,9 @@ public class NioBlockingSelector {
             if (timedout)\r
                 throw new SocketTimeoutException();\r
         } finally {\r
-//            if (key != null) {\r
-//                socket.getPoller().addEvent(\r
-//                    new Runnable() {\r
-//                    public void run() {\r
-//                        key.cancel();\r
-//                    }\r
-//                });\r
-//            }\r
+            if (timedout && key != null) {\r
+                cancelKey(socket,key);\r
+            }\r
         }\r
         return read;\r
     }\r