Skip to content

fix(signals): stop foreground loops when wt itself receives Ctrl-C - #4533

Merged
max-sixty merged 4 commits into
mainfrom
fix/ctrl-c-stops-foreground-loops
Oct 20, 2026
Merged

max-sixty merged 4 commits into
mainfrom
fix/ctrl-c-stops-foreground-loops

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

What regressed

Since v0.80.0, Ctrl-C during wt remove a b c no 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 at a and keeps every worktree; current main removes b and c, branches included, and runs their hooks. wt step prune did 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.
  • Covered loops: hook pipelines (including Warn-strategy post-* hooks), multi-step aliases, for-each, multi-target remove and prune.
  • A command's final interactive child keeps the key as input and its own exit status: help and config show pagers, --execute programs and custom wt-* subcommands opt in through Cmd::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 in less, a REPL started by -x or such an alias, then quitting normally, would turn into exit 130. Earlier alias steps, concurrent groups and every hook step still cancel.
  • Unchanged: between children, idle SIGINT still kills wt natively. Detached background hooks, timeout-bounded captures in their own process group and the picker hold no foreground scope. A SIGINT sent only to wt is still not forwarded to the child; wt waits for it to finish, then stops. Windows is unchanged: wt installs no console Ctrl-C handler, so the default handler already ends wt and nothing more starts.
  • AGENTS.md "Signal Handling" and the signal_forwarder / shell_exec / concurrent module docs state the new rule.

Verification

  • New unix integration tests, each pressing Ctrl-C (a group SIGINT) after wt has admitted a child that catches it: remove with 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-each visits one worktree when the child exits 0. All four fail on the old semantics. test_alias_last_step_keeps_status_after_caught_ctrl_c covers 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.
  • Existing tests that asserted the old rule now assert cancellation: the PTY caught_keyboard_interrupt_cancels_following_step, test_alias_pid_signal_policy (PID-only INT) and test_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_output is deleted because its premise is the old rule. The pager and custom-subcommand cases in custom_commands_and_help_pagers_preserve_native_signals pass unchanged.
  • The alias signal harness moved to tests/common as SignalJob for 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.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 2 commits October 20, 2026 11:33
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]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

max-sixty and others added 2 commits October 20, 2026 11:57
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]>
@max-sixty
max-sixty merged commit 0559edc into main Oct 20, 2026
38 checks passed
@max-sixty
max-sixty deleted the fix/ctrl-c-stops-foreground-loops branch October 20, 2026 19:32
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