RFR: 8362123: ClassLoader Leak via Executors.newSingleThreadExecutor(...)
Viktor Klang
vklang at openjdk.org
Fri Sep 26 15:31:26 UTC 2025
On Mon, 14 Jul 2025 15:49:04 GMT, Chris Dennis <duke at openjdk.org> wrote:
> Executors shutdown via `shutdownNow()` should have their cleanables cleaned to prevent a classloader leak. This can happen if a classloader exists that both references the wrapped executor and is referenced by the delegate executor.
>
> To quote @Martin-Buchholz:
>> BTW: I find Cleaners much harder to use than old finalize, and it looks like I'm not the only one!
test/jdk/java/util/concurrent/Executors/AutoShutdown.java line 138:
> 136:
> 137: assertTrue(ForceGC.wait(() -> queue.poll() != null));
> 138: Reference.reachabilityFence(reference);
`reachabilityFence` should be placed in a finally-block as per: https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/ref/Reference.html#reachabilityFence(java.lang.Object)
Suggestion:
try {
ClassLoader classLoader = Utils.getTestClassPathURLClassLoader(ClassLoader.getPlatformClassLoader());
ReferenceQueue<?> queue = new ReferenceQueue<>();
Reference<?> reference = new PhantomReference(classLoader, queue);
classLoader.loadClass("AutoShutdown$IsolatedClass").getDeclaredMethod("shutdown", Consumer.class).invoke(null, shutdown);
classLoader = null;
assertTrue(ForceGC.wait(() -> queue.poll() != null));
} finally {
Reference.reachabilityFence(reference);
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/26296#discussion_r2382730005
More information about the core-libs-dev
mailing list