fix(server): stop actually stops a runaway Claude turn - #3
Merged
Conversation
Clicking Stop during a Claude turn could freeze every thread in the app for close to two minutes, and then not stop the agent at all. Two defects combined: 1. `ClaudeAdapter.interruptTurn` awaited `context.query.interrupt()` with no time bound. The SDK's interrupt is a control request that can block until the in-flight tool call returns. Measured twice on 2026-08-17: 114s and 111.5s. 2. `ProviderCommandReactor` drains every thread command through a single `DrainableWorker` lane, so that one hung call blocked all threads. Further Stop clicks were persisted and never processed. An acknowledged interrupt is also not proof the turn ended. The event log showed the agent kept working straight through the stall: a burst of identically-timestamped tool calls flushed the moment the queue cleared. So Stop now escalates. It arms a detached watchdog, asks the SDK to interrupt with a 5 second bound on the ack, and if the same turn is still live 5 seconds later it tears the session down through the existing `stopSessionInternal`. That path routes through `completeTurn`, which updates the resume cursor, so a forced teardown still leaves the thread resumable. The reactor's queue stays serial on purpose. Forking the interrupt would let a following `turn-start-requested` race ahead of a session teardown and prompt into a dying session. Bounding the adapter call shrinks the global block from ~112s to ~5s, which is the win without the race. Verified: 72 tests pass in ClaudeAdapter.test.ts. The new never-acknowledged-interrupt test hangs to the 60s harness timeout without the fix. Only the Claude adapter is changed; it is the only provider with observed hangs. Codex, Cursor, Grok and OpenCode are not audited yet. Written by Claude Opus 5 in T3 Code.
xiaogwu
marked this pull request as ready for review
August 17, 2026 02:53
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 problem
Clicking Stop during a Claude turn could freeze every thread in the app for close to two minutes, and then not stop the agent at all.
Two defects combine:
ClaudeAdapter.interruptTurnawaitedcontext.query.interrupt()with no time bound. The Claude Agent SDK's interrupt is a control request that can block until the in-flight tool call returns. Measured twice on 2026-08-17 againstclaudeAgent: 114s and 111.5s.ProviderCommandReactordrains every thread command through a singleDrainableWorkerlane. One hung provider call therefore blocks all threads, on every client. Later Stop clicks were persisted toorchestration_eventsand never processed.An acknowledged interrupt is also not proof the turn ended. The event log shows the agent kept working straight through the stall: a burst of identically-timestamped tool calls flushed the instant the queue cleared.
The fix
Stop now escalates instead of trusting the provider.
interruptTurnarms a detached watchdog, then asks the SDK to interrupt with a 5 second bound on the ack. If the same turn is still live 5 seconds later, the session is torn down through the existingstopSessionInternal. That path routes throughcompleteTurn, which updates the resume cursor, so a forced teardown still leaves the thread resumable and the UI honest about the turn being interrupted.Turn identity (
context.turnState?.turnIdcompared against the id captured at interrupt time) is what distinguishes "same turn still live" from "turn ended" and "a new turn started". No new signal orDeferredwas needed.The watchdog runs on a make-level
Effect.runForkWithcapture, so under test it inheritsTestClockand the escalation is deterministic rather than timing-dependent.Deliberate non-change
The reactor's queue stays serial. Forking the interrupt off the shared lane would let a following
turn-start-requestedrace ahead of a session teardown and prompt into a dying session. Bounding the adapter call shrinks the global block from ~112s to ~5s, which is the win without the race.Scope
Only the Claude adapter changes. It is the only provider with observed hangs. Codex, Cursor, Grok, Gemini and OpenCode are not audited yet.
No client change is needed. Web, desktop and mobile all send the same
thread.turn.interruptrequest throughpackages/client-runtime/src/operations/commands.ts, so all three surfaces are fixed by the server change alone.Verification
vp test run apps/server/src/provider/Layers/ClaudeAdapter.test.ts— 72 passed, including two new tests.tsgo --noEmitonapps/server— 0 errors.vp linton both files — clean (oneno-useless-spreadwarning, pre-existing onmain).Upstream
Upstream knows about this bug and has not shipped a fix. Open issues #4524 and #4713 describe it. Open PR #5891 fixes it by always closing the session and never calling
interrupt()at all, which discards the session even on a Stop that would have worked. This fork keeps the graceful path and escalates only when the agent ignores it. If pingdotgg#5891 merges, expect a conflict in these two files.Written by Claude Opus 5 in T3 Code.