Craft Anything: understand how any software works, and craft your own.
Rust
Stop programs started alongside a console capture from holding it open (#997)
BFlat73fe0b661b3f
diff --git a/crates/platform/src/capture.rs b/crates/platform/src/capture.rsindex 60f55939..b919ceb9 100644--- a/crates/platform/src/capture.rs+++ b/crates/platform/src/capture.rs@@ -331,6 +331,18 @@ pub fn capture(_spec: Spec, _cancel: Cancel) -> Result<Capture, ToolError> {/// Run a program that crashes without a real console (it asks the console for its size, which/// fails on [`crate::process`]'s pipes) and return what it drew and its exit code. The deadline/// and `cancel` still end it; nothing writes to its input.+/// Ends the process `$Root` and what it started, and nothing else. Windows keeps a dead parent's+/// number on its children and reuses numbers, so `taskkill /T` can end a stranger whose parent's+/// number `$Root` took: here a process counts only if it started after its parent.+#[cfg(windows)]+const END_TREE: &str = "$all = Get-CimInstance Win32_Process -Property ProcessId,ParentProcessId,CreationDate; $tree = @{}; \+ foreach ($p in $all) { if ($p.ProcessId -eq $Root) { $tree[$Root] = $p.CreationDate } }; if ($tree.Count -eq 0) { exit 0 }; \+ do { $more = $false; foreach ($p in $all) { $id = [int]$p.ProcessId; $parent = [int]$p.ParentProcessId; \+ if (-not $tree.ContainsKey($id) -and $tree.ContainsKey($parent) -and $p.CreationDate -ge $tree[$parent]) { $tree[$id] = $p.CreationDate; $more = $true } } } while ($more); \+ Stop-Process -Id @($tree.Keys) -Force -ErrorAction SilentlyContinue; \+ $left = @($tree.Keys | Where-Object { Get-Process -Id $_ -ErrorAction SilentlyContinue }); \+ if ($left.Count -gt 0) { Write-Output ('still running: ' + ($left -join ', ')); exit 1 }";+#[cfg(windows)]pub fn console_text(program: &std::path::Path, args: &[OsString], timeout: Duration, cancel: Option<Cancel>) -> Result<(String, Option<i32>), ToolError> {use portable_pty::{CommandBuilder, PtySize, native_pty_system};@@ -339,12 +351,15 @@ pub fn console_text(program: &std::path::Path, args: &[OsString], timeout: Duratconst ROWS: u16 = 50;const COLS: u16 = 200;let failed = |e: &dyn std::fmt::Display| ToolError::new(ErrorCode::ProgramFailed, format!("{}: {e}", program.display()));- let portable_pty::PtyPair { master, slave } =- native_pty_system().openpty(PtySize { rows: ROWS, cols: COLS, pixel_width: 0, pixel_height: 0 }).map_err(|e| failed(&e))?;- let mut cmd = CommandBuilder::new(program.as_os_str());- cmd.args(args);- let mut child = slave.spawn_command(cmd).map_err(|e| failed(&e))?;- drop(slave);+ let (master, mut child) = {+ // A program another thread started while the console opened would hold it open.+ let _starting = crate::process::STARTING.write().unwrap_or_else(std::sync::PoisonError::into_inner);+ let portable_pty::PtyPair { master, slave } =+ native_pty_system().openpty(PtySize { rows: ROWS, cols: COLS, pixel_width: 0, pixel_height: 0 }).map_err(|e| failed(&e))?;+ let mut cmd = CommandBuilder::new(program.as_os_str());+ cmd.args(args);+ (master, slave.spawn_command(cmd).map_err(|e| failed(&e))?)+ };let mut reader = master.try_clone_reader().map_err(|e| failed(&e))?;let mut writer = master.take_writer().map_err(|e| failed(&e))?;// The console asks where the cursor is and starts nothing until it's told.@@ -384,6 +399,20 @@ pub fn console_text(program: &std::path::Path, args: &[OsString], timeout: Duratstd::thread::sleep(Duration::from_millis(50));};if stopped.is_err() {+ // Windows Server 2022's console host can hang closing a console that programs are still+ // attached to, and they then keep running: end everything the program started first.+ if let Some(pid) = child.process_id() {+ let script = format!("$Root = {pid}; {END_TREE}");+ match crate::process::Process::new("powershell")+ .args(["-NoProfile", "-NonInteractive", "-Command", script.as_str()])+ .timeout(Duration::from_secs(10))+ .run()+ {+ Ok(out) if out.success => {}+ Ok(out) => log::warn!("couldn't end everything {} started: {}", program.display(), out.stdout_text().trim()),+ Err(e) => log::warn!("couldn't end what {} started: {}", program.display(), e.message),+ }+ }let _ = child.kill();} else {// The console sends what the program drew a moment after it exits, and closing it first@@ -418,13 +447,36 @@ pub fn console_text(program: &std::path::Path, args: &[OsString], timeout: Duratmod console_tests {use super::*;+ /// Runs `f` while this process starts other programs, as the app and other tests do. On+ /// Windows Server 2022 a program started while a console opens inherits handles that keep the+ /// console open until the program ends. Six programs at most, ended afterwards.+ fn while_other_programs_start<R>(f: impl FnOnce() -> R) -> R {+ let comspec = std::env::var_os("ComSpec").unwrap_or_else(|| "cmd.exe".into());+ let starter = std::thread::spawn(move || {+ (0..6)+ .filter_map(|_| {+ let p = crate::process::Process::new(&comspec).args(["/c", "ping -n 12 127.0.0.1 >nul"]).timeout(Duration::from_secs(20)).spawn().ok();+ std::thread::sleep(Duration::from_millis(3));+ p+ })+ .collect::<Vec<_>>()+ });+ let r = f();+ let started = starter.join().unwrap_or_default();+ assert_eq!(started.len(), 6, "the other programs didn't start");+ drop(started);+ r+ }+#[test]fn reads_what_the_program_drew_and_stops_at_the_deadline() {let cmd = PathBuf::from(std::env::var_os("ComSpec").unwrap());let echo: Vec<OsString> = ["/c", "echo hello-console & exit 3"].map(OsString::from).to_vec();- let (text, code) = console_text(&cmd, &echo, Duration::from_secs(30), None).unwrap();+ let start = std::time::Instant::now();+ let (text, code) = while_other_programs_start(|| console_text(&cmd, &echo, Duration::from_secs(30), None)).unwrap();assert!(text.contains("hello-console"), "{text:?}");assert_eq!(code, Some(3));+ assert!(start.elapsed() < Duration::from_secs(5), "waited {:?} for a console another program held open", start.elapsed());let hang: Vec<OsString> = ["/c", "ping -n 60 127.0.0.1"].map(OsString::from).to_vec();assert_eq!(console_text(&cmd, &hang, Duration::from_millis(300), None).unwrap_err().code, ErrorCode::TimedOut);}@@ -438,13 +490,15 @@ mod console_tests {std::fs::write(dir.path().join("main.cmd"), "@echo off\r\nstart /b cmd /c \"%~dp0tick.cmd\"\r\nping -n 60 127.0.0.1 >nul\r\n").unwrap();let cmd = PathBuf::from(std::env::var_os("ComSpec").unwrap());let run = vec![OsString::from("/c"), dir.path().join("main.cmd").into_os_string()];- let e = console_text(&cmd, &run, Duration::from_secs(4), None).unwrap_err();- assert_eq!(e.code, ErrorCode::TimedOut);- assert!(ticks.is_file(), "the second program never ran");- std::thread::sleep(Duration::from_secs(2));- let before = std::fs::metadata(&ticks).unwrap().len();- std::thread::sleep(Duration::from_secs(4));- assert_eq!(std::fs::metadata(&ticks).unwrap().len(), before, "the second program is still running");+ while_other_programs_start(|| {+ let e = console_text(&cmd, &run, Duration::from_secs(4), None).unwrap_err();+ assert_eq!(e.code, ErrorCode::TimedOut);+ assert!(ticks.is_file(), "the second program never ran");+ std::thread::sleep(Duration::from_secs(2));+ let before = std::fs::metadata(&ticks).unwrap().len();+ std::thread::sleep(Duration::from_secs(4));+ assert_eq!(std::fs::metadata(&ticks).unwrap().len(), before, "the second program is still running");+ });}}@@ -833,7 +887,11 @@ mod unix {let pty = native_pty_system();let size = PtySize { rows: spec.terminal.rows, cols: spec.terminal.cols, pixel_width: 0, pixel_height: 0 };- let pair = pty.openpty(size).map_err(|e| to_err("opening a pseudo-terminal", e))?;+ let pair = {+ // A program another thread started while the terminal opened would hold it open.+ let _starting = crate::process::STARTING.write().unwrap_or_else(PoisonError::into_inner);+ pty.openpty(size).map_err(|e| to_err("opening a pseudo-terminal", e))?+ };let mut cmd = CommandBuilder::new(&program);for a in &pre_args {@@ -855,7 +913,11 @@ mod unix {cmd.env("TERM", "xterm-256color");let portable_pty::PtyPair { master, slave } = pair;- let mut child = slave.spawn_command(cmd).map_err(|e| to_err("starting the program", e))?;+ let mut child = {+ // Started like any other program, so it can't inherit a terminal another capture is opening.+ let _starting = crate::process::STARTING.read().unwrap_or_else(PoisonError::into_inner);+ slave.spawn_command(cmd).map_err(|e| to_err("starting the program", e))?+ };// Drop the slave so the reader sees EOF when the child and our writer are gone.drop(slave);let leader_pid = child.process_id();diff --git a/crates/platform/src/process.rs b/crates/platform/src/process.rsindex 548358ee..8c06a299 100644--- a/crates/platform/src/process.rs+++ b/crates/platform/src/process.rs@@ -361,19 +361,29 @@ impl Process {}}+/// Programs start under its read side and [`crate::capture`] opens consoles and terminals under its+/// write side: one being opened briefly has handles that a program started at that moment inherits+/// (Windows Server 2022's console; a Unix terminal until it's made close-on-exec), and it then can't+/// close until that program ends. Starts don't wait for each other, and nothing else is taken under it.+pub(crate) static STARTING: std::sync::RwLock<()> = std::sync::RwLock::new(());+/// Start a program, trying again for about a third of a second while its file is busy. A program/// just written (an agent's update, a test's stand-in) can still be open for writing in a child/// another thread is starting, and Linux then refuses to run it ("Text file busy").fn spawn_retrying<T>(mut spawn: impl FnMut() -> std::io::Result<T>) -> std::io::Result<T> {+ let mut start = || {+ let _starting = STARTING.read().unwrap_or_else(PoisonError::into_inner);+ spawn()+ };let mut wait = Duration::from_millis(5);for _ in 0..6 {- match spawn() {+ match start() {Err(e) if e.kind() == std::io::ErrorKind::ExecutableFileBusy => std::thread::sleep(wait),started => return started,}wait = wait.saturating_mul(2);}- spawn()+ start()}/// The child gets [`search_path`](crate::which::search_path), which is also where a bare programdiff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex 48c13097..c9d7adf1 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -190,6 +190,26 @@ only when it's green and a maintainer has fully reviewed it, code as well as worwith the fixture's canonical path, so macOS's `/var` → `/private/var` alias does not failthe test. Remote expansion and special-character quoting checks are retained — PR #949— claimed: Codex, branch test/r5.2-macos-remote-query-path+- [x] `which::tests::usual_windows_dirs_come_after_path` (platform) failed on Windows with 10+ found, 9 expected ("nothing twice"): #748's second Antigravity folder, counted since #770+- [x] `capture::console_tests` (platform) timed out now and then with the whole platform suite+ running. On Windows Server 2022 a program another thread starts while a console opens+ inherits its handles and holds it open until it ends, and closing a console that programs+ are still attached to can hang its host. Program starts and console opens (and Unix terminal+ opens, which have a shorter gap of the same kind) now take one lock (`process::STARTING`),+ and a timeout ends the program's whole tree before closing+ — claimed: SoldierPePe, branch test/r5.2-console-capture-flake+- [ ] A console program that exits by itself and leaves programs attached to its console: closing+ it 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+- [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 still+ holds it open ("Text file busy"). #787 tries again: on Linux 4 of 72 runs failed before+ it, none after+- [ ] `a_remote_query_expands_only_the_remote_working_directory` (ai crate) fails on a Mac: the+ temp folder is `/var/…`, a link to `/private/var/…`, and the shell's `$PWD` gives the second.+ Compare with the folder's canonical path- [ ] Revisit the two advisory exceptions in `deny.toml`: `rsa` (no fix; we only verify with publickeys) when rsa 0.10 lands, and unmaintained `zbus_names`/`zvariant` (Linux-only via keyringand accesskit) when those move on. `deny.toml`'s comments say this is tracked in R0.3: pointdiff --git a/docs/pitfalls.md b/docs/pitfalls.mdindex 74eba8cb..028ea7c3 100644--- a/docs/pitfalls.md+++ b/docs/pitfalls.md@@ -171,6 +171,12 @@ the program it opened stayed open (and held the shell's pipe, so the command nevthe caller may exit next, use `kill_and_wait`, and check after a live run that nothing was leftrunning.+**A console held open by a program started next door.** On Windows Server 2022 opening a+pseudoconsole briefly makes inheritable handles (a Unix terminal too, until it's made close-on-exec),+so a program another thread started at that moment kept the console open until it ended, and closing+a console with programs still attached hung its host. Program starts and console or terminal opens+take `process::STARTING`; end a console's process tree before closing it.+**The newest program isn't the app.** Play started the newest executable in `target/`; aworkspace that builds an app and its CLI started the CLI, which printed help and quit with noconsole, so the button did nothing, three times in a row. Pick what to start from what the project