Skip to content

Commit b26f550

Browse files
Fix AXIS2-5696: Prevent ThreadPool thread leaks by enabling core thread timeout
Enable allowCoreThreadTimeOut(true) on the ThreadPoolExecutor, which was commented out with a JDK 1.6 FIXME that is long obsolete. Idle core threads now terminate after the 10-second keepAlive period instead of persisting indefinitely. Also shut down the ThreadPool in ConfigurationContext.terminate() as defense-in-depth. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 370ec7f commit b26f550

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

modules/kernel/src/org/apache/axis2/context/ConfigurationContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,10 @@ public void shutdownModulesAndServices() throws AxisFault{
737737
*/
738738
public void terminate() throws AxisFault {
739739
shutdownModulesAndServices();
740+
// AXIS2-5696: Shut down the thread pool to prevent thread leaks
741+
if (threadPool instanceof ThreadPool) {
742+
((ThreadPool) threadPool).safeShutDown();
743+
}
740744
if (listenerManager != null) {
741745
listenerManager.destroy();
742746
}

modules/kernel/src/org/apache/axis2/util/threadpool/ThreadPool.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ TimeUnit.SECONDS, new SynchronousQueue(),
114114
TimeUnit.SECONDS, new LinkedBlockingQueue(),
115115
new DefaultThreadFactory(name, daemon, priority));
116116
}
117-
// FIXME: This API is only in JDK 1.6 - Use reflection?
118-
// rc.allowCoreThreadTimeOut(true);
117+
rc.allowCoreThreadTimeOut(true);
119118
return rc;
120119
}
121120

modules/kernel/test/org/apache/axis2/util/threadpool/TestThreadPool.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import java.util.ArrayList;
2626
import java.util.List;
27+
import java.util.concurrent.ThreadPoolExecutor;
28+
import java.util.concurrent.TimeUnit;
2729

2830
public class TestThreadPool extends AbstractTestCase {
2931
/**
@@ -46,7 +48,7 @@ public boolean isWorkDone() {
4648
}
4749

4850

49-
public void testPool() throws AxisFault {
51+
public void testPool() throws Exception {
5052
ThreadPool tPool = new ThreadPool();
5153
List workerList = new ArrayList();
5254

@@ -57,11 +59,43 @@ public void testPool() throws AxisFault {
5759
}
5860

5961
tPool.safeShutDown();
62+
ThreadPoolExecutor executor = (ThreadPoolExecutor) tPool.getExecutor();
63+
executor.awaitTermination(5, TimeUnit.SECONDS);
6064

6165
for (int i = 0; i < 5; i++) {
6266
assertEquals(true, ((TestWorker) workerList.get(i)).isWorkDone());
6367
}
6468

6569
}
6670

71+
/**
72+
* Test that core threads time out and terminate after the keepAlive period
73+
* when allowCoreThreadTimeOut is enabled (AXIS2-5696).
74+
*/
75+
public void testCoreThreadsTimeOut() throws Exception {
76+
ThreadPool tPool = new ThreadPool();
77+
ThreadPoolExecutor executor = (ThreadPoolExecutor) tPool.getExecutor();
78+
79+
// Verify allowCoreThreadTimeOut is enabled
80+
assertTrue("allowCoreThreadTimeOut should be enabled",
81+
executor.allowsCoreThreadTimeOut());
82+
83+
// Submit a task directly to the executor to create a core thread
84+
TestWorker worker = new TestWorker();
85+
executor.execute(worker);
86+
87+
// Wait briefly for the task to complete and the thread to be created
88+
Thread.sleep(500);
89+
assertTrue("Worker should have completed", worker.isWorkDone());
90+
assertTrue("Pool should have at least one thread",
91+
executor.getPoolSize() > 0);
92+
93+
// Wait for keepAlive timeout (10 seconds) plus buffer
94+
Thread.sleep(12_000);
95+
96+
// Core threads should have timed out and terminated
97+
assertEquals("All core threads should have timed out", 0,
98+
executor.getPoolSize());
99+
}
100+
67101
}

0 commit comments

Comments
 (0)