-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Problem Description
The org.apache.curator.utils.ThreadUtils.newFixedThreadPool method internally wraps Executors.newFixedThreadPool:
// ThreadUtils.java
public static ExecutorService newFixedThreadPool(int qty, String processName) {
return Executors.newFixedThreadPool(qty, newThreadFactory(processName));
}
Root Cause
The JDK's Executors.newFixedThreadPool uses a LinkedBlockingQueue with a default capacity of Integer.MAX_VALUE. This creates an unbounded queue.
Impact
In distributed scenarios (e.g., high ZooKeeper server latency or connection storms), background tasks and callbacks generated by Curator clients will accumulate indefinitely in this queue. Since there is no backpressure mechanism (queue capacity limit), this eventually leads to java.lang.OutOfMemoryError: Java heap space.
Suggested Fix
Replace the Executors factory method with a direct ThreadPoolExecutor construction using a bounded queue (e.g., ArrayBlockingQueue) and an appropriate RejectedExecutionHandler.
// Proposed change
public static ExecutorService newFixedThreadPool(int qty, String processName) {
return new ThreadPoolExecutor(
qty, qty,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(10000), // Bounded capacity
newThreadFactory(processName)
);
}