diff options
| author | jyn <github@jyn.dev> | 2024-07-05 17:24:10 -0400 |
|---|---|---|
| committer | jyn <github@jyn.dev> | 2024-07-12 11:52:04 -0400 |
| commit | 1c8f9bb84d5956abe6671bf77ccaba64505266a5 (patch) | |
| tree | 392bbbc17025cf590dea0819277c294f6e07149d | |
| parent | de14f1f932f4f11130be3fed5cd370bd0936032e (diff) | |
| download | rust-1c8f9bb84d5956abe6671bf77ccaba64505266a5.tar.gz rust-1c8f9bb84d5956abe6671bf77ccaba64505266a5.zip | |
fix interleaved panic output
previously, we only held a lock for printing the backtrace itself. since all threads were printing to the same file descriptor, that meant random output in the default panic hook would be interleaved with the backtrace. now, we hold the lock for the full duration of the hook, and the output is ordered.
| -rw-r--r-- | library/std/src/panicking.rs | 8 | ||||
| -rw-r--r-- | library/std/src/sys/backtrace.rs | 53 | ||||
| -rw-r--r-- | tests/ui/backtrace/synchronized-panic-handler.rs | 2 | ||||
| -rw-r--r-- | tests/ui/backtrace/synchronized-panic-handler.run.stderr | 8 |
4 files changed, 37 insertions, 34 deletions
diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index ebd05415695..418a855fb72 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -253,16 +253,20 @@ fn default_hook(info: &PanicHookInfo<'_>) { let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>"); let write = |err: &mut dyn crate::io::Write| { + // Use a lock to prevent mixed output in multithreading context. + // Some platforms also require it when printing a backtrace, like `SymFromAddr` on Windows. + let mut lock = backtrace::lock(); let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}"); static FIRST_PANIC: AtomicBool = AtomicBool::new(true); match backtrace { + // SAFETY: we took out a lock just a second ago. Some(BacktraceStyle::Short) => { - drop(backtrace::print(err, crate::backtrace_rs::PrintFmt::Short)) + drop(lock.print(err, crate::backtrace_rs::PrintFmt::Short)) } Some(BacktraceStyle::Full) => { - drop(backtrace::print(err, crate::backtrace_rs::PrintFmt::Full)) + drop(lock.print(err, crate::backtrace_rs::PrintFmt::Full)) } Some(BacktraceStyle::Off) => { if FIRST_PANIC.swap(false, Ordering::Relaxed) { diff --git a/library/std/src/sys/backtrace.rs b/library/std/src/sys/backtrace.rs index 0b2338fd7de..7401d8ce320 100644 --- a/library/std/src/sys/backtrace.rs +++ b/library/std/src/sys/backtrace.rs @@ -7,44 +7,41 @@ use crate::fmt; use crate::io; use crate::io::prelude::*; use crate::path::{self, Path, PathBuf}; -use crate::sync::{Mutex, PoisonError}; +use crate::sync::{Mutex, MutexGuard, PoisonError}; /// Max number of frames to print. const MAX_NB_FRAMES: usize = 100; -pub fn lock() -> impl Drop { +pub(crate) struct BacktraceLock<'a>(#[allow(dead_code)] MutexGuard<'a, ()>); + +pub(crate) fn lock<'a>() -> BacktraceLock<'a> { static LOCK: Mutex<()> = Mutex::new(()); - LOCK.lock().unwrap_or_else(PoisonError::into_inner) + BacktraceLock(LOCK.lock().unwrap_or_else(PoisonError::into_inner)) } -/// Prints the current backtrace. -pub fn print(w: &mut dyn Write, format: PrintFmt) -> io::Result<()> { - // There are issues currently linking libbacktrace into tests, and in - // general during std's own unit tests we're not testing this path. In - // test mode immediately return here to optimize away any references to the - // libbacktrace symbols - if cfg!(test) { - return Ok(()); - } - - // Use a lock to prevent mixed output in multithreading context. - // Some platforms also requires it, like `SymFromAddr` on Windows. - unsafe { - let _lock = lock(); - _print(w, format) - } -} +impl BacktraceLock<'_> { + /// Prints the current backtrace. + /// + /// NOTE: this function is not Sync. The caller must hold a mutex lock, or there must be only one thread in the program. + pub(crate) fn print(&mut self, w: &mut dyn Write, format: PrintFmt) -> io::Result<()> { + // There are issues currently linking libbacktrace into tests, and in + // general during std's own unit tests we're not testing this path. In + // test mode immediately return here to optimize away any references to the + // libbacktrace symbols + if cfg!(test) { + return Ok(()); + } -unsafe fn _print(w: &mut dyn Write, format: PrintFmt) -> io::Result<()> { - struct DisplayBacktrace { - format: PrintFmt, - } - impl fmt::Display for DisplayBacktrace { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - unsafe { _print_fmt(fmt, self.format) } + struct DisplayBacktrace { + format: PrintFmt, + } + impl fmt::Display for DisplayBacktrace { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + unsafe { _print_fmt(fmt, self.format) } + } } + write!(w, "{}", DisplayBacktrace { format }) } - write!(w, "{}", DisplayBacktrace { format }) } unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::Result { diff --git a/tests/ui/backtrace/synchronized-panic-handler.rs b/tests/ui/backtrace/synchronized-panic-handler.rs index 0ea285968d5..00eb249da1d 100644 --- a/tests/ui/backtrace/synchronized-panic-handler.rs +++ b/tests/ui/backtrace/synchronized-panic-handler.rs @@ -1,6 +1,8 @@ //@ run-pass //@ check-run-results //@ edition:2021 +//@ exec-env:RUST_BACKTRACE=0 +//@ needs-threads use std::thread; const PANIC_MESSAGE: &str = "oops oh no woe is me"; diff --git a/tests/ui/backtrace/synchronized-panic-handler.run.stderr b/tests/ui/backtrace/synchronized-panic-handler.run.stderr index 06ef53836c2..b7c815a94c8 100644 --- a/tests/ui/backtrace/synchronized-panic-handler.run.stderr +++ b/tests/ui/backtrace/synchronized-panic-handler.run.stderr @@ -1,5 +1,5 @@ -thread '<unnamed>' panicked at $DIR/synchronized-panic-handler.rs:thread '8<unnamed>:5' panicked at : -oops oh no woe is me$DIR/synchronized-panic-handler.rs -:note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -8:5: +thread '<unnamed>' panicked at $DIR/synchronized-panic-handler.rs:10:5: +oops oh no woe is me +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace +thread '<unnamed>' panicked at $DIR/synchronized-panic-handler.rs:10:5: oops oh no woe is me |
