ClassLoader Leak via Executors.newSingleThreadExecutor(...)

Chris Dennis chris.w.dennis at gmail.com
Fri Jul 11 14:42:54 UTC 2025


Hi All,

I believe I've identified a bug in
Executors.AutoShutdownDelegatedExecutorService that can trigger a
classloader leak even in the presence of "correct" Executor lifecycling.
AutoShutdownDelegatedExecutorService only unlinks the PhantomReference used
for cleanup handling when it is shutdown via the shutdown() method. If an
Executor wrapped in this way is instead shutdown using the shutdownNow()
method and it references a classloader via an injected attribute:
ThreadFactory, AbortPolicy, etc. then the cleanup action will reference the
classloader, and the classloader will remain strongly referenced. Adding an
additional override as shown in the attached patch is sufficient to fix the
leak in my testing.

Thanks,

Chris

P.S. I believe I'm likely covered wrt OCA via my employer IBM.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250711/2049d68e/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: executor-classloader-leak.patch
Type: application/octet-stream
Size: 724 bytes
Desc: not available
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250711/2049d68e/executor-classloader-leak-0001.patch>


More information about the core-libs-dev mailing list