Fix NullPointerException on concurrent GenericContainer.stop() calls#11914
Open
nikita-kibitkin wants to merge 1 commit into
Open
Fix NullPointerException on concurrent GenericContainer.stop() calls#11914nikita-kibitkin wants to merge 1 commit into
nikita-kibitkin wants to merge 1 commit into
Conversation
stop() re-read the mutable containerId field after the null guard, so a concurrent stop() that completes in between, or a containerIsStopping() hook that reenters stop(), made the second read pass null to ResourceReaper.stopAndRemoveContainer, crashing with NullPointerException in ConcurrentHashMap.remove. Read the container state into locals once instead; the duplicate removal attempt is already tolerated by ResourceReaper (NotFoundException is swallowed). Fixes testcontainers#11492
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.
Fixes #11492
stop() reads the mutable containerId field twice: in the null guard and again when calling ResourceReaper.stopAndRemoveContainer(). The only null write is stop()'s own finally block, so a concurrent stop() that completes between the two reads (the Trino CI case: cleanup racing after a failed startup), or a containerIsStopping() hook that reenters stop(), makes the second read pass null to the reaper and crash with the reported NullPointerException in ConcurrentHashMap.remove(). The fix reads the container state into locals once, so null can never reach the reaper from stop().
This is deliberately fixed at the caller rather than with a null guard inside ResourceReaper.stopAndRemoveContainer() (suggested earlier in the issue): the reaper methods are deprecated public API with a non-null contract, and a guard there would hide caller bugs instead of fixing this one. The duplicate removal attempt by the losing thread is already tolerated - removeContainer() swallows NotFoundException. stop() intentionally stays non-atomic: exactly-once semantics would require holding a lock across Docker calls and user hooks, and the duplicate path is benign.
Both race variants have deterministic tests: a latch-based concurrent test and a reentrant-hook test. Both fail on main with the exact NPE from the issue and pass with the fix; full GenericContainerTest is green; spotless applied.