From 02de3303af294e3eeebea2a7fc983ee838e8bdf6 Mon Sep 17 00:00:00 2001 From: merzleckeier <362449027+merzleckeier@users.noreply.github.com> Date: Sun, 29 Nov 2026 22:53:14 +0100 Subject: [PATCH] warm: report compiler process stack overflows --- d3d12/apps/leonida/src/warm.rs | 122 ++++++++++++++++++++++++++++++--- 1 file changed, 113 insertions(+), 9 deletions(-) diff --git a/d3d12/apps/leonida/src/warm.rs b/d3d12/apps/leonida/src/warm.rs index e72cda75a7..39df8efb2a 100644 --- a/d3d12/apps/leonida/src/warm.rs +++ b/d3d12/apps/leonida/src/warm.rs @@ -2,9 +2,9 @@ //! processes of their own: a compiler that crashes or hangs costs one pipeline. use std::collections::HashSet; -use std::io::{BufRead, BufReader, Write}; +use std::io::{BufRead, BufReader, Read, Write}; use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; +use std::process::{Child, Command, Stdio}; use std::sync::Mutex; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::time::{Duration, Instant}; @@ -152,7 +152,7 @@ fn lock(m: &Mutex) -> std::sync::MutexGuard<'_, T> { } /// Has a process make `these`; how many it made, and how it ended if not well. -fn child(these: &[usize], mut each: impl FnMut(usize, f64, bool)) -> (usize, Option) { +fn child(these: &[usize], each: impl FnMut(usize, f64, bool)) -> (usize, Option) { let list: Vec = these.iter().map(usize::to_string).collect(); let spawned = std::env::current_exe().and_then(|exe| { use std::os::windows::process::CommandExt; @@ -161,21 +161,63 @@ fn child(these: &[usize], mut each: impl FnMut(usize, f64, bool)) -> (usize, Opt .args(["warm", "--these", &list.join(",")]) .stdin(Stdio::null()) .stdout(Stdio::piped()) - .stderr(Stdio::null()) + .stderr(Stdio::piped()) .creation_flags(IDLE_PRIORITY_CLASS) .spawn() }); - let Ok(mut process) = spawned else { return (these.len(), None) }; - let Some(out) = process.stdout.take() else { return (these.len(), None) }; + let Ok(process) = spawned else { + return (these.len(), None); + }; + collect_child(process, these.len(), LONGEST, each) +} + +fn collect_child( + mut process: Child, + total: usize, + quiet_limit: Duration, + mut each: impl FnMut(usize, f64, bool), +) -> (usize, Option) { + let (Some(out), Some(err)) = (process.stdout.take(), process.stderr.take()) else { + return (total, None); + }; let process = Mutex::new(process); let (heard, over) = (Mutex::new(Instant::now()), AtomicBool::new(false)); let hung = AtomicBool::new(false); + let stack_overflow = AtomicBool::new(false); let mut made = 0; std::thread::scope(|s| { + s.spawn(|| { + let mut err = BufReader::new(err); + let mut line = Vec::new(); + // Wine can leave a process alive after its Rust thread hit the stack guard. + // Drain stderr without saving driver output or treating it as progress. + const MAX_DIAGNOSTIC: u64 = 4096; + loop { + line.clear(); + let read = err.by_ref().take(MAX_DIAGNOSTIC).read_until(b'\n', &mut line); + if !read.is_ok_and(|n| n != 0) { + break; + } + if line.len() as u64 == MAX_DIAGNOSTIC && !line.ends_with(b"\n") { + if err.skip_until(b'\n').is_err() { + break; + } + continue; + } + let diagnostic = line.trim_ascii(); + if diagnostic.starts_with(b"thread '") + && diagnostic.ends_with(b" has overflowed its stack") + { + stack_overflow.store(true, Ordering::Relaxed); + let _ = lock(&process).kill(); + return; + } + } + }); s.spawn(|| { while !over.load(Ordering::Relaxed) { std::thread::sleep(Duration::from_millis(500)); - if lock(&heard).elapsed() > LONGEST { + if lock(&heard).elapsed() > quiet_limit { hung.store(true, Ordering::Relaxed); let _ = lock(&process).kill(); return; @@ -196,12 +238,15 @@ fn child(these: &[usize], mut each: impl FnMut(usize, f64, bool)) -> (usize, Opt over.store(true, Ordering::Relaxed); }); let status = lock(&process).wait(); + if stack_overflow.load(Ordering::Relaxed) { + return (made, Some("compiler process stack overflow".into())); + } let well = status.as_ref().is_ok_and(|s| s.success()); if well { - return (these.len(), None); + return (total, None); } let why = if hung.load(Ordering::Relaxed) { - format!("hung for {} s", LONGEST.as_secs()) + format!("hung for {} s", quiet_limit.as_secs()) } else { status.map_or_else(|e| e.to_string(), |s| format!("{s}")) }; @@ -228,3 +273,62 @@ fn make(dir: &Path, these: &str) -> Result<()> { let _ = out.flush(); std::process::exit(0) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn warm_child_fixture() { + let Ok(mode) = std::env::var("LEONIDA_TEST_WARM_CHILD") else { + return; + }; + println!("\nmade 7 2.00 1"); + println!("at 8"); + std::io::stdout().flush().unwrap(); + if mode == "overflow" { + std::io::stderr().write_all(&[b'x'; 8192]).unwrap(); + eprintln!(); + eprintln!("thread 'main' (36) has overflowed its stack"); + } + // Simulate a process whose remaining threads stay alive after the diagnostic. + loop { + std::thread::park(); + } + } + + fn run_fixture(mode: &str, quiet_limit: Duration) -> (usize, Option) { + let process = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "warm::tests::warm_child_fixture", "--nocapture"]) + .env("LEONIDA_TEST_WARM_CHILD", mode) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let mut reported = Vec::new(); + let result = collect_child(process, 2, quiet_limit, |n, ms, there| { + reported.push((n, ms, there)); + }); + assert_eq!(reported, [(7, 2.0, true)]); + result + } + + #[test] + fn reports_stack_overflow_before_waiting_for_the_timeout() { + let quiet_limit = Duration::from_secs(8); + let started = Instant::now(); + assert_eq!( + run_fixture("overflow", quiet_limit), + (1, Some("compiler process stack overflow".into())) + ); + assert!(started.elapsed() < quiet_limit, "the watchdog had to stop the child"); + } + + #[test] + fn silent_child_still_times_out() { + assert_eq!( + run_fixture("quiet", Duration::from_secs(2)), + (1, Some("hung for 2 s".into())) + ); + } +}