Fix stopDispatcherThreads() to block until dispatcher threads have actually terminated#1149
Draft
Fix stopDispatcherThreads() to block until dispatcher threads have actually terminated#1149
stopDispatcherThreads() to block until dispatcher threads have actually terminated#1149Conversation
…ads have actually terminated - Remove premature `stopped = true` from `stopDispatcher()` so `isStopped()` only returns true when `doRun()` fully completes, not when a stop was merely requested. - Add `CountDownLatch doneLatch` to `ThreadAdapter` and count it down in the `run()` finally block; expose `awaitTermination(timeout, unit)` so callers can join the thread even when backed by an external Executor. - Rewrite `stopDispatcherThreads()` to snapshot the dispatcher map (avoids live-view concurrent-modification surprises) and call `awaitTermination()` on each dispatcher instead of polling `isStopped()`. - Add regression test `testStopDispatcherThreadsIsBlocking` that enqueues a slow-processing message, calls `stopDispatcherThreads()`, then immediately asserts (no extra sleep) that the dispatcher thread is dead. Co-authored-by: chrjohn <6644028+chrjohn@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix stop() method for ThreadPerSessionEventHandlingStrategy
Fix stopDispatcherThreads() to block until dispatcher threads have actually terminated
Feb 25, 2026
stopDispatcherThreads() to block until dispatcher threads have actually terminated
Member
|
@copilot the test fails on windows with jdk21, but probably it is a general problem |
The previous implementation called CountDownLatch.countDown() in the run() finally block, but this fires BEFORE Thread.isAlive() transitions to false. On Windows/JDK21 the gap is large enough to cause the test assertion to fail. Replace the done-latch approach with thread tracking + Thread.join(): - Capture runnerThread = Thread.currentThread() at the top of run() - Signal startedLatch so awaitTermination() knows the thread reference is set - awaitTermination() now calls runnerThread.join(remaining) which blocks until the thread has truly terminated, guaranteeing isAlive() == false on return. Co-authored-by: chrjohn <6644028+chrjohn@users.noreply.github.com>
Contributor
Author
Fixed in dcf45b2. The root cause was a race between Replaced the done-latch with thread tracking +
|
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.
stopDispatcher()setsstopped = trueimmediately, making the wait loop exit before the dispatcher thread actually finishesstopped = truefromstopDispatcher()soisStopped()reflects real terminationThreadAdapter: capturerunnerThreadinrun(), signalstartedLatch, thenawaitTermination()callsThread.join()— guaranteesisAlive() == falseon return (fixes Windows/JDK21 race)stopDispatcherThreads()to snapshot dispatchers (not use live view) and useawaitTermination()instead of pollingisStopped()testStopDispatcherThreadsIsBlocking: enqueue slow-processing message, call stop, assert thread is dead immediately after stop returns (no polling needed)Original prompt
This pull request was created from Copilot chat.
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.