-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Print thread ID in panic message #115746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Print thread ID in panic message #115746
Conversation
r? @davidtwco (rustbot has picked a reviewer for you, use r? to override) |
I think this needs FCP since it's a visible change @rustbot label -T-libs -T-compiler +T-libs-api |
d68fab6
to
371dd28
Compare
The Miri subtree was changed cc @rust-lang/miri |
8e72037
to
3da1046
Compare
Maybe
or
? That fits more with the way named threads are shown. |
Note that this is a rust-internal thread ID, which isn't the OS thread ID which can be relevant if you're also logging from C code or looking at things with GDB. And |
I just proposed the syntax as a demo, figured libs-api would make the final choice - though between those two, I like the first one better
It is kind of unfortunate that Rust and C won't use the same thread IDs. But this seems worthwhile still - Rust-only applications printing the thread ID in logs is common ( This is all inspired by the below failure I recently had in an highly threaded system. The entire backtrace uselessly displayed one repeated function, with no hints about which preceding log events were relevant:
I don't think that stability of the thread ID is much of a concern since we will always have some integer ID for a thread, even if the semantics of that ID might change. The important thing is probably just that this panic message thread ID is the same as what shows up as |
Another display option - it looks a bit cleaner to not even call out that a thread is unnamed, and I don't think it adds much
Or we could use
I also tried to poke some discussion at #67939 |
☔ The latest upstream changes (presumably #115627) made this pull request unmergeable. Please resolve the merge conflicts. |
@joshtriplett do you have any thoughts on this? |
3da1046
to
e055544
Compare
Sorry to add bike shedding to the thread. Here is a slight derivative of the last suggestion, which omits a "with":
Although it's uglier than using the thread identifier in a sentence, using the Debug form makes it look like a Rust value rather than something else. It's also consistent with other output from Rust, although the case can definitely be made that thread IDs deserve a special case. |
I know Josh has a pretty busy queue, so perhaps it is best to reroll r? libs-api |
I think it wouldn't need one. T-libs-api FCP is primarily for any time a permanent API commitment is being made by the standard library, and deprecations. In contrast, anything that we can just change back with minimal effort or disruption would not warrant FCP. #112849 had a T-libs FCP but I am not familiar with their criteria for that. @rust-lang/libs would you want to handle this PR? In any case, this looks good to me. |
I don't know that this needs FCP. I'm not opposed and there is precedent for it, but OTOH we wouldn't FCP a panic message change somewhere in the stdlib. So I don't feel strongly. In any case, I think this change is a good idea, and am in favor. |
@@ -115,6 +115,14 @@ impl Thread { | |||
} | |||
} | |||
|
|||
pub(crate) fn current_os_id() -> Option<u64> { | |||
// SAFETY: FFI call with no preconditions. | |||
let id: u32 = unsafe { c::GetThreadId(c::GetCurrentThread()) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am going to change this to GetCurrentThreadId
that does both steps, but not until the Miri shims merge so I can stop manually syncing changes.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Print thread ID in panic message `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>' (12345) panicked at src/main.rs:3:5: explicit panic Stack overflow messages are updated as well. This change applies to both named and unnamed threads. The ID printed is the OS integer thread ID rather than the Rust thread ID, which should also be what debuggers print. try-job: `*various*` try-job: `x86_64-msvc*`
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Print thread ID in panic message `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>' (12345) panicked at src/main.rs:3:5: explicit panic Stack overflow messages are updated as well. This change applies to both named and unnamed threads. The ID printed is the OS integer thread ID rather than the Rust thread ID, which should also be what debuggers print. try-job: `*various*` try-job: `x86_64-msvc*`
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Print thread ID in panic message `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>' (12345) panicked at src/main.rs:3:5: explicit panic Stack overflow messages are updated as well. This change applies to both named and unnamed threads. The ID printed is the OS integer thread ID rather than the Rust thread ID, which should also be what debuggers print. try-job: aarch64-gnu try-job: test-various try-job: x86_64-gnu try-job: x86_64-gnu-aux try-job: x86_64-msvc-1
This comment was marked as outdated.
This comment was marked as outdated.
@bors2 try |
Print thread ID in panic message `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>' (12345) panicked at src/main.rs:3:5: explicit panic Stack overflow messages are updated as well. This change applies to both named and unnamed threads. The ID printed is the OS integer thread ID rather than the Rust thread ID, which should also be what debuggers print. try-job: x86_64-msvc-1 try-job: `x86_64-mingw-*`
// Most platforms have a function returning a `pid_t` or int, which is an `i32`. | ||
if #[cfg(any(target_os = "android", target_os = "linux", target_os = "nto"))] { | ||
// SAFETY: FFI call with no preconditions. | ||
let id: libc::pid_t = unsafe { libc::gettid() }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is too new for our baseline on linux-gnu
targets, glibc 2.17 vs gettid@GLIBC_2.30
, but SYS_gettid
goes way back to kernel 2.4.11, so using the syscall!
macro should have no trouble.
(I expect the dist
jobs would have uncovered this too, simply failing to link with their older sysroots.)
I have no idea about baseline availability on most of the other targets though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the catch. I just did a quick breeze through the others:
- OpenBSD: added in 2014 (unsure of specific version) openbsd/src@cbcba41
- FreeBSD: added in 2011 / 9.0 freebsd/freebsd-src@678b238
- NetBSD since 2003 (unsure of specific version) https://github.com/NetBSD/src/blame/d0905be2d3efc632b786a696905d9a3ba646a6ec/include/lwp.h#L47
- Apple - unsure how to check this but this man page says 10.12.3, which matches our 10.12 minimum https://www.manpagez.com/man/3/pthread_threadid_np/
So I think the rest should be fine here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated Linux to use the syscall.
For Neutrino, based on rust-lang/libc@720151f gettid
is supported at least since 7.1. I updated nto to only be enabled on 7.1+ but just in case @jonathanpallant any idea if gettid
is actually available in 7.0 as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR still needs the Miri changes to merge first, but @cuviper is the current design enough to resolve the concern (link) and get FCP started?
Cc @joshtriplett since you registered the concern.
// FIXME: output is different between stage1 and stage2. This should be possible to remove | ||
// when stages get bumped. | ||
// For the grep: cfg(bootstrap) | ||
//@normalize-stderr-test: "thread 'rustc' panicked" -> "thread 'rustc' ($$TID) panicked" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still true after the stage0 redesign? Which library is getting used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seemingly so; I needed this in my most recent push which was based on master from ~3 days ago. The same for tests/ui/track-diagnostics/track.rs
, I'm less certain about what might be going on there.
Cc @rust-lang/bootstrap in case you have any ideas. For context, it seems like stage1 at the test here, as well as for track-diagnostics/track.rs
, may be getting run with beta or nightly std rather than in-tree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look at what stdlib is being used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Locally track-diagnostics/track.rs
runs with stage 1 stdlib, at least with ./x test tests/ui/track-diagnostics/track.rs
. I'll check the miri test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm from testing locally; if I remove the normalize in track.rs
, ./x test tests/ui/track-diagnostics/track.rs
fails but ./x test --stage 2 tests/ui/track-diagnostics/track.rs
succeeds. I think this one failed in CI as well but I don't recall for sure.
The Miri test I know for sure failed both locally and in CI without the workaround.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wait, this makes sense doesn't it; everything that is a panic during runtime is using the in-tree compiler, so the output change will show up in the tests immediately. But the two tests here are panics in rustc at comptime, and that rustc was built using downloaded std so it won't have the update - at least until stage 2.
So having the normalize step here is expected to be needed (or somehow skipping the test for stage1), I was just getting myself confused around the redesign and thinking it shouldn't be necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the comment to make this more clear
@bors2 try |
Print thread ID in panic message `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>' (12345) panicked at src/main.rs:3:5: explicit panic Stack overflow messages are updated as well. This change applies to both named and unnamed threads. The ID printed is the OS integer thread ID rather than the Rust thread ID, which should also be what debuggers print. try-job: dist-x86_64-freebsd try-job: dist-x86_64-linux try-job: dist-x86_64-linux-alt try-job: dist-x86_64-netbsd
panic!
does not print any identifying information for threads that areunnamed. However, in many cases, the thread ID can be determined.
This changes the panic message from something like this:
To something like this:
Stack overflow messages are updated as well.
This change applies to both named and unnamed threads. The ID printed is
the OS integer thread ID rather than the Rust thread ID, which should
also be what debuggers print.
try-job: dist-x86_64-freebsd
try-job: dist-x86_64-linux
try-job: dist-x86_64-linux-alt
try-job: dist-x86_64-netbsd