Don't hijack the global uncaught exception handler during Docker probing#11851
Open
seonwooj0810 wants to merge 1 commit into
Open
Conversation
DockerClientProviderStrategy.test() probes the Docker socket through an executor-backed Awaitility await. With Awaitility's default settings, uncaught-exception catching is enabled, which installs Awaitility's own Thread.UncaughtExceptionHandler as the JVM-global default handler for the duration of the await. This silently replaces the application's handler while Testcontainers initializes (see the stack trace in testcontainers#11483). The probed condition runs on a single dedicated thread and never relies on exceptions propagating from other threads, so catching uncaught exceptions provides no value here. Opt out with dontCatchUncaughtExceptions() so the application's global handler is left untouched. Fixes testcontainers#11483
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 #11483
Problem
DockerClientProviderStrategy.test()probes the Docker socket through an executor-backedAwaitility.await()...untilAsserted(...). Awaitility's default has uncaught-exception catching enabled, and in that modeConditionAwaiterinstalls its ownThread.UncaughtExceptionHandleras the JVM-global default handler for the duration of the await:So while Testcontainers initializes, the application's global uncaught exception handler is replaced and exceptions can be intercepted by Awaitility instead of the application's handler — exactly the stack trace in #11483:
Fix
The probed condition (
socket.connect(...)) runs on a single dedicated poll thread and never relies on exceptions surfacing from other threads, so Awaitility's uncaught-exception catching provides no value here. Opt out per-call withdontCatchUncaughtExceptions()so the application's global handler is left untouched.This is scoped to the one affected call site. I verified that the other
Awaitility.await()usages in core (GenericContainer,HostPortWaitStrategy,RemoteDockerImage) all usepollInSameThread(), and same-thread polling does not install the global handler — so they are unaffected (confirmed by the test below). A per-call opt-out is preferred over the globalAwaitility.doNotCatchUncaughtExceptionsByDefault()switch, which would change Awaitility's behaviour for user code too.Test evidence
Added
AwaitilityUncaughtExceptionHandlerTest, which drives the same executor-backed awaittest()uses and observes the active default handler while polling:defaultExecutorPollingHijacksTheGlobalHandler— documents the broken behaviour (handler is replaced).dontCatchUncaughtExceptionsKeepsTheGlobalHandler— with the fix the application handler stays in place.Both pass; the first fails if
dontCatchUncaughtExceptions()is removed.Verification done
UncaughtExceptionHandler).main: decompiled the shaded Awaitility 4.3.0ConditionAwaiterto verify the handler install is gated onshouldCatchUncaughtExceptions(), and demonstrated via the test that executor polling installs it while same-thread polling does not../gradlew :testcontainers:test --tests "...AwaitilityUncaughtExceptionHandlerTest"and:testcontainers:spotlessApplypass.