Repository navigation
fix(signals): stop foreground loops when wt itself receives Ctrl-C - #4533
Conversation
Since v0.80.0, a Ctrl-C during a multi-target `wt remove` no longer stops the batch when the pre-remove hook catches SIGINT and exits normally, as pre-commit does (`trap ... INT; exit 130`). The later targets were removed, with their hooks run, after the user pressed Ctrl-C. Two changes combined: #4400 moved foreground children into the caller's native job group and followed the shell's wait-and-cooperative-exit rule (a child that handles the key keeps its own exit status), and #4357 made a multi-target remove continue past a failing target. Worktrunk now follows the batch-runner convention of make, ninja and cargo instead. The terminal delivers Ctrl-C to the whole job, so wt receives the key alongside its child. wt latches that receipt; the child still handles the key its own way and is never signalled for it, but once it exits the command starts nothing more and exits interrupted, whatever status the child chose. Numeric exit codes such as 130 are still never read as cancellation. The latch extends the existing per-operation cancellation: a foreground scope now shares the CommandOperation's SIGINT flag, as it already shared the SIGTERM one, so the scope's own outcome reports cancellation and later admissions in the same command are refused. This covers hook pipelines (including Warn-strategy ones), aliases, for-each, remove and prune. The output drain no longer registers a fresh flag to ignore keys the children already handled. A command's final interactive child owns the key as input instead: help and `config show` pagers, `--execute` programs and custom subcommands opt in through `Cmd::hand_off_terminal` / `TerminalInterrupt::ChildInput`, keeping their own exit status. Detached background hooks, timed captures and Windows are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
Alias steps get the terminal too, but they run as Cancels. run_alias builds its steps with ForegroundStdin::Inherit, and execute_shell_command in src/output/handlers.rs sets .forward_signals(). So an alias that launches an interactive program, such as agent = "claude" or new = "wt switch -c {{ args }} -x claude", ends with the "interrupted" exit (130) whenever the user pressed Ctrl-C inside it, even after quitting normally. That is the case the PR exempts for -x ("pressing Ctrl-C in less or a REPL started by -x, then quitting normally, would turn into exit 130"). With the nested form, the inner wt keeps the child's status and the outer alias still reports interrupted. Ctrl-C is how people stop an agent's current turn, so this is likely to come up.
Stopping a multi-step alias's later steps after a Ctrl-C matches the make/cargo rule. The open question is the last (or only) step: should it be ChildInput, like the final interactive child of other commands? If the current behavior is intended, the AGENTS.md list of final interactive children ("a pager, an --execute program, a custom subcommand") could say that aliases don't count, so the next reader doesn't treat the omission as a bug.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
An alias is the whole command, so its last serial step is the command's final interactive child, like an `--execute` program: `agent = "claude"` should keep claude's exit status after the user stops a turn with Ctrl-C. Earlier steps still cancel the alias on wt's receipt of the key, and concurrent groups (closed stdin) always do. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
What regressed
Since v0.80.0, Ctrl-C during
wt remove a b cno longer stops the batch when the pre-remove hook catches SIGINT and exits normally, as pre-commit does (trap '...' INT; exit 130). Reproduced with a hook that traps INT, exits 130 and otherwise sleeps, plus a process-group SIGINT: v0.80.0 stops ataand keeps every worktree; current main removesbandc, branches included, and runs their hooks.wt step prunedid the same with a hook that exits 0 after catching the key: every candidate was removed and wt exited 0.Two changes combined. #4400 moved foreground children into the caller's native job group and followed the shell's wait-and-cooperative-exit rule, so a child that handles the key keeps its own exit status. #4357 made a multi-target remove continue past a failing target.
The rule
Max chose the batch-runner convention (make, ninja, cargo) over the shell's: Ctrl-C that wt itself receives stops the current command. The terminal delivers SIGINT to the whole foreground job, so wt receives the key alongside the child. A shell waits and exits only if the child died of the signal, because the child might be an interactive program using Ctrl-C as input. A batch runner's steps aren't interactive, and the user pressing Ctrl-C means "stop the batch". For a destructive loop like remove or prune, that rule is also the one the data-safety principle asks for.
The child still handles the key its own way, and wt never signals it for INT. Once the child exits, the command starts nothing more and exits interrupted (native SIGINT re-raise / 130), whatever status the child chose. Numeric exit codes such as 130 are still never read as cancellation; wt's own receipt is the evidence.
What changes
src/signal_forwarder.rs: the existing per-operation cancellation (CommandOperation) gains a SIGINT flag beside its SIGTERM one. Foreground scopes register it, so the scope reports cancellation when its child exits, and later admissions in the same command are refused. The output drain's fresh flag, which existed to ignore keys the children had already handled, is gone.for-each, multi-targetremoveandprune.config showpagers,--executeprograms and customwt-*subcommands opt in throughCmd::hand_off_terminal(TerminalInterrupt::ChildInput). So does an alias's last serial step (ForegroundStdin::HandOff), since the alias is the whole command;agent = "claude"keeps claude's status after the user stops a turn with Ctrl-C. Otherwise pressing Ctrl-C inless, a REPL started by-xor such an alias, then quitting normally, would turn into exit 130. Earlier alias steps, concurrent groups and every hook step still cancel.signal_forwarder/shell_exec/concurrentmodule docs state the new rule.Verification
removewith a hook exiting 130 and with one exiting 0 keeps both targets; a post-start pipeline doesn't run its next step after a step exits 130;for-eachvisits one worktree when the child exits 0. All four fail on the old semantics.test_alias_last_step_keeps_status_after_caught_ctrl_ccovers the alias handoff. The exit-0 remove case also fails if the scope stops reporting the latch and only refuses later admissions, since the current target would still be removed.caught_keyboard_interrupt_cancels_following_step,test_alias_pid_signal_policy(PID-only INT) andtest_alias_group_sigint_does_not_escalate. The children still handle every key exactly once and wt never dies while they run.test_alias_caught_sigint_preserves_successful_descendant_outputis deleted because its premise is the old rule. The pager and custom-subcommand cases incustom_commands_and_help_pagers_preserve_native_signalspass unchanged.tests/commonasSignalJobfor reuse.cargo nextest run --all-features(5377 passed),cargo clippy --all-targets --all-features -- -D warnings, pre-commit on the changed files. Manual repro of remove and prune before and after.