Skip to content

Commit 4962cfe

Browse files
committed
Try to write the panic message with a single write_all call
1 parent c5b5713 commit 4962cfe

File tree

77 files changed

+141
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+141
-14
lines changed

library/std/src/panicking.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,21 @@ fn default_hook(info: &PanicInfo<'_>) {
260260
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
261261

262262
let write = |err: &mut dyn crate::io::Write| {
263-
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
263+
// Try to write the panic message to a buffer first to prevent other concurrent outputs
264+
// interleaving with it.
265+
let mut buffer = [0u8; 512];
266+
let mut cursor = crate::io::Cursor::new(&mut buffer[..]);
267+
268+
let write_msg = |dst: &mut dyn crate::io::Write| {
269+
writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}")
270+
};
271+
272+
let _ = if write_msg(&mut cursor).is_ok() {
273+
let pos = cursor.position() as usize;
274+
err.write_all(&buffer[0..pos])
275+
} else {
276+
write_msg(err)
277+
};
264278

265279
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
266280

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind1.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at $DIR/exported_symbol_bad_unwind1.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.both.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
12
thread 'main' panicked at $DIR/exported_symbol_bad_unwind2.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
46
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
57
panic in a function that cannot unwind
68
stack backtrace:

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.definition.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
12
thread 'main' panicked at $DIR/exported_symbol_bad_unwind2.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
46
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
57
panic in a function that cannot unwind
68
stack backtrace:

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.extern_block.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at $DIR/exported_symbol_bad_unwind2.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/function_calls/return_pointer_on_unwind.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at $DIR/return_pointer_on_unwind.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
23
aborted execution: attempted to instantiate uninhabited type `!`
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
23
aborted execution: attempted to zero-initialize type `fn()`, which is invalid
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/panic/bad_unwind.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at $DIR/bad_unwind.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/panic/double_panic.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
12
thread 'main' panicked at $DIR/double_panic.rs:LL:CC:
23
first
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
46
thread 'main' panicked at $DIR/double_panic.rs:LL:CC:
57
second
68
stack backtrace:
9+
710
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
811
panic in a destructor during cleanup
912
thread caused non-unwinding panic. aborting.

0 commit comments

Comments
 (0)