Craft Anything: understand how any software works, and craft your own.
Rust
Stop watched launches with their process trees (#1027)
Tomáš Doubek50829156e202
diff --git a/crates/platform/src/process.rs b/crates/platform/src/process.rsindex 8c06a299..32420758 100644--- a/crates/platform/src/process.rs+++ b/crates/platform/src/process.rs@@ -980,7 +980,17 @@ impl Launch {const CREATE_NO_WINDOW: u32 = 0x0800_0000;cmd.creation_flags(CREATE_NO_WINDOW);}- let child = spawn_retrying(|| cmd.spawn()).map_err(|e| spawn_error(&name, &self.program, self.cwd.as_deref(), e, self.remediation.as_deref()))?;+ let mut wrap = process_wrap::std::CommandWrap::from(cmd);+ #[cfg(unix)]+ wrap.wrap(process_wrap::std::ProcessGroup::leader());+ #[cfg(windows)]+ {+ wrap.wrap(process_wrap::std::JobObject);+ if self.hide_console {+ wrap.wrap(process_wrap::std::CreationFlags(windows::Win32::System::Threading::CREATE_NO_WINDOW));+ }+ }+ let child = spawn_retrying(|| wrap.spawn()).map_err(|e| spawn_error(&name, &self.program, self.cwd.as_deref(), e, self.remediation.as_deref()))?;let launched = Launched { pid: child.id(), started: Instant::now(), state: Arc::new(Mutex::new(LaunchState { child: Some(child), ended: None })) };// Reap it when it closes. If no thread can be made the child still runs (and `ended`// stays unknown); on Unix it stays a zombie until we exit, which is harmless.@@ -1012,7 +1022,7 @@ pub struct Launched {#[derive(Debug)]struct LaunchState {- child: Option<std::process::Child>,+ child: Option<Box<dyn process_wrap::std::ChildWrapper>>,ended: Option<Ended>,}@@ -1284,6 +1294,17 @@ mod launch_tests {assert!(l.ended().unwrap().after < Duration::from_secs(20));}+ #[cfg(unix)]+ #[test]+ fn stopping_a_watched_program_stops_its_process_group() {+ let dir = tempfile::tempdir().unwrap();+ let l = shell("sleep 2; echo survived > descendant & wait").cwd(dir.path()).start_watched().unwrap();+ assert!(l.wait_timeout(Duration::from_millis(300)).is_none(), "it should still be running");+ assert!(l.stop());+ std::thread::sleep(Duration::from_secs(3));+ assert!(!dir.path().join("descendant").exists(), "a descendant outlived the watched program");+ }+fn exits_with(code: u8) -> Process {let code = format!("exit {code}");if cfg!(windows) { Process::new("cmd").args(["/C", &code]) } else { Process::new("sh").args(["-c", &code]) }diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex 2a09294e..3ee97787 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -217,6 +217,7 @@ only when it's green and a maintainer has fully reviewed it, code as well as worit can still hang the console host on Windows Server 2022 (only a timeout ends the tree)- [ ] `process::launch_tests::a_watched_program_can_be_stopped` leaves its `sleep 30` (Windows:`ping -n 30`) running, on `main` too: `stop` ends the shell but not what the shell started+ — in PR #1027- [x] `a_helper_is_judged_by_whether_it_runs` (ai crate) sometimes failed when several crates'tests ran at once: a script just written can't start while another thread's child stillholds it open ("Text file busy"). #787 tries again: on Linux 4 of 72 runs failed before