This is an automated email from the ASF dual-hosted git repository.

hulee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/helix.git


The following commit(s) were added to refs/heads/master by this push:
     new ff0485b  Add null check in close() of ZkConnectionManager (#1016)
ff0485b is described below

commit ff0485bdb65d1051ca63ecc97c5b864aee932465
Author: Hunter Lee <[email protected]>
AuthorDate: Mon May 18 21:49:37 2020 -0700

    Add null check in close() of ZkConnectionManager (#1016)
    
    Java objects may not have been fully initialized when their 
callback/interface methods are called. This adds a null check to ensure that an 
NPE does not happen.
---
 .../helix/zookeeper/impl/factory/ZkConnectionManager.java | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git 
a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/impl/factory/ZkConnectionManager.java
 
b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/impl/factory/ZkConnectionManager.java
index 1413111..82b3ab9 100644
--- 
a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/impl/factory/ZkConnectionManager.java
+++ 
b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/impl/factory/ZkConnectionManager.java
@@ -118,7 +118,7 @@ public class ZkConnectionManager extends ZkClient {
 
   protected synchronized void close(boolean skipIfWatched) {
     cleanupInactiveWatchers();
-    if (_sharedWatchers.size() > 0) {
+    if (_sharedWatchers != null && _sharedWatchers.size() > 0) {
       if (skipIfWatched) {
         LOG.debug("Skip closing ZkConnection due to existing watchers. Watcher 
count {}.",
             _sharedWatchers.size());
@@ -134,12 +134,15 @@ public class ZkConnectionManager extends ZkClient {
 
   protected void cleanupInactiveWatchers() {
     Set<Watcher> closedWatchers = new HashSet<>();
-    for (Watcher watcher : _sharedWatchers) {
-      // TODO ideally, we shall have a ClosableWatcher interface so as to 
check accordingly. -- JJ
-      if (watcher instanceof SharedZkClient && ((SharedZkClient) 
watcher).isClosed()) {
-        closedWatchers.add(watcher);
+    // Null check needed because close() might get invoked before 
initialization
+    if (_sharedWatchers != null) {
+      for (Watcher watcher : _sharedWatchers) {
+        // TODO ideally, we shall have a ClosableWatcher interface so as to 
check accordingly. -- JJ
+        if (watcher instanceof SharedZkClient && ((SharedZkClient) 
watcher).isClosed()) {
+          closedWatchers.add(watcher);
+        }
       }
+      _sharedWatchers.removeAll(closedWatchers);
     }
-    _sharedWatchers.removeAll(closedWatchers);
   }
 }

Reply via email to