Repository navigation
fix(server): cancel a /v1 run when its client disconnects - #516
Merged
Merged
Conversation
The turn runs on its own task and the SSE stream only forwards its events. A client that closed the stream mid-turn left the turn running: it spent tokens, held the session so the next run queued behind it, and each permission ask waited out its 300 s timeout. A /v1 stream cannot be joined again, so nobody could ever read that output. The stream now owns a guard that sets the run's cancel flag when it is dropped before the run ended. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Merged
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.
Problem
A
/v1/sessions/:id/runturn runs on its own task; the SSE stream only forwards its events. A client that closed the stream mid-turn (crash, timeout, Ctrl+C in a script) left the turn running in the daemon. It spent tokens, held the session so the next run queued behind it, and each permission ask waited out its 300 s timeout. A/v1stream cannot be joined again, so nobody could read that output.Fix
The stream owns a
CancelOnDisconnectguard. Dropped before the run ended, it sets the run's cancel flag, and the turn stops through the normal cancel path (session saved, registrations cleaned up). The guard is built outside thestream!block, so a stream dropped before its first poll still cancels.This is a behaviour change for SDK clients: closing the event stream before
turn_donenow cancels the run.docs/guide.mdsays so./api/session/:id/chat(TUI attach) is unchanged.Verification
a_run_is_cancelled_when_its_client_leaves: the provider hangs for 60 s, the response is dropped, and the session must be free within 10 s. Fails onmain, passes here.cargo test -p whycodes-server: 96 passed.cargo test -p whycodes-sdk: 53 passed. Clippy, fmt, budget scripts: green.cargo llvm-cov clean, no uncovered lines. The fullscripts/coverage.shworkspace run and the TypeScript SDK tests were not run locally.docs/knowhow.mdhas the entry.🤖 Generated with Claude Code