From fe005eeca64e9e51c504da70520212719b6f75f9 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Sat, 28 Mar 2026 23:31:53 +0000 Subject: [PATCH] =?UTF-8?q?phase:=20graceful=20process=20termination=20(SI?= =?UTF-8?q?GTERM=20=E2=86=92=20SIGKILL)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit kill_process_tree() now sends SIGTERM first, waits 10s grace period, then escalates to SIGKILL. This lets child process managers (like nextest) clean up their own children in separate process groups before being force-killed. Previously, immediate SIGKILL to the process group left grandchild processes in their own process groups orphaned as PID 1 children. --- .../bough-core/src/bin/bough-test-helper.rs | 43 ++++++++++++- crates/bough-core/src/phase.rs | 63 +++++++++++++++++-- 2 files changed, 101 insertions(+), 5 deletions(-) diff --git a/crates/bough-core/src/bin/bough-test-helper.rs b/crates/bough-core/src/bin/bough-test-helper.rs index 9618813e..80486941 100644 --- a/crates/bough-core/src/bin/bough-test-helper.rs +++ b/crates/bough-core/src/bin/bough-test-helper.rs @@ -8,9 +8,10 @@ fn main() { 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)), + Some("spawn-own-pgroup") => spawn_own_pgroup(&args[1..]), _ => { eprintln!( - "usage: bough-test-helper [args...]" + "usage: bough-test-helper [args...]" ); std::process::exit(1); } @@ -52,6 +53,46 @@ fn spawn_chain(args: &[String]) { std::thread::sleep(std::time::Duration::from_secs(1000)); } +fn spawn_own_pgroup(args: &[String]) { + use std::os::unix::process::CommandExt; + use std::sync::atomic::Ordering; + + let pid_dir = args.first().expect("spawn-own-pgroup requires a pid-dir path"); + let pid_dir = std::path::Path::new(pid_dir); + + std::fs::write(pid_dir.join("parent.pid"), std::process::id().to_string()).unwrap(); + + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["sleep"]) + .process_group(0) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + .expect("spawn child in own pgroup"); + + let child_pid = child.id() as i32; + std::fs::write(pid_dir.join("child.pid"), child_pid.to_string()).unwrap(); + + CHILD_PID_FOR_HANDLER.store(child_pid, Ordering::SeqCst); + + unsafe { + libc::signal(libc::SIGTERM, handle_sigterm as *const () as libc::sighandler_t); + } + + let _ = child.wait(); + std::thread::sleep(std::time::Duration::from_secs(1000)); +} + +static CHILD_PID_FOR_HANDLER: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0); + +extern "C" fn handle_sigterm(_sig: libc::c_int) { + let child_pid = CHILD_PID_FOR_HANDLER.load(std::sync::atomic::Ordering::SeqCst); + if child_pid > 0 { + unsafe { libc::kill(-child_pid, libc::SIGKILL); } + } + unsafe { libc::_exit(0); } +} + 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 4839d49d..79ccd0c9 100644 --- a/crates/bough-core/src/phase.rs +++ b/crates/bough-core/src/phase.rs @@ -42,13 +42,22 @@ impl From for Error { } } -fn kill_process_tree(child: &std::process::Child) { +const GRACEFUL_SHUTDOWN_PERIOD: Duration = Duration::from_secs(10); + +fn kill_process_tree(child: &mut std::process::Child) { #[cfg(unix)] { + use wait_timeout::ChildExt; let pid = child.id() as i32; - unsafe { - libc::kill(-pid, libc::SIGKILL); + + unsafe { libc::kill(-pid, libc::SIGTERM); } + + match child.wait_timeout(GRACEFUL_SHUTDOWN_PERIOD) { + Ok(Some(_)) => return, + _ => {} } + + unsafe { libc::kill(-pid, libc::SIGKILL); } } #[cfg(not(unix))] { @@ -182,7 +191,7 @@ impl<'a, R: Root> Phase<'a, R> { match child.wait_timeout(timeout)? { Some(_status) => false, None => { - kill_process_tree(&child); + kill_process_tree(&mut child); child.wait()?; true } @@ -650,6 +659,52 @@ mod tests { } } + #[cfg(unix)] + #[test] + fn phase_run_timeout_gracefully_terminates_nested_process_groups() { + 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-own-pgroup".into(), + pid_dir.to_str().unwrap().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(500)); + + let parent_pid: i32 = std::fs::read_to_string(pid_dir.join("parent.pid")) + .expect("parent pid file") + .trim() + .parse() + .expect("parse parent pid"); + let child_pid: i32 = std::fs::read_to_string(pid_dir.join("child.pid")) + .expect("child pid file") + .trim() + .parse() + .expect("parse child pid"); + + let parent_alive = unsafe { libc::kill(parent_pid, 0) } == 0; + let child_alive = unsafe { libc::kill(child_pid, 0) } == 0; + + assert!( + !parent_alive, + "parent (pid {parent_pid}) should have been killed but is still running" + ); + assert!( + !child_alive, + "child in own pgroup (pid {child_pid}) should have been killed but is still running" + ); + } + #[cfg(unix)] #[test] fn phase_run_timeout_kills_grandchild_processes() { -- knotmirror 0.0.1