From af4aeaa5e8bd27f25be621da984b74c38559fabb Mon Sep 17 00:00:00 2001 From: bobcatchris <83428450+bobcatchris@users.noreply.github.com> Date: Mon, 30 Nov 2026 23:52:07 -0600 Subject: [PATCH 1/2] d3d12: never panic printing player-context messages to stderr say() used eprintln!, which panics if writing to stderr fails. On shutdown, end_title -> end() -> TerminateProcess closes pipes while the always-running leonida-player-context thread keeps calling say; the title then panics on thread "leonida-player-context" with "failed printing to stderr: The pipe is being closed. (os error 232)", which tools/game_gate.py counts as panic (fault_after_stop). Write to stderr with writeln! and ignore the error instead. No behaviour change to player-context reading; 8 player_context tests pass. Separate from a GPU device removal at shutdown, which this does not address. AI-assisted: yes --- d3d12/apps/leonida/src/player_context.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/d3d12/apps/leonida/src/player_context.rs b/d3d12/apps/leonida/src/player_context.rs index 9cd0c03f49..5840254696 100644 --- a/d3d12/apps/leonida/src/player_context.rs +++ b/d3d12/apps/leonida/src/player_context.rs @@ -295,8 +295,11 @@ impl Reader { } } +/// Tells on stderr. Does NOT use `eprintln!`, which panics if writing to +/// stderr fails (e.g. a closed pipe at shutdown, on this thread, which has no +/// stop signal); a player-context message must never panic. fn say(line: &str) { - eprintln!("Leonida: player context: {line}"); + let _ = writeln!(std::io::stderr(), "Leonida: player context: {line}"); } /// The local date and time: year, month, day, hour, minute, second, millisecond. From 4db84a19500558db2b4b534ba264aa22f765c74a Mon Sep 17 00:00:00 2001 From: bobcatchris <83428450+bobcatchris@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:24:36 -0600 Subject: [PATCH 2/2] leonida: pin player_context::say closed-stderr behavior with subprocess test --- d3d12/apps/leonida/src/player_context.rs | 44 +++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/d3d12/apps/leonida/src/player_context.rs b/d3d12/apps/leonida/src/player_context.rs index 5840254696..450393bd14 100644 --- a/d3d12/apps/leonida/src/player_context.rs +++ b/d3d12/apps/leonida/src/player_context.rs @@ -297,7 +297,8 @@ impl Reader { /// Tells on stderr. Does NOT use `eprintln!`, which panics if writing to /// stderr fails (e.g. a closed pipe at shutdown, on this thread, which has no -/// stop signal); a player-context message must never panic. +/// stop signal); a player-context message must never panic, so failed writes +/// are silently dropped. fn say(line: &str) { let _ = writeln!(std::io::stderr(), "Leonida: player context: {line}"); } @@ -815,4 +816,45 @@ mod tests { assert_eq!(label_of(r"..\..\x"), "x"); assert_eq!(label_of(&"a".repeat(80)).len(), 32); } + + #[test] + fn say_does_not_panic_when_stderr_pipe_is_closed() { + const CHILD_ENV: &str = "PLAYER_CONTEXT_CLOSED_STDERR_TEST"; + if std::env::var_os(CHILD_ENV).is_some() { + // Wait until the parent has closed the stderr read end and signalled via stdin EOF. + let mut buf = [0u8; 1]; + let _ = std::io::Read::read(&mut std::io::stdin(), &mut buf); + assert!( + writeln!(std::io::stderr(), "probe").is_err(), + "expected writing to a closed stderr pipe to fail" + ); + for _ in 0..3 { + say("closed-pipe probe"); + } + return; + } + let exe = std::env::current_exe().expect("current test executable"); + let mut child = std::process::Command::new(exe) + .env(CHILD_ENV, "1") + .args([ + "--exact", + "player_context::tests::say_does_not_panic_when_stderr_pipe_is_closed", + "--nocapture", + ]) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .expect("spawn closed-stderr child"); + // Close our read end of stderr first, then release the child by closing stdin. + drop(child.stderr.take()); + drop(child.stdin.take()); + let output = child.wait_with_output().expect("wait for child"); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!( + output.status.success() && stdout.contains("1 passed"), + "say() failed or did not run on closed stderr (status={:?}, stdout={stdout})", + output.status, + ); + } }