Repository navigation
refactor(signal): unify foreground signal forwarding into one module - #2734
Conversation
`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
left a comment
There was a problem hiding this comment.
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.
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]>
Summary
shell_exec.rs::stream(single child) andoutput/concurrent.rs(N children) each carried their own copy of the same pattern — install asignal_hookSIGINT/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 toconcurrent.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 likekill -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_listenerholds the shared scaffolding (own the handle, spawn the listener thread, package intoActiveForwarder) so eachforward_to_*body is just its policy.Both call sites drop ~30 lines of plumbing each:
concurrent.rsdeletes itsSignalForwarderstruct +spawn_signal_forwarder;shell_exec.rs::streamdeletes its inline ~60-line listener block.shell_exec::forward_signal_to_pidis nowpubso the new module can call it.No behavior change.
concurrent.rskeeps 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.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.