Skip to content

refactor(signal): unify foreground signal forwarding into one module - #2734

Merged
max-sixty merged 2 commits into
mainfrom
diagnose-affected-tests-advisory
May 12, 2026
Merged

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

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

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.

`shell_exec.rs::stream` (single child) and `output/concurrent.rs` (N
children) each had 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 in an `Arc<AtomicI32>` 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.

Extract this into `src/signal_forwarder.rs`:

- `ForegroundSignals::install()` — register the handler before spawn
  (queues a signal arriving mid-spawn rather than default-killing wt).
- `forward_to_pid(pid, share_parent_pgroup)` — single child; PID-only
  single-shot delivery in shared-pgroup mode (kernel already broadcast
  the tty signal; explicit forward only covers `kill -TERM <wt-pid>`,
  and skipping escalation avoids wedging an interactive child mid-tty-
  restore), per-PID escalation otherwise.
- `forward_to_pgids(pgids)` — N children, per-PGID escalation, with a
  second-signal SIGKILL-all impatient path since per-PGID escalation is
  serialized.
- `ActiveForwarder::stop()` — tear down and return the originating signal.

Both call sites drop ~30 lines of plumbing each; `run_listener` holds the
shared scaffolding so each `forward_to_*` body is just its policy. Made
`shell_exec::forward_signal_to_pid` public so the new module can call it.
No behavior change — `concurrent.rs` keeps its per-outcome originating-
signal override (that's the multi-child shape, not signal forwarding).

Co-Authored-By: Claude <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes May 12, 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.

Dismissing my approval — the test (windows) job is failing on this PR with a real build break:

error: unresolved link to `ForegroundSignals::install`
  --> src\signal_forwarder.rs:18:10
   |
18 | //! 1. [`ForegroundSignals::install`] — call before spawning. Queues any
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^ no item named `ForegroundSignals` in scope

(plus four more for ForegroundSignals::forward_to_pid, forward_to_pgids, and ActiveForwarder::stop)

Every item in signal_forwarder.rs is #[cfg(unix)]-gated, but the module-level //! doc isn't — on Windows the module ends up empty and rustdoc can't resolve the intra-doc links, which -D rustdoc::broken-intra-doc-links then promotes to a fatal cargo doc error.

Cleanest fix is to gate the whole module at the lib.rs declaration, since both call sites (shell_exec.rs:1249, output/concurrent.rs:52) already use it only under #[cfg(unix)]. Inline suggestion below. Happy to push the fix if you'd prefer.

Comment thread src/lib.rs Resolved
@worktrunk-bot
worktrunk-bot dismissed their stale review May 12, 2026 01:47

CI failed on Windows — broken intra-doc-links from cfg(unix)-gated items referenced in non-gated module docstring. See follow-up review.

The module-level docstring links to `ForegroundSignals` / `ActiveForwarder`,
which were `#[cfg(unix)]` while the module itself wasn't — so on Windows the
types don't exist and `RUSTDOCFLAGS=-Dwarnings cargo doc` failed on the
broken intra-doc links. The module is unix-only anyway (no POSIX signals or
process groups on Windows), so gate it at the `pub mod` declaration and drop
the now-redundant inner `#[cfg(unix)]` attributes. Both call sites already
reference it only under `#[cfg(unix)]`.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 89d684f into main May 12, 2026
33 checks passed
@max-sixty
max-sixty deleted the diagnose-affected-tests-advisory branch May 12, 2026 02:02
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