Skip to content

fix(concurrent): report originating signal as wt's exit code after escalation - #2724

Merged
max-sixty merged 3 commits into
mainfrom
diagnose-affected-tests-advisory
May 11, 2026
Merged

max-sixty merged 3 commits into
mainfrom
diagnose-affected-tests-advisory

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

wt step <concurrent-alias> interrupted by Ctrl-C could exit with code 143 (SIGTERM) instead of 130 (SIGINT) under load. The race lives in src/output/concurrent.rs's signal forwarder: it sends SIGINT to each child's process group, then waits 200ms before escalating to SIGTERM. Under CI load the path signal_hook polls → killpg → child receives → forwards to sleep → sleep dies → sh dies → pgroup empty exceeds 200ms, so wt escalates and the child dies from SIGTERM. Wt's interrupt_exit_code() reads the child's actual death signal and reports 128 + 15 = 143 even though the user only ever sent SIGINT.

This was the cause of the affected tests (linux, advisory) failure on #2709 (selected test_alias_concurrent_receives_sigint because that PR touched src/main.rs, dragging in coverage of nearly every wt invocation path) — diagnosis recorded there, not a regression from the approval refactor.

Fix

Record the user's first signal in a shared Arc<AtomicI32> set by the signal forwarder thread. After every child has been waited on, override any per-child ChildProcessExited::signal whose value differs from the originating one. wt's exit code now reflects what the user pressed (130 for SIGINT) regardless of which signal the escalation chain ended up using to actually kill the child.

The fix is contained to run_concurrent_commands. The single-step shell_exec.rs::stream path has the same escalation logic but isn't exercised by the failing test; if the equivalent flake shows up there, the same pattern can be applied.

Test plan

  • New test_alias_concurrent_sigint_reports_origin_after_escalation (tests/integration_tests/step_alias.rs:1406) — children trap "" INT so SIGINT is ignored and only the SIGTERM escalation kills them. Verified to reproduce the exact CI failure mode (unix_wait_status(36608) = exit 143) without the fix and pass with it.
  • Existing test_alias_concurrent_receives_sigint and test_alias_concurrent_second_sigint_kills still pass.
  • cargo run -- hook pre-merge --yes: 3667 tests pass, lints clean.

…calation

`wt step <concurrent-alias>` interrupted by Ctrl-C could exit with code 143
(SIGTERM) instead of 130 (SIGINT) under load. The signal forwarder in
`run_concurrent_commands` sends SIGINT to each child's process group, then
waits 200ms before escalating to SIGTERM. Under CI load the propagation chain
(signal_hook polls → killpg → child receives → sh forwards to sleep → sleep
dies → sh dies → pgroup empty) exceeds 200ms, so wt escalates and the child
dies from SIGTERM. `interrupt_exit_code()` reads the child's actual death
signal and reports `128 + 15 = 143` even though the user only ever sent
SIGINT.

Record the user's first signal in a shared `Arc<AtomicI32>` set by the
forwarder thread. After every child has been waited on, override any per-
child `ChildProcessExited::signal` whose value differs from the originating
one. wt's exit code now reflects what the user pressed (130 for SIGINT)
regardless of which signal the escalation chain used to actually kill the
child.

Fix is contained to `run_concurrent_commands`. The single-step
`shell_exec.rs::stream` path has the same escalation logic but isn't
exercised by the failing test; if the equivalent flake shows up there, the
same pattern can be applied.

Adds a regression test where children trap SIGINT so only the SIGTERM
escalation kills them — without the fix it reproduces the exact CI failure
mode (`unix_wait_status(36608)` = exit 143).

Co-Authored-By: Claude <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes May 11, 2026

@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.

codecov/patch is failing on a single uncovered line: src/output/concurrent.rs:207 — the return; in override_with_originating_signal's let-else that fires when an outcome is Err but not a signal-derived ChildProcessExited. None of the new tests reach that branch (all the SIGINT-traps children exit via signal). A tiny unit test on the private helper closes the gap; happy to push if you want.

@worktrunk-bot
worktrunk-bot dismissed their stale review May 11, 2026 22:26

codecov/patch failed — src/output/concurrent.rs:207 uncovered. Posted analysis in the follow-up review.

The four-way table covers `Ok`, non-`WorktrunkError` `Err`, exit-code
`ChildProcessExited` (no signal), same-signal, and different-signal cases.
Closes the codecov/patch gap on the early-out arm that the integration
regression test doesn't reach.

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 1fa70d3 into main May 11, 2026
31 checks passed
@max-sixty
max-sixty deleted the diagnose-affected-tests-advisory branch May 11, 2026 22:46
max-sixty added a commit that referenced this pull request May 12, 2026
…2734)

## Summary

`shell_exec.rs::stream` (single child) and `output/concurrent.rs` (N
children) each carried their own copy of the same pattern — install a
`signal_hook` SIGINT/SIGTERM handler before spawning, run a listener
thread that records the user's first signal and forwards it (with
graceful escalation) to the child or child-groups, then close the handle
and join the thread on shutdown, reporting the *originating* signal so
wt's exit code reflects what the user pressed even when escalation
killed the child with a later signal. Follow-up to #2724, which added
the originating-signal tracking to `concurrent.rs` — at that point both
sites had the same logic, just duplicated.

## Changes

New `src/signal_forwarder.rs`:

- `ForegroundSignals::install()` — register wt's handler *before*
spawning children (a signal arriving mid-spawn is queued, not
default-killing wt and orphaning already-spawned children).
- `forward_to_pid(pid, share_parent_pgroup)` — single child.
Shared-pgroup mode delivers a single shot by PID (the kernel already
broadcast the tty signal to the child; explicit forwarding only matters
for externally-delivered signals like `kill -TERM <wt-pid>`, and
skipping escalation avoids wedging an interactive child
mid-tty-restore). Otherwise per-PID escalation SIGINT → SIGTERM →
SIGKILL.
- `forward_to_pgids(pgids)` — N children, per-PGID escalation, plus a
second-signal SIGKILL-all impatient path (per-PGID escalation is
serialized, so a user mashing Ctrl-C would otherwise wait N × 400 ms for
the chain to walk).
- `ActiveForwarder::stop()` — tear down and return the originating
signal.

A private `run_listener` holds the shared scaffolding (own the handle,
spawn the listener thread, package into `ActiveForwarder`) so each
`forward_to_*` body is just its policy.

Both call sites drop ~30 lines of plumbing each: `concurrent.rs` deletes
its `SignalForwarder` struct + `spawn_signal_forwarder`;
`shell_exec.rs::stream` deletes its inline ~60-line listener block.
`shell_exec::forward_signal_to_pid` is now `pub` so the new module can
call it.

No behavior change. `concurrent.rs` keeps its per-outcome
originating-signal override — that's the multi-child outcome shape, not
signal forwarding.

## Test plan

- `cargo run -- hook pre-merge --yes`: 3669 tests pass, lints clean.
- The alias signal tests (`test_alias_concurrent_receives_sigint`,
`test_alias_concurrent_sigint_reports_origin_after_escalation`,
`test_alias_concurrent_second_sigint_kills`,
`test_alias_external_sigint_reaches_child`) all pass against the unified
module.

---------

Co-authored-by: Claude <[email protected]>
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