From 42ce8dd02c59d96226e8a3adf8657c29bdb11eb8 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Fri, 27 Mar 2026 16:50:58 +0000 Subject: [PATCH] Add graceful Ctrl-C interrupt handling and process tree tests --- Cargo.lock | 66 +++++++++++++++++++ crates/bough-cli/Cargo.toml | 1 + crates/bough-cli/src/run.rs | 15 +++++ .../bough-core/src/bin/bough-test-helper.rs | 23 ++++++- crates/bough-core/src/phase.rs | 36 ++++++++++ 5 files changed, 140 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9290b9ac..291bf57f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -332,6 +332,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" +dependencies = [ + "objc2", +] + [[package]] name = "bough-cli" version = "0.0.2" @@ -339,6 +348,7 @@ dependencies = [ "bough-core", "bough-typed-hash", "chrono", + "ctrlc", "facet", "facet-json", "facet-toml", @@ -478,6 +488,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "chacha20" version = "0.10.0" @@ -600,6 +616,17 @@ dependencies = [ "typenum", ] +[[package]] +name = "ctrlc" +version = "3.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0b1fab2ae45819af2d0731d60f2afe17227ebb1a1538a236da84c93e9a60162" +dependencies = [ + "dispatch2", + "nix", + "windows-sys 0.61.2", +] + [[package]] name = "deunicode" version = "1.6.2" @@ -616,6 +643,18 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dispatch2" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38" +dependencies = [ + "bitflags 2.11.0", + "block2", + "libc", + "objc2", +] + [[package]] name = "dlmalloc" version = "0.2.13" @@ -1327,6 +1366,18 @@ version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc0287524726960e07b119cebd01678f852f147742ae0d925e6a520dca956126" +[[package]] +name = "nix" +version = "0.31.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d6d0705320c1e6ba1d912b5e37cf18071b6c2e9b7fa8215a1e8a7651966f5d3" +dependencies = [ + "bitflags 2.11.0", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "notify" version = "8.2.0" @@ -1390,6 +1441,21 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" +[[package]] +name = "objc2" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a12a8ed07aefc768292f076dc3ac8c48f3781c8f2d5851dd3d98950e8c5a89f" +dependencies = [ + "objc2-encode", +] + +[[package]] +name = "objc2-encode" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" + [[package]] name = "object" version = "0.37.3" diff --git a/crates/bough-cli/Cargo.toml b/crates/bough-cli/Cargo.toml index e3bcf3af..0816a939 100644 --- a/crates/bough-cli/Cargo.toml +++ b/crates/bough-cli/Cargo.toml @@ -25,6 +25,7 @@ tracing = "0.1" tracing-subscriber = "0.3" indicatif = "0.17" chrono = "0.4.44" +ctrlc = "3" [dev-dependencies] serial_test = "3" diff --git a/crates/bough-cli/src/run.rs b/crates/bough-cli/src/run.rs index 629dacd8..461ec2e2 100644 --- a/crates/bough-cli/src/run.rs +++ b/crates/bough-cli/src/run.rs @@ -100,6 +100,16 @@ impl Run { let done = Arc::new(AtomicBool::new(false)); + // https://github.com/CodeEnPlace/bough/issues/40 — escalating signal handling + let done_for_handler = done.clone(); + ctrlc::set_handler(move || { + done_for_handler.store(true, Ordering::SeqCst); + eprintln!("\nInterrupted. Waiting for in-flight tests to finish..."); + eprintln!("Repeated Ctrl-C will NOT force-kill — this avoids orphaning test processes."); + eprintln!("If you must kill bough, send SIGKILL and clean up child processes manually."); + }) + .expect("failed to set ctrl-c handler"); + let interactive = std::io::stdout().is_terminal(); let pb = if interactive { let pb = indicatif::ProgressBar::new(total); @@ -164,6 +174,11 @@ impl Run { } loop { + if done.load(Ordering::Relaxed) { + info!(%workspace_id, "shutting down worker"); + break; + } + let Ok(mut guard) = session.lock() else { error!(%workspace_id, "mutex poisoned in worker"); break; diff --git a/crates/bough-core/src/bin/bough-test-helper.rs b/crates/bough-core/src/bin/bough-test-helper.rs index 05bc2970..9618813e 100644 --- a/crates/bough-core/src/bin/bough-test-helper.rs +++ b/crates/bough-core/src/bin/bough-test-helper.rs @@ -6,10 +6,11 @@ fn main() { Some("flood-stdout") => flood(std::io::stdout(), &args[1..]), Some("flood-stderr") => flood(std::io::stderr(), &args[1..]), Some("spawn-and-wait") => spawn_and_wait(&args[1..]), + Some("spawn-chain") => spawn_chain(&args[1..]), Some("sleep") => std::thread::sleep(std::time::Duration::from_secs(1000)), _ => { eprintln!( - "usage: bough-test-helper [args...]" + "usage: bough-test-helper [args...]" ); std::process::exit(1); } @@ -31,6 +32,26 @@ fn flood(mut w: impl Write, args: &[String]) { w.flush().unwrap(); } +fn spawn_chain(args: &[String]) { + let pid_dir = args.first().expect("spawn-chain requires a pid-dir path"); + let depth: usize = args + .get(1) + .and_then(|s| s.parse().ok()) + .expect("spawn-chain requires a depth"); + + let pid_file = std::path::Path::new(pid_dir).join(format!("depth-{depth}.pid")); + std::fs::write(&pid_file, std::process::id().to_string()).unwrap(); + + if depth > 0 { + let _child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["spawn-chain", pid_dir, &(depth - 1).to_string()]) + .spawn() + .expect("spawn child"); + } + + std::thread::sleep(std::time::Duration::from_secs(1000)); +} + fn spawn_and_wait(args: &[String]) { let pid_file = args .first() diff --git a/crates/bough-core/src/phase.rs b/crates/bough-core/src/phase.rs index 508d6223..86a2073e 100644 --- a/crates/bough-core/src/phase.rs +++ b/crates/bough-core/src/phase.rs @@ -612,6 +612,42 @@ mod tests { ); } + #[cfg(unix)] + #[test] + fn phase_run_timeout_kills_deep_process_tree() { + let dir = tempfile::tempdir().unwrap(); + let pid_dir = dir.path().join("pids"); + std::fs::create_dir_all(&pid_dir).unwrap(); + let root = TestRoot(dir.path().to_path_buf()); + let phase = Phase { + cmd: vec![ + helper_bin().to_str().unwrap().into(), + "spawn-chain".into(), + pid_dir.to_str().unwrap().into(), + "3".into(), + ], + pwd: crate::file::Twig::new(PathBuf::from(".")).unwrap(), + timeout: Some(Duration::from_millis(500)), + ..make_phase(&root) + }; + let outcome = phase.run().unwrap(); + assert!(matches!(outcome, PhaseOutcome::TimedOut { .. })); + + std::thread::sleep(Duration::from_millis(200)); + + for depth in 0..=3 { + let pid_file = pid_dir.join(format!("depth-{depth}.pid")); + let pid_str = std::fs::read_to_string(&pid_file) + .unwrap_or_else(|_| panic!("missing pid file for depth {depth}")); + let pid: i32 = pid_str.trim().parse().expect("parse pid"); + let alive = unsafe { libc::kill(pid, 0) } == 0; + assert!( + !alive, + "process at depth {depth} (pid {pid}) should have been killed but is still running" + ); + } + } + #[cfg(unix)] #[test] fn phase_run_timeout_kills_grandchild_processes() { -- knotmirror 0.0.1