fix(agent): wake the shutdown waiter that notify_waiters left behind - #79
Merged
Conversation
`Shutdown::stop` signalled with `Notify::notify_waiters`, which wakes only the tasks already parked at that instant and stores no permit. Both waits -- the session loop and the reconnect backoff -- awaited `notified()` directly, with no level-triggered read of the flag `stop` had already set. When `stop` landed while the client was anywhere else (mid-recovery, mid-register, mid-dispatch) the signal was lost. The session loop then parked on a notification already gone, the inbound stream stayed open and silent, `session` never returned, and `run` never reached its `is_stopped` check. That is a permanent hang, not a slow one: 26 hours parked on a condvar at zero CPU on CI, ~1 run in 50 locally. It presented as a hung `cargo test --workspace` and was twice mistaken for a stuck runner, because a hang leaves no failing test behind to point at. `Shutdown::cancelled` registers as a waiter before reading the flag, and `stop` writes the flag before notifying. Two opposed orderings, so no interleaving misses both. The regression test asserts under a timeout on purpose -- the bug hangs rather than fails, so a bare assertion would reproduce it instead of reporting it. Verified by sabotaging `cancelled` back to the naive form and confirming the test goes red. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01U9keBRnGXBP2DiQmrcqm33
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.
The hang, root-caused
cargo test --workspacehung for 20+ minutes on CI and reproduced locally. It wasdiagnosed twice as a stuck runner. It is a real defect, and this fixes it.
A
cargo test --workspacefrom the previous evening was still alive after 26hours. Sampling it named the parked frame exactly:
Root cause
Shutdown::stopsignals withNotify::notify_waiters, which wakes only the tasksalready parked at that instant and stores no permit for one arriving later.
Both waits — the session loop (
client.rs:573) and the reconnect backoff(
client.rs:500) — awaitednotified()directly, with no level-triggered read ofthe
stoppedflag thatstophad already set.So when
stoplanded while the client was anywhere else — mid-recovery,mid-register, mid-dispatch — the wakeup was lost. The session loop then parked on
a notification already gone while the inbound stream stayed open and silent.
session()never returned, sorun()never reached theis_stopped()check thatends it. Permanent, not slow.
Evidence
feat/transport-mode, one test ×260main(pre-merge), same test ×260connect_clientbinary ×150The defect is latent in
client.rs, which #78 never touched; #78 appears to havewidened the window (the added
transportfield lengthensregister, delaying theclient's arrival at the park). The main-vs-branch split is suggestive, not proof —
separate target dirs mean code layout differs — so treat this as exposed by, not
caused by, #78.
The fix
Shutdown::cancelledregisters as a waiter before reading the flag;stopwrites the flag before notifying. Two opposed orderings, so no interleaving can
miss both. Used at both wait sites.
On the test
It asserts under a
timeoutdeliberately. The bug hangs rather than fails, so abare assertion would reproduce the disease instead of reporting it — which is
precisely why this cost 26 hours with no failing test to point at.
Verified honestly:
cancelledwas temporarily sabotaged back to the naivenotified().awaitand the test went red withElapsed(()), while thealready-parked-waiter test stayed green. The two cover opposite directions.
🤖 Generated with Claude Code
https://claude.ai/code/session_01U9keBRnGXBP2DiQmrcqm33