Skip to content

fix(agent): make Ctrl-C always exit the agent - #567

Merged
huichen merged 2 commits into
mainfrom
claude/agent-ctrl-c
Sep 12, 2026
Merged

huichen merged 2 commits into
mainfrom
claude/agent-ctrl-c

Conversation

@huichen

@huichen huichen commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

Problem

future agent --verbose sometimes ignores Ctrl-C: the console keeps sitting there and further presses do nothing, so the only way out is closing the terminal.

Cause

The SIGINT handler was registered on the agent's own Tokio runtime (tokio::signal::ctrl_c() inside tokio::select! in async_main). The first Ctrl-C did run — it set shutting_down and aborted live sessions — but the process then stopped making progress exactly where blocking work piles up:

  • Runtime::drop waits indefinitely for blocking tasks (spawn_blocking) to finish: sandbox process waits (WaitForSingleObject), session/index persistence, image decoding for vision requests.
  • After the runtime is gone, run() writes the profiler flamegraph and cleanup_windows_sandbox_on_exit() revokes Windows sandbox ACEs — still no signal handler alive.

At that point the handler is already consumed, so the second (and third) Ctrl-C is dropped. --verbose makes it much more likely because longer, tool-heavy sessions leave more sandbox leases and blocking I/O in flight at shutdown.

Fix

Signal handling moves out of the agent runtime into a dedicated thread with its own current-thread runtime, started before the agent runtime and outliving it (agent/src/cli/shutdown.rs):

  • 1st Ctrl-C → forwarded over a oneshot to the server: unchanged graceful shutdown. A registration failure is reported as a normal startup error instead of silently pretending Ctrl-C was pressed.
  • 2nd Ctrl-C → immediate forced exit.
  • 5s grace deadline after the forwarded interrupt → forced exit (status 130) even if nobody presses again.

Forced exit deliberately skips destructors only after shutdown has already stalled: the Windows Job handles close with the process, the singleton lock is released by the OS, and stale ACEs are recovered by cleanup_windows_sandbox_on_startup() (the documented crash-recovery path). Normal shutdowns still destroy the guard, cancel the deadline and join the signal thread, so they are not delayed.

hydrate_from_login_shell() moves ahead of the signal thread — the set_var calls must still run before any other thread exists (this was the only other single-threaded-only code on the startup path; it is not reached by the sessionless --probe-* / --migrate-sessions maintenance paths).

Verification

  • New agent/src/cli/shutdown.rs unit tests (paused clock): no deadline while idle, first interrupt → graceful request + deadline, second interrupt → immediate force, deadline applies even when the server already dropped the receiver, listener failure → no force.
  • New stalled_shutdown_subprocess test runs a real stalled Runtime::drop, a synchronous cleanup lock, and the second-interrupt path in a child process and asserts exit code 130 — the watchdog thread itself is what kills the child.
  • Existing agent_shuts_down_cleanly_on_sigint (Linux CI) now exercises the new wiring end to end: SIGINT → watchdog → graceful exit 0.
  • cargo fmt -p future-agent --check, cargo clippy -p future-agent --all-targets -- -D warnings clean; cargo test -p future-agent shows the same failure set as unmodified main on this Windows host (verified by stashing the change and diffing the lists — the 31 lib + 2 cli_smoke failures are pre-existing Windows-only expectations: /-vs-\ path literals and pprof being Unix-only). CI runs Linux, where those pass.

The SIGINT handler lived on the agent's own Tokio runtime. The first
Ctrl-C aborted active sessions but then left the process stuck exactly
where blocking work can pile up: Tokio's runtime drop waits
indefinitely for blocking tasks (sandbox process waits, session and
index writes, image encoding), and the profiler plus Windows ACE
cleanup run after the runtime — at which point the handler is already
consumed, so further Ctrl-C presses were dropped and the console was
held hostage.

Run signal handling on a dedicated thread with its own runtime, started
before the agent runtime and outliving it. The first Ctrl-C is
forwarded to the server for the normal graceful shutdown; a second
Ctrl-C, or a 5s grace deadline, force-exits (status 130) so no stalled
blocking task can hang the process. Environment hydration moves in
front of that thread so it still happens while single-threaded.

Covered by a child-process test that stalls a real Tokio runtime drop
and a synchronous cleanup lock, in-process tests for the
second-interrupt and deadline paths, and the existing Linux SIGINT
smoke test, which now exercises the new wiring end to end.
@huichen
huichen enabled auto-merge (squash) September 12, 2026 05:17
@huichen
huichen merged commit c5e87ee into main Sep 12, 2026
12 checks passed
@huichen
huichen deleted the claude/agent-ctrl-c branch September 12, 2026 05:23
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