/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.curator.framework.imps;

import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.curator.utils.ThreadUtils;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import java.io.Closeable;

class NamespaceWatcher implements Watcher, Closeable
{
    private volatile CuratorFrameworkImpl client;
    private volatile Watcher actualWatcher;
    private volatile CuratorWatcher curatorWatcher;

    NamespaceWatcher(CuratorFrameworkImpl client, Watcher actualWatcher)
    {
        this.client = client;
        this.actualWatcher = actualWatcher;
        this.curatorWatcher = null;
    }

    NamespaceWatcher(CuratorFrameworkImpl client, CuratorWatcher curatorWatcher)
    {
        this.client = client;
        this.actualWatcher = null;
        this.curatorWatcher = curatorWatcher;
    }

    @Override
    public void close()
    {
        client = null;
        actualWatcher = null;
        curatorWatcher = null;
    }

    @Override
    public void process(WatchedEvent event)
    {
        if ( client != null )
        {
            if ( actualWatcher != null )
            {
                actualWatcher.process(new NamespaceWatchedEvent(client, event));
            }
            else if ( curatorWatcher != null )
            {
                try
                {
                    curatorWatcher.process(new NamespaceWatchedEvent(client, event));
                }
                catch ( Exception e )
                {
                    ThreadUtils.checkInterrupted(e);
                    client.logError("Watcher exception", e);
                }
            }
        }
    }
}

@@ -1677,24 +1677,37 @@
 %0A    public 
+synchronized 
 void close()
@@ -1900,32 +1900,130 @@
 ull )%0A        %7B%0A
+            synchronized (this) %7B%0A                if ( client != null )%0A                %7B%0A        
             if (
@@ -2051,34 +2051,50 @@
 l )%0A            
-%7B%0A
+        %7B%0A        
                 
@@ -2162,34 +2162,45 @@
 ));%0A            
-%7D%0A
+        %7D%0A   
             else
@@ -2195,16 +2195,21 @@
         
+     
 else if 
@@ -2239,34 +2239,45 @@
 l )%0A            
-%7B%0A
+        %7B%0A   
                 
@@ -2276,16 +2276,21 @@
         
+     
 try%0A    
@@ -2293,34 +2293,45 @@
                 
-%7B%0A
+        %7B%0A   
                 
@@ -2326,32 +2326,37 @@
                 
+     
 curatorWatcher.p
@@ -2413,34 +2413,45 @@
                 
-%7D%0A
+        %7D%0A   
                 
@@ -2450,16 +2450,21 @@
         
+     
 catch ( 
@@ -2485,34 +2485,44 @@
                 
-%7B%0A
+        %7B%0A  
                 
@@ -2525,16 +2525,22 @@
         
+      
 ThreadUt
@@ -2560,24 +2560,32 @@
 rrupted(e);%0A
+        
             
@@ -2629,24 +2629,72 @@
 ption%22, e);%0A
+                        %7D%0A                    %7D%0A
             

Fix NPE during race between ZooKeeperGroup.close() and org.apache.curator.framework.imps.NamespaceWatcher.process()