Repository navigation
fix(acp): cancel running turns before killing agents on shutdown - #5812
Merged
Merged
Conversation
On stdin close, SIGTERM or SIGINT the bridge released every session and killed every agent at once. ACP agents run their tool commands in process groups of their own, so a command running at that moment (a dev server, a long sleep) outlived bb, re-parented to init. A normal `thread stop` does not leave them because it sends `session/cancel` first and the agent ends its commands. Shutdown now stops each session through the same path with a 1.5 s cancel window (all sessions in parallel, inside the runtime's 5 s escalation), kills connections that belong to no session (discovery, sessions still starting) at once, and then kills whatever is left. The new shutdown case drives a fake agent that starts a detached child per prompt and ends it on cancel; it fails before this change on SIGTERM, SIGINT and stdin close.
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.
Human comments
What was wrong
When bb shut a provider worker down (stdin close, SIGTERM or SIGINT), the ACP bridge released every session and killed every agent at once. ACP agents run their tool commands in process groups of their own, so a command running at that moment outlived bb, re-parented to init. A normal
thread stopdoesn't leave them, because it sendssession/cancelfirst and the agent ends its own commands. #5374 added the shutdown hooks but deliberately skipped the cancel, to stay inside the runtime's 5 s escalation window.Found while live-testing #5775: Ctrl-C of bb mid-turn left Cursor's and OpenCode's
sleep 120running under systemd.What changed
packages/provider-bridge-acp/src/bridge/bridge.ts: shutdown stops each session throughstopSessionwith a 1.5 s cancel window (SHUTDOWN_CANCEL_TIMEOUT_MS). All sessions are stopped in parallel, so total shutdown stays well inside 5 s. Connections owned by no session (model/option discovery, sessions still starting) are killed straight away, and anything left is killed afterwards. Previously oneallSettledkilled every connection in parallel with releasing sessions, so the agent died before it could handle a cancel.shutdown.test.ts: new case per shutdown path. A fake agent starts a detached child onsession/promptand kills it onsession/cancel; the test asserts the child is gone once the bridge exits.How you verified
pnpm exec turbo run test lint typecheck --filter=@bb/provider-bridge-acp: 508 passed, 4 skipped.start:worktreebuild with real cursor-agent 2026.08.11 and opencode 1.18.35: both threads ransleep 120, then bb got Ctrl-C (SIGINT to its process group). Before the fix, both sleeps survived. After it, ports are free and nosleep,cursor-agent acporopencode acpprocess is left.