better error handling
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 23 Mar 2007 16:19:47 +0000 (16:19 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 23 Mar 2007 16:19:47 +0000 (16:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@521790 13f79535-47bb-0310-9956-ffa450edef68

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

index a60b0a8..b73b69c 100644 (file)
@@ -62,7 +62,7 @@ public class NioBlockingSelector {
                         continue; //we successfully wrote, try again without a selector\r
                     }\r
                 }\r
-                \r
+                if ( key == null ) throw new IOException("Key no longer registered");\r
                 KeyAttachment att = (KeyAttachment) key.attachment();\r
                 try {\r
                     if ( att.getLatch()==null || att.getLatch().getCount()==0) att.startLatch(1);\r
index 4171938..e43ad6d 100644 (file)
@@ -1311,6 +1311,7 @@ public class NioEndpoint {
         }
         public void cancelledKey(SelectionKey key, SocketStatus status, boolean dispatch) {
             try {
+                if ( key == null ) return;//nothing to do
                 KeyAttachment ka = (KeyAttachment) key.attachment();
                 if (ka != null && ka.getComet()) {
                     //the comet event takes care of clean up
@@ -1954,54 +1955,60 @@ public class NioEndpoint {
         }
          
         public void run() {
-
-           
-            SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
-            int handshake = -1;
+            SelectionKey key = null;
             try {
-                handshake = socket.handshake(key.isReadable(), key.isWritable());
-            }catch ( IOException x ) {
-                handshake = -1;
-                if ( log.isDebugEnabled() ) log.debug("Error during SSL handshake",x);
-            }catch ( CancelledKeyException ckx ) {
-                handshake = -1;
-            }
-            if ( handshake == 0 ) {
-                // Process the request from this socket
-                boolean closed = (status==null)?(handler.process(socket)==Handler.SocketState.CLOSED) :
-                    (handler.event(socket,status)==Handler.SocketState.CLOSED);
+                key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
+                int handshake = -1;
+                try {
+                    handshake = socket.handshake(key.isReadable(), key.isWritable());
+                }catch ( IOException x ) {
+                    handshake = -1;
+                    if ( log.isDebugEnabled() ) log.debug("Error during SSL handshake",x);
+                }catch ( CancelledKeyException ckx ) {
+                    handshake = -1;
+                }
+                if ( handshake == 0 ) {
+                    // Process the request from this socket
+                    boolean closed = (status==null)?(handler.process(socket)==Handler.SocketState.CLOSED) :
+                        (handler.event(socket,status)==Handler.SocketState.CLOSED);
 
-                if (closed) {
-                    // Close socket and pool
-                    try {
-                        KeyAttachment att = (KeyAttachment)socket.getAttachment(true);
-                        try {socket.close();}catch (Exception ignore){}
-                        if ( socket.isOpen() ) socket.close(true);
-                        key.cancel();
-                        key.attach(null);
-                        nioChannels.offer(socket);
-                        if ( att!=null ) keyCache.offer(att);
-                    }catch ( Exception x ) {
-                        log.error("",x);
-                    }
-                } 
-            } else if (handshake == -1 ) {
-                KeyAttachment ka = (KeyAttachment)key.attachment();
-                socket.getPoller().cancelledKey(key,SocketStatus.DISCONNECT,false);
-                try {socket.close(true);}catch (IOException ignore){}
-                nioChannels.offer(socket);
-                if ( ka!=null ) keyCache.offer(ka);
-            } else {
-                final SelectionKey fk = key;
-                final int intops = handshake;
-                final KeyAttachment ka = (KeyAttachment)fk.attachment();
-                ka.getPoller().add(socket,intops);
+                    if (closed) {
+                        // Close socket and pool
+                        try {
+                            KeyAttachment att = (KeyAttachment)socket.getAttachment(true);
+                            try {socket.close();}catch (Exception ignore){}
+                            if ( socket.isOpen() ) socket.close(true);
+                            key.cancel();
+                            key.attach(null);
+                            nioChannels.offer(socket);
+                            if ( att!=null ) keyCache.offer(att);
+                        }catch ( Exception x ) {
+                            log.error("",x);
+                        }
+                    } 
+                } else if (handshake == -1 ) {
+                    KeyAttachment ka = (KeyAttachment)key.attachment();
+                    socket.getPoller().cancelledKey(key,SocketStatus.DISCONNECT,false);
+                    try {socket.close(true);}catch (IOException ignore){}
+                    nioChannels.offer(socket);
+                    if ( ka!=null ) keyCache.offer(ka);
+                } else {
+                    final SelectionKey fk = key;
+                    final int intops = handshake;
+                    final KeyAttachment ka = (KeyAttachment)fk.attachment();
+                    ka.getPoller().add(socket,intops);
+                }
+            }catch(CancelledKeyException cx) {
+                socket.getPoller().cancelledKey(key,SocketStatus.ERROR,false);
+            }catch ( Throwable t ) {
+                log.error("",t);
+                socket.getPoller().cancelledKey(key,SocketStatus.ERROR,false);
+            } finally {
+                socket = null;
+                status = null;
+                //return to cache
+                processorCache.offer(this);
             }
-
-            socket = null;
-            status = null;
-            //return to cache
-            processorCache.offer(this);
         }
 
     }