Repository navigation
Wait only on the stopped loop in StopAsync when a restart follows [patch] - #92
Merged
Merged
Conversation
…tch] StopAsync read PollingTask after the stop, so a Restart() requested straight afterwards could publish its new loop first, and StopAsync then waited on that loop, which polls until some later stop. Capture the loop and run together with the stop, under the lock, and publish a restart's loop under the same lock as its stop-generation check, so a stop sees either that loop or none. Fixes #91 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW2nrCvY4DyoEF72Nk1
|
This was referenced Nov 1, 2026
This was referenced Nov 11, 2026
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 #91
What was wrong
StopAsync()captured only the restart pending at the time of the stop. It awaited that restart and only then readPollingTask(and laterActionTask). ARestart()/RestartAsync()requested after the stop is meant to run normally, and it published its new loop asPollingTaskoutside the lock. When that publish won the race,StopAsync()waited on the new loop, which keeps polling until some later stop, so the returned task never completed. This is the pause-then-resume hang.Change
RestartCoreAsyncpublishesPollingTaskinside the same locked block that checksStopGeneration. The loop body moves to a privatePollAsyncmethod with unchanged logic. TheCancellationTokenSourceis now created after the check, so a cancelled restart creates nothing.StopCorecapturesPollingTaskandActionTaskunder the lock, together with the generation bump. A restart that had passed its check has therefore already published the loop that gets captured, and any restart after that publishes nothing.TryRunalready refuses to claim a run after a stop, so the capturedActionTaskis the last run that loop can start.StopAsync()awaits exactly those two tasks. It no longer awaits the pending restart, which was only there to catch a loop published late, and that can no longer happen.Verification
StopAsyncCompletesWhenARestartFollowsItruns the issue's repro: 3000 trials ofStopAsync()followed byawait RestartAsync()with 1 ms intervals, and 32 minimum pool threads restored afterwards. It also checks that the resumed action still runs.Of 3000 pause-then-resume trials, StopAsync was still pending after 500 ms in 141, and the resumed action never ran in 0.dotnet test IntervalAction.Test/IntervalAction.Test.csproj): 47/47 pass, including the existing Stop() called while a RestartAsync() is still pending is ignored, so the action keeps running after the caller stopped it #57/Stop() followed by Restart() leaves the old polling loop alive, so two loops run the action and it can execute concurrently #61/Restart right after Stop leaves the old polling loop alive, so two loops run the action at double rate and can overlap it #52 stop-and-restart tests.dotnet build -c Release: 0 warnings.Generated by Claude Code