Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion d3d12/apps/leonida/src/player_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,12 @@ 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, so failed writes
/// are silently dropped.
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.
Expand Down Expand Up @@ -812,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,
);
}
}
Loading