openai / openai/openai-java

Phantom-reachable wrappers can close resources twice after explicit cleanup

Open
#884 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
1.5k
Forks
264
Avg merge
9h 46m
Merged PRs (30d)
96

Description

Description

The phantom-reachability helper registers a Java 9+ Cleaner action but discards the registration handle. Wrapper classes such as PhantomReachableClosingHttpClient, PhantomReachableSleeper, PhantomReachableClosingStreamResponse, PhantomReachableClosingAsyncStreamResponse, and PhantomReachableClosingHttpRequestAuthenticator then implement explicit close() by calling the underlying resource directly.

That creates two independent cleanup paths:

  1. explicit wrapper close;
  2. the still-registered cleaner action after the wrapper becomes phantom reachable.

A resource that is closed explicitly can therefore be closed a second time later by the cleaner.

Why it matters

AutoCloseable.close() is not generally required to be idempotent. Provider-owned authenticators, custom HTTP clients, stream responses, or sleepers can perform stateful cleanup or throw on repeated close. Cleanup should run at most once regardless of whether it is triggered explicitly or by GC fallback.

Expected behavior

Explicit cleanup and phantom-reachable cleanup should share one idempotent close gate. An explicit close should mark the registered cleanup as already performed, so a later Cleaner invocation becomes a no-op.

Suggested approach

Have closeWhenPhantomReachable(...) return a small close handle backed by a shared atomic once-guard. Register that same guarded action with the Cleaner and have explicit-close wrappers call the returned handle instead of closing the delegate independently.

Java 8 can use the same handle for explicit cleanup while retaining the existing no-Cleaner fallback.

Scope

The executor-service wrapper is less sensitive because repeated shutdown() is safe, but the AutoCloseable wrappers should not rely on delegate idempotency.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating closeWhenPhantomReachable(...) and the named PhantomReachable wrapper classes. Trace both the Java 8 fallback and Java 9+ Cleaner paths, then verify that explicit cleanup followed by phantom cleanup performs underlying resource cleanup at most once.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
66/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.