Skip to content

Commit c322091

Browse files
committed
Print thread ID in panic message if thread name is unknown
`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.
1 parent 5c8459f commit c322091

File tree

7 files changed

+16
-9
lines changed

7 files changed

+16
-9
lines changed

library/std/src/panicking.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,15 @@ fn default_hook(info: &PanicHookInfo<'_>) {
250250

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

255255
let write = |err: &mut dyn crate::io::Write| {
256-
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
256+
let _ = match thread_info {
257+
Some((id, name)) => {
258+
writeln!(err, "thread '{name}' ({id}) panicked at {location}:\n{msg}")
259+
}
260+
None => writeln!(err, "thread '<unnamed>' panicked at {location}:\n{msg}"),
261+
};
257262

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

tests/ui/proc-macro/load-panic-backtrace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ aux-build:test-macros.rs
22
//@ compile-flags: -Z proc-macro-backtrace
33
//@ rustc-env:RUST_BACKTRACE=0
4-
//@ normalize-stderr-test "thread '.*' panicked " -> ""
4+
//@ normalize-stderr-test "thread '.*' \(\d+\) panicked " -> ""
55
//@ normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
66
//@ needs-unwind proc macro panics to report errors
77

tests/ui/process/multi-panic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ fn check_for_no_backtrace(test: std::process::Output) {
88
let err = String::from_utf8_lossy(&test.stderr);
99
let mut it = err.lines();
1010

11-
assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' panicked")), Some(true));
11+
assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' (")), Some(true));
1212
assert_eq!(it.next().is_some(), true);
1313
assert_eq!(it.next(), Some("note: run with `RUST_BACKTRACE=1` \
1414
environment variable to display a backtrace"));
15-
assert_eq!(it.next().map(|l| l.starts_with("thread 'main' panicked at")), Some(true));
15+
assert_eq!(it.next().map(|l| l.starts_with("thread 'main' (")), Some(true));
1616
assert_eq!(it.next().is_some(), true);
1717
assert_eq!(it.next(), None);
1818
}

tests/ui/test-attrs/terse.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//@ check-run-results
55
//@ exec-env:RUST_BACKTRACE=0
66
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
7+
//@ normalize-stdout-test "(thread '.*') \(\d+\)" -> "$1 (tid)"
78
//@ ignore-emscripten no threads support
89
//@ needs-unwind
910

tests/ui/test-attrs/terse.run.stdout

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ foo2 --- FAILED
99
failures:
1010

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

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

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

2424

tests/ui/test-attrs/test-panic-abort.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//@ check-run-results
66
//@ exec-env:RUST_BACKTRACE=0
77
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
8+
//@ normalize-stdout-test "(thread '.*') \(\d+\)" -> "$1 (tid)"
89

910
//@ ignore-android #120567
1011
//@ ignore-wasm no panic or subprocess support

tests/ui/test-attrs/test-panic-abort.run.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ hello, world
1717
testing123
1818
---- it_fails stderr ----
1919
testing321
20-
thread 'main' panicked at $DIR/test-panic-abort.rs:39:5:
20+
thread 'main' (tid) panicked at $DIR/test-panic-abort.rs:40:5:
2121
assertion `left == right` failed
2222
left: 2
2323
right: 5

0 commit comments

Comments
 (0)