Skip to content

fix(server): cancel a /v1 run when its client disconnects - #516

Merged
mrnasil merged 1 commit into
mainfrom
fix/server-cancel-on-disconnect
Nov 27, 2026
Merged

mrnasil merged 1 commit into
mainfrom
fix/server-cancel-on-disconnect

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 27, 2026

Copy link
Copy Markdown
Member

Problem

A /v1/sessions/:id/run turn 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 /v1 stream cannot be joined again, so nobody could read that output.

Fix

The stream owns a CancelOnDisconnect guard. 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 the stream! 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_done now cancels the run. docs/guide.md says 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 on main, passes here.
  • cargo test -p whycodes-server: 96 passed. cargo test -p whycodes-sdk: 53 passed. Clippy, fmt, budget scripts: green.
  • Coverage: server crate measured locally after cargo llvm-cov clean, no uncovered lines. The full scripts/coverage.sh workspace run and the TypeScript SDK tests were not run locally.
  • docs/knowhow.md has the entry.

🤖 Generated with Claude Code

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]>
@mrnasil
mrnasil merged commit f19e4d1 into main Nov 27, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/server-cancel-on-disconnect branch November 27, 2026 20:06
@mrnasil mrnasil mentioned this pull request Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant