Skip to content

fix(acp): cancel running turns before killing agents on shutdown - #5812

Merged
SawyerHood merged 1 commit into
mainfrom
acp-fu-1-shutdown-cancel
Oct 20, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
acp-fu-1-shutdown-cancel

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

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 stop doesn't leave them, because it sends session/cancel first 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 120 running under systemd.

What changed

  • packages/provider-bridge-acp/src/bridge/bridge.ts: shutdown stops each session through stopSession with 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 one allSettled killed 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 on session/prompt and kills it on session/cancel; the test asserts the child is gone once the bridge exits.

How you verified

  • The three new cases (SIGTERM, SIGINT, stdin) fail before the change with "the agent's command outlived the bridge". They pass after it, as do the six existing reap cases.
  • pnpm exec turbo run test lint typecheck --filter=@bb/provider-bridge-acp: 508 passed, 4 skipped.
  • Live, on an isolated start:worktree build with real cursor-agent 2026.08.11 and opencode 1.18.35: both threads ran sleep 120, then bb got Ctrl-C (SIGINT to its process group). Before the fix, both sleeps survived. After it, ports are free and no sleep, cursor-agent acp or opencode acp process is left.

AGENT GENERATED

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.
@SawyerHood
SawyerHood added this pull request to stack #5818 October 20, 2026 00:50
@SawyerHood
SawyerHood merged commit 8f2af05 into main Oct 20, 2026
47 checks passed
@SawyerHood
SawyerHood deleted the acp-fu-1-shutdown-cancel branch October 20, 2026 15:37
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.

2 participants