diff options
| author | John Kåre Alsaker <john.kare.alsaker@gmail.com> | 2024-03-15 19:09:24 +0100 |
|---|---|---|
| committer | The 8472 <git@infinite-source.de> | 2025-01-01 15:58:29 +0100 |
| commit | 4bf85c25eceee6172ed3892570f36b5d06d5177f (patch) | |
| tree | 0286f505fd0185e26a2869dbaccc5d9348d3a01e /library/std/src/panicking.rs | |
| parent | eeeff9a66cda13934600800eee40fd84ba1647eb (diff) | |
| download | rust-4bf85c25eceee6172ed3892570f36b5d06d5177f.tar.gz rust-4bf85c25eceee6172ed3892570f36b5d06d5177f.zip | |
Try to write the panic message with a single `write_all` call
Diffstat (limited to 'library/std/src/panicking.rs')
| -rw-r--r-- | library/std/src/panicking.rs | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index e7ce5bc6140..3b02254548b 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -266,7 +266,23 @@ fn default_hook(info: &PanicHookInfo<'_>) { // 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}"); + // Try to write the panic message to a buffer first to prevent other concurrent outputs + // interleaving with it. + let mut buffer = [0u8; 512]; + let mut cursor = crate::io::Cursor::new(&mut buffer[..]); + + let write_msg = |dst: &mut dyn crate::io::Write| { + // We add a newline to ensure the panic message appears at the start of a line. + writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}") + }; + + if write_msg(&mut cursor).is_ok() { + let pos = cursor.position() as usize; + let _ = err.write_all(&buffer[0..pos]); + } else { + // The message did not fit into the buffer, write it directly instead. + let _ = write_msg(err); + }; static FIRST_PANIC: AtomicBool = AtomicBool::new(true); |
