From 4a99fce5e153b720021d0ddb5cca3d9a05ce37b1 Mon Sep 17 00:00:00 2001 From: vakaobr <15154330+vakaobr@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:03:49 -0300 Subject: [PATCH] xcl run: echo log lines on a thread of their own, never blocking a caller log::write echoed every Info line to stderr synchronously, on whichever thread logged, after the file line. A stderr that nobody reads blocks that write: a pipe whose reader stopped, or a Windows console with text selected. Each thread that logged then stopped right after its file line, including the title's own threads inside a console call, and the title froze with nothing in the log to say why. The echo now goes through a bounded queue to an "xcl-stderr" thread. A caller never waits: with the queue full (4096 lines) the line is dropped and counted, and the next line echoed says how many were left out. The file write is unchanged and still comes first. log::flush gives the echo up to 200 ms to catch up, so lines from the end of a run still reach the console. Measured on an M5 Pro, macOS 27.2.1, CrossOver 26.3 + D3DMetal, with the test title presenting 1920x1080 frames on the 60 Hz frame clock and stderr connected to a pipe whose reader never reads: - before: the title's frames stopped about 2 s in; xcl.log ended at 69 lines, each logging thread right after one line; - after: 240 s at 60 fps to the end, 402 log lines, exit code 0. an_unread_stderr_does_not_stop_the_thread_that_logs (new) runs this test binary again as a child whose stderr is a pipe the test never reads; the child logs 4000 lines and must exit within 30 s. It fails before this change ("logging stopped on a stderr nobody reads") and passes after; the blocked pipe and the queued lines stay in the child. --- crates/xcl/src/log.rs | 83 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/log.rs b/crates/xcl/src/log.rs index 7f5d1f180a..e945ccfcf9 100644 --- a/crates/xcl/src/log.rs +++ b/crates/xcl/src/log.rs @@ -4,7 +4,8 @@ use std::fs::File; use std::io::Write; use std::path::Path; -use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering}; +use std::sync::mpsc::{sync_channel, SyncSender}; use std::sync::{Mutex, OnceLock}; use std::time::Instant; @@ -85,7 +86,42 @@ pub fn write(level: Level, args: std::fmt::Arguments<'_>) { } } if ECHO.load(Ordering::Relaxed) && level <= Level::Info { - let _ = std::io::stderr().write_all(format!("xcl: {args}\n").as_bytes()); + echo(format!("xcl: {args}\n")); + } +} + +/// Echo lines waiting for the stderr thread, and lines dropped since the last one it wrote. +static ECHO_PENDING: AtomicUsize = AtomicUsize::new(0); +static ECHO_DROPPED: AtomicUsize = AtomicUsize::new(0); +const ECHO_QUEUE: usize = 4096; + +/// The echo goes through a thread of its own: a write to a stderr nobody reads (a pipe whose +/// reader stopped, a console in selection mode) blocks, and it must not block the thread that +/// logs, which may be the title's inside a console call. With the queue full, lines are dropped +/// and counted; the file has them all. +fn echo(line: String) { + static TX: OnceLock>> = OnceLock::new(); + let tx = TX.get_or_init(|| { + let (tx, rx) = sync_channel::(ECHO_QUEUE); + let spawned = std::thread::Builder::new().name("xcl-stderr".into()).spawn(move || { + for line in rx { + let dropped = ECHO_DROPPED.swap(0, Ordering::Relaxed); + let mut err = std::io::stderr().lock(); + if dropped > 0 { + let _ = writeln!(err, "xcl: ({dropped} log lines not echoed here: stderr was not being read; the log file has them)"); + } + let _ = err.write_all(line.as_bytes()); + drop(err); + ECHO_PENDING.fetch_sub(1, Ordering::Relaxed); + } + }); + spawned.is_ok().then_some(tx) + }); + let Some(tx) = tx else { return }; + ECHO_PENDING.fetch_add(1, Ordering::Relaxed); + if tx.try_send(line).is_err() { + ECHO_PENDING.fetch_sub(1, Ordering::Relaxed); + ECHO_DROPPED.fetch_add(1, Ordering::Relaxed); } } @@ -95,6 +131,11 @@ pub fn flush() { let _ = f.flush(); } } + // the echo gets a moment to catch up (the process may be about to end), never more + let until = Instant::now() + std::time::Duration::from_millis(200); + while ECHO_PENDING.load(Ordering::Relaxed) > 0 && Instant::now() < until { + std::thread::sleep(std::time::Duration::from_millis(5)); + } } /// A panic goes to the log as well as to stderr. @@ -133,4 +174,42 @@ mod tests { assert!(died.is_err()); assert!(log.contains("thread 'xcl-dies' panicked at ") && log.contains("gone for the run"), "the log: {log:?}"); } + + /// A stderr nobody reads (a pipe whose reader stopped, a console in selection mode) must not + /// stop the thread that logs: the echo is put off or dropped, the file line is written. The + /// logging runs in a child process of this test binary whose stderr is a pipe this test never + /// reads, so neither the blocked pipe nor the lines still queued reach the other tests. + #[test] + fn an_unread_stderr_does_not_stop_the_thread_that_logs() { + const CHILD: &str = "XCL_TEST_UNREAD_STDERR_CHILD"; + if std::env::var_os(CHILD).is_some() { + // far more than a pipe holds + for i in 0..4000 { + write(Level::Info, format_args!("echo into a stderr nobody reads, line {i:4} ........................................")); + } + flush(); + return; + } + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "log::tests::an_unread_stderr_does_not_stop_the_thread_that_logs", "--test-threads=1", "--nocapture"]) + .env(CHILD, "1") + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + let start = Instant::now(); + let status = loop { + if let Some(status) = child.try_wait().unwrap() { + break Some(status); + } + if start.elapsed() > std::time::Duration::from_secs(30) { + let _ = child.kill(); + let _ = child.wait(); + break None; + } + std::thread::sleep(std::time::Duration::from_millis(50)); + }; + assert!(status.is_some_and(|s| s.success()), "logging stopped on a stderr nobody reads (child: {status:?})"); + } }