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:?})"); + } }