Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(concurrent): drop per-child SIGINT escalation race (#3075)
Maximilian Roos751e419615dd
diff --git a/src/signal_forwarder.rs b/src/signal_forwarder.rsindex 724c4efa0..1e01180cc 100644--- a/src/signal_forwarder.rs+++ b/src/signal_forwarder.rs@@ -42,9 +42,12 @@//! `share_parent_pgroup=false`, the child is in its own pgroup and we//! escalate SIGINT → SIGTERM → SIGKILL with grace windows.//! - **Multi PGID** ([`forward_to_pgids`]) — `wt step <concurrent-alias>`.-//! Always escalates per-pgroup. A second user signal SIGKILLs every-//! still-live pgroup immediately to short-circuit the otherwise-//! serialized N × 400 ms escalation walk.+//! Forwards the user's signal once per pgroup; a second user signal+//! SIGKILLs every still-live pgroup. No per-child escalation —+//! cooperative children die from the user's signal so `wt`'s exit code+//! reflects intent (130 on Ctrl-C, not 143 from a SIGTERM landed during+//! a sub-second escalation grace window under CI scheduling latency).+//! Stubborn children require a second Ctrl-C, matching `make` / `cargo`.//!//! [`forward_to_pid`]: ForegroundSignals::forward_to_pid//!@@ -92,26 +95,24 @@ impl ForegroundSignals {})}- /// Begin forwarding to N children's PGIDs with per-PGID escalation.- /// A second user signal SIGKILLs every still-live PGID immediately —- /// per-PGID escalation is serialized, so without the impatient path a- /// user mashing Ctrl-C might wait `N × 400 ms` for the chain to walk.+ /// Begin forwarding to N children's PGIDs. Forwards the same signal+ /// once on the first press; a second user signal SIGKILLs every+ /// still-live PGID immediately.+ ///+ /// No per-child escalation: cooperative children die from the user's+ /// signal, so `child.wait()` reports a cause of death that matches+ /// intent (e.g. `wt step <alias>` exits 130 on Ctrl-C, not 143 from a+ /// SIGTERM that landed during a 200 ms escalation grace window under+ /// CI scheduling latency). Stubborn children that ignore SIGINT+ /// require a second Ctrl-C — matching `make` / `cargo` behavior.pub fn forward_to_pgids(self, child_pgids: Vec<i32>) -> ActiveForwarder {let mut seen_once = false;self.run_listener(move |sig, originating| {record_originating(originating, sig);- if !seen_once {- seen_once = true;- for &pgid in &child_pgids {- forward_signal_with_escalation(pgid, sig);- }- } else {- for &pgid in &child_pgids {- let _ = nix::sys::signal::killpg(- nix::unistd::Pid::from_raw(pgid),- nix::sys::signal::Signal::SIGKILL,- );- }+ let signal_to_send = pgid_broadcast_signal(sig, seen_once);+ seen_once = true;+ for &pgid in &child_pgids {+ let _ = nix::sys::signal::killpg(nix::unistd::Pid::from_raw(pgid), signal_to_send);}})}@@ -150,6 +151,22 @@ fn record_originating(slot: &AtomicI32, sig: i32) -> bool {.is_ok()}+/// The signal to broadcast to every still-live pgroup on a user signal. The+/// first press forwards the user's own signal so cooperative children die with+/// the cause the user intended; any later press escalates to SIGKILL. The+/// listener registers only SIGINT and SIGTERM, so a non-SIGTERM press is+/// necessarily SIGINT.+fn pgid_broadcast_signal(sig: i32, already_signaled: bool) -> nix::sys::signal::Signal {+ use nix::sys::signal::Signal;+ if already_signaled {+ Signal::SIGKILL+ } else if sig == SIGTERM {+ Signal::SIGTERM+ } else {+ Signal::SIGINT+ }+}+/// Running signal-forwarder. Returned from/// [`ForegroundSignals::forward_to_pid`] / [`ForegroundSignals::forward_to_pgids`]./// Call [`stop`] after every child has been waited on.@@ -175,3 +192,21 @@ impl ActiveForwarder {}}}++#[cfg(test)]+mod tests {+ use super::*;+ use nix::sys::signal::Signal;++ #[test]+ fn first_press_forwards_the_user_signal() {+ assert_eq!(pgid_broadcast_signal(SIGINT, false), Signal::SIGINT);+ assert_eq!(pgid_broadcast_signal(SIGTERM, false), Signal::SIGTERM);+ }++ #[test]+ fn later_press_escalates_to_sigkill() {+ assert_eq!(pgid_broadcast_signal(SIGINT, true), Signal::SIGKILL);+ assert_eq!(pgid_broadcast_signal(SIGTERM, true), Signal::SIGKILL);+ }+}diff --git a/tests/integration_tests/step_alias.rs b/tests/integration_tests/step_alias.rsindex d227910ee..dfbdc1deb 100644--- a/tests/integration_tests/step_alias.rs+++ b/tests/integration_tests/step_alias.rs@@ -1437,23 +1437,28 @@ two = "sh -c 'echo start-two >> slow_two.log; sleep 30; echo done-two >> slow_tw}}-/// When children trap SIGINT, wt's forwarder escalates SIGINT → SIGTERM,-/// and the children actually die from SIGTERM. But the user only sent SIGINT-/// — wt's exit code must reflect that (130), not the escalated signal (143).-/// Otherwise users who Ctrl-C a `wt step <concurrent-alias>` whose children-/// happen to swallow SIGINT (or are slow under load) see "terminated by-/// SIGTERM" / exit 143 even though they pressed Ctrl-C.+/// Children that trap SIGINT survive a single Ctrl-C — wt forwards the+/// user's signal once and waits. Matches `make` / `cargo` behavior:+/// stubborn children need a second Ctrl-C (see+/// `test_alias_concurrent_second_sigint_kills`) to escalate to SIGKILL.+///+/// Counterpart to `test_alias_concurrent_receives_sigint` (cooperative+/// children, single press is sufficient). Together they pin the+/// no-per-child-escalation contract: the previous design escalated+/// SIGINT → SIGTERM → SIGKILL inside one press, which under CI scheduling+/// latency could land SIGTERM on a cooperative-but-slow child and make+/// `wt step <alias>` exit 143 instead of 130 on Ctrl-C.#[rstest]#[cfg(unix)]-fn test_alias_concurrent_sigint_reports_origin_after_escalation(repo: TestRepo) {+fn test_alias_concurrent_sigint_trapped_survives_first_press(repo: TestRepo) {use crate::common::wait_for_file_content;use nix::sys::signal::{Signal, kill};use nix::unistd::Pid;use std::os::unix::process::CommandExt;use std::process::Stdio;- // Children trap SIGINT (ignore it) but NOT SIGTERM — so wt's- // SIGINT → SIGTERM escalation is what actually kills them.+ // Children trap SIGINT (ignore it). With the new no-escalation+ // contract, a single SIGINT should NOT kill them.repo.write_test_config(r#"[aliases.intignored]@@ -1478,14 +1483,26 @@ two = "sh -c 'trap \"\" INT; echo start-two >> ignored_two.log; sleep 30'"kill(Pid::from_raw(-wt_pgid.as_raw()), Signal::SIGINT).expect("failed to send SIGINT to wt's process group");- let status = child.wait().expect("failed to wait for wt");+ // Give the signal a moment to traverse the forwarder. With escalation+ // (the old design), within ~400 ms SIGTERM would have killed both+ // children and wt would be in the process of exiting. With the new+ // contract, both children keep sleeping and wt is still running.+ std::thread::sleep(std::time::Duration::from_millis(500));- use std::os::unix::process::ExitStatusExt;- assert!(- status.signal() == Some(2) || status.code() == Some(130),- "wt should report the originating SIGINT (signal 2 / code 130) even \- when escalation killed children with SIGTERM, got: {status:?}"- );+ match child.try_wait().expect("try_wait failed") {+ None => {+ // Expected: wt is still running, blocked on its sleeping+ // children. Clean up with a second SIGINT (impatient+ // SIGKILL path).+ kill(Pid::from_raw(-wt_pgid.as_raw()), Signal::SIGINT)+ .expect("failed to send second SIGINT");+ let _ = child.wait();+ }+ Some(status) => panic!(+ "wt exited after a single SIGINT against SIGINT-trapping children — \+ per-child escalation appears to have crept back in. status: {status:?}"+ ),+ }}/// A second SIGINT (user mashing Ctrl-C) must escalate to SIGKILL on every