Browse Source

ZOOKEEPER-4296: Add null checks to ClientCnxnSocketNetty onClosing (#1697)

Co-authored-by: Colvin Cowie <COLVINCO@uk.ibm.com>
Colvin Cowie 2 years ago
parent
commit
de8768807f

+ 6 - 2
zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocketNetty.java

@@ -244,7 +244,9 @@ public class ClientCnxnSocketNetty extends ClientCnxnSocket {
 
     @Override
     void onClosing() {
-        firstConnect.countDown();
+        if (firstConnect != null) {
+            firstConnect.countDown();
+        }
         wakeupCnxn();
         LOG.info("channel is told closing");
     }
@@ -253,7 +255,9 @@ public class ClientCnxnSocketNetty extends ClientCnxnSocket {
         if (needSasl.get()) {
             waitSasl.release();
         }
-        outgoingQueue.add(WakeupPacket.getInstance());
+        if (outgoingQueue != null) {
+          outgoingQueue.add(WakeupPacket.getInstance());
+        }
     }
 
     @Override

+ 10 - 0
zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketTest.java

@@ -90,4 +90,14 @@ public class ClientCnxnSocketTest {
             assertEquals("Packet len " + length + " is out of range!", e.getMessage());
         }
     }
+
+    @Test
+    public void testClientCanBeClosedWhenNotInitialized() throws IOException {
+        ZKClientConfig clientConfig = new ZKClientConfig();
+        final ClientCnxnSocketNetty clientCnxnSocket = new ClientCnxnSocketNetty(clientConfig);
+        // Should not throw
+        clientCnxnSocket.close();
+        // Call onClosing explicitly since it otherwise won't be invoked without more setup.
+        clientCnxnSocket.onClosing();
+    }
 }