THRIFT-145. java: Make TNonblockingServer more robust against errors

TNonblockingServer was throwing a null pointer exception in certain cases.
We're not really sure what the root cause is, but this change prevents
the exception and also improves logging of certain undesirable conditions.


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@702661 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/com/facebook/thrift/server/TNonblockingServer.java b/lib/java/src/com/facebook/thrift/server/TNonblockingServer.java
index 1e1284f..47fdd5f 100644
--- a/lib/java/src/com/facebook/thrift/server/TNonblockingServer.java
+++ b/lib/java/src/com/facebook/thrift/server/TNonblockingServer.java
@@ -318,6 +318,8 @@
           } else if (key.isWritable()) {
             // deal with writes
             handleWrite(key);
+          } else {
+            LOGGER.log(Level.WARNING, "Unexpected state in select! " + key.interestOps());
           }
         }
       } catch (IOException e) {
@@ -343,9 +345,10 @@
      */
     private void handleAccept() throws IOException {
       SelectionKey clientKey = null;
+      TNonblockingTransport client = null;
       try {
         // accept the connection
-        TNonblockingTransport client = (TNonblockingTransport)serverTransport.accept();
+        client = (TNonblockingTransport)serverTransport.accept();
         clientKey = client.registerSelector(selector, SelectionKey.OP_READ);
 
         // add this key to the map
@@ -353,9 +356,10 @@
         clientKey.attach(frameBuffer);
       } catch (TTransportException tte) {
         // something went wrong accepting.
-        cleanupSelectionkey(clientKey);
         LOGGER.log(Level.WARNING, "Exception trying to accept!", tte);
         tte.printStackTrace();
+        if (clientKey != null) cleanupSelectionkey(clientKey);
+        if (client != null) client.close();
       }
     }