-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Document panic from thread::current #107216
Conversation
r? @thomcc (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
Not sure whether we're able to guarantee a safe panic in this situation, or if this use of atexit is unsound. @rfcbot poll libs Guarantee panic? |
Guaranteeing anything after main is probably pretty difficult in general. |
In particular, if an implementation ran TLS dtors for the main thread after calling atexit callbacks, I'm unsure how we would implement this if we wanted to guarantee panic in that case -- we could do it in the case where we control I think it's reasonable to document that this will panic in some cases, though. |
See also Windows. In general I think it'd be good to actually document that the stdlib should not be used before/after main (or at least that your on your own if you try). But by the same token we can't really guarantee anything specific happens if you do do stuff before or after main. At least not in a cross-platform way. |
@@ -717,6 +717,13 @@ where | |||
|
|||
/// Gets a handle to the thread that invokes it. | |||
/// | |||
/// # Panics | |||
/// | |||
/// Panics if called beyond the end of `main`, when the Rust standard library's |
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.
Why not just remove the guarantee?
/// Panics if called beyond the end of `main`, when the Rust standard library's | |
/// This may panic if called beyond the end of `main`, if the Rust standard library's |
/// platforms can hit this. In general much of the standard library is not okay | ||
/// to use before or after `main`. |
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 isn't really the right place to mention this, as ChrisDenton said it would be good to have some module documentation (perhaps on the std top level module) explaining this and then linking to that section.
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 look for a spot. Is it fair to say 100% of libcore is fine to use before/after main, but the default assumption for libstd stuff like std::thread and std::io is it's UB to use before/after main, with the observable behavior being indistinguishable from a panic in the best case? How about liballoc?
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.
Some core and alloc functions could get poisoned through hooks like the global allocator or panic hooks.
I'll look for a spot.
Since this affects multiple modules maybe the top level?
the default assumption for libstd stuff like std::thread and std::io is it's UB to use before/after main, with the observable behavior being indistinguishable from a panic in the best case
That's a tough one, I guess it's true that there might be UB lurking somewhere but feels a bit like a "known to the state of california to cause cancer" statement.
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.
Technically there are some functions that are probably UB on Windows. In particular when you're running before main on Windows, the loader lock is likely held, and we may call into functions that are UB if the loader lock is helt (but in practice these are likely to be just a deadlock).
We historically have taken measures to reduce the damage of UB (usually by mitigating it into a "does not work" state) from doing stuff to std before main, as people do do it. I would certainly like to discourage it, 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.
with the observable behavior being indistinguishable from a panic in the best case
The best case is that it just works. But that depends on the platform and operation.
I filed #110708 with takeaways from the feedback above, and I'll close this PR since I agree this is not appropriate to document like this. |
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=978be7b8cb9d857f33f217ab4ff13eca