fix: stop WireMock servers when context closes during AOT processing - #200
Merged
tomasbjerre merged 1 commit intoJul 13, 2026
Merged
Conversation
moyis
force-pushed
the
fix/stop-wiremock-on-aot-processing
branch
from
July 13, 2026 16:59
b9ad4ba to
8aea333
Compare
During Spring test AOT processing (SpringBootTestAotProcessor), the test application context is loaded and closed outside the JUnit lifecycle. The ContextClosedEvent is never delivered in that case (the event multicaster is not initialized by refreshForAotProcessing), so the WireMock servers were never stopped and their non-daemon Jetty threads kept the AOT processing JVM alive forever, hanging processTestAot. Singletons are still destroyed when that context is closed, so each started server is now also registered as a disposable bean. The existing ContextClosedEvent listener is kept, and both paths are guarded against double-stop. Fixes wiremock#199
moyis
force-pushed
the
fix/stop-wiremock-on-aot-processing
branch
from
July 13, 2026 17:00
8aea333 to
9aa48c3
Compare
Collaborator
|
Thanks! |
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 #199
Problem
When a
@SpringBootTestclass uses@EnableWireMock, Gradle'sprocessTestAot(SpringBootTestAotProcessor) hangs forever. During test AOT processing, the test application context is loaded and closed outside the JUnit lifecycle:TestContextAotGeneratorloads the context viarefreshForAotProcessing(), which runsprepareRefresh()but never initializes the application event multicaster.doClose()publishes theContextClosedEventinto the still-activeearlyApplicationEventsset, so it is never delivered.ApplicationListener<ContextClosedEvent>, so the WireMock servers were never stopped and their non-daemon Jettyqtp*threads kept the AOT processing JVM alive aftermain()returned. Gradle waits on the forked process forever.Fix
doClose()still destroys singletons even in that path, so each started server is now also registered as a disposable bean in the singleton registry. The existingContextClosedEventlistener is kept for the regular lifecycle, and both paths route through a single stop method guarded byserver.isRunning(), so the server is never stopped twice.An alternative considered was skipping server startup entirely when running under AOT processing (
spring.aot.processingproperty). Rejected: it would require faking the injected base URL/port properties so context loading still succeeds, and it makes the AOT pass behave differently from a regular context load. Stopping the servers when the context that started them is closed fixes the root cause without behavior divergence.Testing
AotProcessingTestrunsTestContextAotGenerator.processAheadOfTime()against an@EnableWireMocktest class — the same context load + close asSpringBootTestAotProcessor, outside JUnit'sSpringExtension— and asserts no new non-daemonqtp*Jetty threads survive the context close. Fails without the fix, passes with it../gradlew buildgreen.qtp*threads); with this patch./gradlew processTestAotcompletes in seconds — the log shows Jetty shutting down when the AOT context is closed.