Skip to content

Commit 2259e76

Browse files
borsgitbot
authored and
gitbot
committed
Auto merge of rust-lang#122565 - Zoxc:atomic-panic-msg, r=the8472
Try to write the panic message with a single `write_all` call This writes the panic message to a buffer before writing to stderr. This allows it to be printed with a single `write_all` call, preventing it from being interleaved with other outputs. It also adds newlines before and after the message ensuring that only the panic message will have its own lines. Before: ``` thread 'thread 'thread 'thread 'thread '<unnamed>thread 'thread 'thread 'thread '<unnamed><unnamed>thread '<unnamed>' panicked at ' panicked at <unnamed><unnamed><unnamed><unnamed><unnamed>' panicked at <unnamed>' panicked at src\heap.rssrc\heap.rs' panicked at ' panicked at ' panicked at ' panicked at ' panicked at src\heap.rs' panicked at src\heap.rs::src\heap.rssrc\heap.rssrc\heap.rssrc\heap.rssrc\heap.rs:src\heap.rs:455455:::::455:455::455455455455455:455:99:::::9:9: : 999: 999: assertion failed: size <= (*queue).block_size: : assertion failed: size <= (*queue).block_size: assertion failed: size <= (*queue).block_size: : : assertion failed: size <= (*queue).block_sizeassertion failed: size <= (*queue).block_sizeassertion failed: size <= (*queue).block_size assertion failed: size <= (*queue).block_size assertion failed: size <= (*queue).block_sizeassertion failed: size <= (*queue).block_sizeerror: process didn't exit successfully: `target\debug\direct_test.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN) ``` After: ``` thread '<unnamed>' panicked at src\heap.rs:455:9: assertion failed: size <= (*queue).block_size thread '<unnamed>' panicked at src\heap.rs:455:9: assertion failed: size <= (*queue).block_size thread '<unnamed>' panicked at src\heap.rs:455:9: assertion failed: size <= (*queue).block_size error: process didn't exit successfully: `target\debug\direct_test.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN) ``` --- try-jobs: x86_64-gnu-llvm-18
2 parents 53d537f + a2b498b commit 2259e76

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

std/src/panicking.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,23 @@ fn default_hook(info: &PanicHookInfo<'_>) {
266266
// Use a lock to prevent mixed output in multithreading context.
267267
// Some platforms also require it when printing a backtrace, like `SymFromAddr` on Windows.
268268
let mut lock = backtrace::lock();
269-
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
269+
// Try to write the panic message to a buffer first to prevent other concurrent outputs
270+
// interleaving with it.
271+
let mut buffer = [0u8; 512];
272+
let mut cursor = crate::io::Cursor::new(&mut buffer[..]);
273+
274+
let write_msg = |dst: &mut dyn crate::io::Write| {
275+
// We add a newline to ensure the panic message appears at the start of a line.
276+
writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}")
277+
};
278+
279+
if write_msg(&mut cursor).is_ok() {
280+
let pos = cursor.position() as usize;
281+
let _ = err.write_all(&buffer[0..pos]);
282+
} else {
283+
// The message did not fit into the buffer, write it directly instead.
284+
let _ = write_msg(err);
285+
};
270286

271287
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
272288

0 commit comments

Comments
 (0)