fix: prevent duplicate phantom-reachable cleanup - #885
Open
sylvesterkaczmarek wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Make explicit close and phantom-reachable fallback cleanup share one at-most-once action for the SDK's closable phantom wrappers.
Fixes #884.
Problem
closeWhenPhantomReachable(...)currently registers a Java 9+ Cleaner action and discards the registration handle. The closable wrappers then implement explicitclose()by invoking the underlying resource independently.That leaves two active cleanup paths:
A delegate can therefore be closed twice.
AutoCloseable.close()is not generally required to be idempotent, and provider-owned/custom resources can perform stateful cleanup or throw on a repeated close.Fix
closeWhenPhantomReachable(...)now:AtomicBoolean.compareAndSet(false, true);AutoCloseablehandle that invokes the same closure explicitly.The five closable phantom wrappers retain that handle and use it from
close():PhantomReachableSleeperPhantomReachableClosingHttpClientPhantomReachableClosingStreamResponsePhantomReachableClosingAsyncStreamResponsePhantomReachableClosingHttpRequestAuthenticatorThe Java 8 behavior remains compatible: there is still no GC Cleaner, while the returned handle performs explicit cleanup through the same once-guard.
PhantomReachableExecutorServiceis intentionally unchanged because its lifecycle API isshutdown/shutdownNowrather thanAutoCloseable, and repeatedshutdown()has different semantics.Regression coverage
PhantomReachableClosingHttpClient.close()closes its delegate at most once across repeated explicit calls.Validation
mainatcf942a40074291290634321ad9fe21e514030b4c;Full repository validation is left to GitHub Actions.
Risk
Low. Forgotten resources still receive Cleaner-based fallback cleanup. Explicitly closed resources now suppress the later duplicate cleanup instead of relying on every delegate to tolerate being closed twice.