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() {