Skip to content

Commit

Permalink
Print thread ID in panic message if thread name is unknown
Browse files Browse the repository at this point in the history
`panic!` does not print any identifying information for threads that are
unnamed. However, in many cases, the thread ID can be determined.

This changes the panic message from something like this:

    thread '<unnamed>' panicked at src/main.rs:3:5:
    explicit panic

To something like this:

    thread '<unnamed>' (1234) panicked at src/main.rs:3:5:
    explicit panic

This change applies to both named and unnamed threads. There is no
change for threads where an ID cannot be determined.
  • Loading branch information
tgross35 committed Jun 19, 2024
1 parent 5c8459f commit c322091
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
9 changes: 7 additions & 2 deletions library/std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,15 @@ fn default_hook(info: &PanicHookInfo<'_>) {

let msg = payload_as_str(info.payload());
let thread = thread::try_current();
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
let thread_info = thread.as_ref().map(|t| (t.id().as_u64(), t.name().unwrap_or("<unnamed>")));

let write = |err: &mut dyn crate::io::Write| {
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
let _ = match thread_info {
Some((id, name)) => {
writeln!(err, "thread '{name}' ({id}) panicked at {location}:\n{msg}")
}
None => writeln!(err, "thread '<unnamed>' panicked at {location}:\n{msg}"),
};

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/proc-macro/load-panic-backtrace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ aux-build:test-macros.rs
//@ compile-flags: -Z proc-macro-backtrace
//@ rustc-env:RUST_BACKTRACE=0
//@ normalize-stderr-test "thread '.*' panicked " -> ""
//@ normalize-stderr-test "thread '.*' \(\d+\) panicked " -> ""
//@ normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
//@ needs-unwind proc macro panics to report errors

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/process/multi-panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ fn check_for_no_backtrace(test: std::process::Output) {
let err = String::from_utf8_lossy(&test.stderr);
let mut it = err.lines();

assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' panicked")), Some(true));
assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' (")), Some(true));
assert_eq!(it.next().is_some(), true);
assert_eq!(it.next(), Some("note: run with `RUST_BACKTRACE=1` \
environment variable to display a backtrace"));
assert_eq!(it.next().map(|l| l.starts_with("thread 'main' panicked at")), Some(true));
assert_eq!(it.next().map(|l| l.starts_with("thread 'main' (")), Some(true));
assert_eq!(it.next().is_some(), true);
assert_eq!(it.next(), None);
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/test-attrs/terse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//@ check-run-results
//@ exec-env:RUST_BACKTRACE=0
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
//@ normalize-stdout-test "(thread '.*') \(\d+\)" -> "$1 (tid)"
//@ ignore-emscripten no threads support
//@ needs-unwind

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/test-attrs/terse.run.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ foo2 --- FAILED
failures:

---- abc stdout ----
thread 'abc' panicked at $DIR/terse.rs:12:5:
thread 'abc' (tid) panicked at $DIR/terse.rs:13:5:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- foo stdout ----
thread 'foo' panicked at $DIR/terse.rs:17:5:
thread 'foo' (tid) panicked at $DIR/terse.rs:18:5:
explicit panic

---- foo2 stdout ----
thread 'foo2' panicked at $DIR/terse.rs:22:5:
thread 'foo2' (tid) panicked at $DIR/terse.rs:23:5:
explicit panic


Expand Down
1 change: 1 addition & 0 deletions tests/ui/test-attrs/test-panic-abort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//@ check-run-results
//@ exec-env:RUST_BACKTRACE=0
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
//@ normalize-stdout-test "(thread '.*') \(\d+\)" -> "$1 (tid)"

//@ ignore-android #120567
//@ ignore-wasm no panic or subprocess support
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/test-attrs/test-panic-abort.run.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ hello, world
testing123
---- it_fails stderr ----
testing321
thread 'main' panicked at $DIR/test-panic-abort.rs:39:5:
thread 'main' (tid) panicked at $DIR/test-panic-abort.rs:40:5:
assertion `left == right` failed
left: 2
right: 5
Expand Down

0 comments on commit c322091

Please sign in to comment.