You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PhantomReachableExecutorService is never cleaned up because its Cleaner action captures the wrapper #1337
PhantomReachableExecutorService is meant to shut down its executor once the wrapper is only phantom reachable.
It can't, because the action registered with the Cleaner holds a strong reference to the wrapper:
Here executorService compiles to a read of the private property through this.
The generated lambda has a this$0 field, as javap -p -c 'com.openai.core.PhantomReachableExecutorService$1' shows on 4.49.0, 4.80.0 and 4.99.0:
final com.openai.core.PhantomReachableExecutorService this$0;
...
invokestatic com/openai/core/PhantomReachableExecutorService.access$getExecutorService$p
invokeinterface java/util/concurrent/ExecutorService.shutdown:()V
The Cleaner holds the action strongly, so the wrapper and its executor stay reachable for the life of the JVM.
Impact
ClientOptions.Builder.build() creates this wrapper by default, and streamHandlerExecutor(...) applies it to any ExecutorService.
So every client strands one wrapper and one ThreadPoolExecutor, whether or not it ever streams.
Calling close() shuts the pool down but doesn't release it.
This matters to applications that build clients repeatedly, for example one per tenant API key.
Reproduction
Build 2,000 sync and async client pairs, discard them, then take a class histogram with jcmd <pid> GC.class_histogram, which forces a full GC.
Client setup
Retained PhantomReachableExecutorService
Default
4,000
Default, calling close() on each client
4,000
streamHandlerExecutor given a plain Executor
0
Clients, sleepers and HTTP client wrappers were released in all three.
Suggested fix
Make the action capture the delegate rather than the wrapper, for example by registering it from a function that doesn't have this in scope, or by copying the delegate into a local first:
This suggestion hasn't been compiled or run.
A regression test could register a wrapper, drop it, and assert through a WeakReference that it's collected after System.gc(), or that the delegate's isShutdown becomes true.
Workaround
Pass an Executor that isn't an ExecutorService to ClientOptions.Builder.streamHandlerExecutor(...).
Activity
Rishankgupta08 commentedon Nov 14, 2026
Hi maintainers!
I'd be interested in contributing to this issue.
The Cleaner lifecycle and the strong reference captured by its cleanup action caught my attention.
I'd like to investigate a fix that allows the wrapper to become phantom reachable while still reliably shutting down the underlying executor.
I can also add regression coverage to ensure the wrapper and its executor are properly released.
Would this approach be suitable for a contribution? I'm also happy to work within the project's Kotlin conventions.
Thanks!