Repository navigation
fix(agent): make Ctrl-C always exit the agent - #567
Merged
Merged
Conversation
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
enabled auto-merge (squash)
September 12, 2026 05:17
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.
Problem
future agent --verbosesometimes 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()insidetokio::select!inasync_main). The first Ctrl-C did run — it setshutting_downand aborted live sessions — but the process then stopped making progress exactly where blocking work piles up:Runtime::dropwaits indefinitely for blocking tasks (spawn_blocking) to finish: sandbox process waits (WaitForSingleObject), session/index persistence, image decoding for vision requests.run()writes the profiler flamegraph andcleanup_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.
--verbosemakes 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):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 — theset_varcalls 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-sessionsmaintenance paths).Verification
agent/src/cli/shutdown.rsunit 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.stalled_shutdown_subprocesstest runs a real stalledRuntime::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.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 warningsclean;cargo test -p future-agentshows the same failure set as unmodifiedmainon this Windows host (verified by stashing the change and diffing the lists — the 31 lib + 2cli_smokefailures are pre-existing Windows-only expectations:/-vs-\path literals and pprof being Unix-only). CI runs Linux, where those pass.