curator-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shawn Smith (JIRA)" <>
Subject [jira] [Created] (CURATOR-66) PathChildrenCache leaks a thread on close().
Date Thu, 17 Oct 2013 18:36:42 GMT
Shawn Smith created CURATOR-66:

             Summary: PathChildrenCache leaks a thread on close().
                 Key: CURATOR-66
             Project: Apache Curator
          Issue Type: Bug
          Components: Recipes
    Affects Versions: 2.2.0-incubating
            Reporter: Shawn Smith

The PathChildrenCache does not shutdown its ExecutorService in close().  As a result, using
any of the PathChildrenCache constructors that create a single threaded executor (which is
all of them but the one that takes an ExecutorService explicitly) will leak a thread when
PathChildrenCache.close() is called.

Here's a program that reproduces the error.  Currently it prints "# Total PathChildrenCache
threads: 100" to indicate that 100 threads were leaked.

import org.apache.curator.ensemble.fixed.FixedEnsembleProvider;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;

import java.util.Map;

public class ThreadLeakTest {
    public static final int NUM_ITERATIONS = 100;

    public static void main(String[] args) throws Exception {
        CuratorFramework curator = CuratorFrameworkFactory.builder()
                .ensembleProvider(new FixedEnsembleProvider("localhost:2181"))
                .retryPolicy(new RetryOneTime(100))

        for (int i = 0; i < NUM_ITERATIONS; i++) {
            PathChildrenCache cache = new PathChildrenCache(curator, "/", true);


        // Wait for things to settle

        int threadCount = 0;
        Map<Thread,StackTraceElement[]> allThreads = Thread.getAllStackTraces();
        for (Thread thread : allThreads.keySet()) {
            if (thread.getName().startsWith("PathChildrenCache")) {
                System.out.printf("Thread still alive: %s%n", thread.getName());
        System.out.printf("# Total PathChildrenCache threads: %d%n", threadCount);

Workaround: use the PathChildrenCache constructor that takes an ExecutorService and call shutdown()
yourself.  Note that closing the CuratorFramework ZK connection does not clean up the leaked

This message was sent by Atlassian JIRA

View raw message