-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
println! sometimes panics in drop methods #29488
Comments
The issue here is that we initialise TLS variables lazily (AFAIR), but they still get dropped in order (of initialisation). If you happen to initialise your droppable type before whatever A simple solution would be to somehow ensure that types which depend on TLS in their |
Currently if a print happens while a thread is being torn down it may cause a panic if the LOCAL_STDOUT TLS slot has been destroyed by that point. This adds a guard to check and prints to the process stdout if that's the case (as we do for if the slot is already borrowed). Closes rust-lang#29488
Sounds bad! I think we just need to add a check to see if TLS is already destroyed: #29491 |
@nagisa I am afraid that we do not have any kind of guarantee about the order in which TLS variables are destroyed. @alexcrichton We might also want to sanitise std to ensure that this issue does not affect other code. This has already been done for stderr and thread_info and it is now being done to stdout. |
That's a good point about an audit! We probably don't need to worry much about rand as it's not an exported interface of the standard library, however, but probably good to fixup nonetheless! |
Currently if a print happens while a thread is being torn down it may cause a panic if the LOCAL_STDOUT TLS slot has been destroyed by that point. This adds a guard to check and prints to the process stdout if that's the case (as we do for if the slot is already borrowed). Closes #29488
If I improve the test case by adding:
(rustc 1.23.0) It's the same if I add another FOO2 : Foo TLS and access it from main in the same way. My question is, shouldn't |
There is another case when applications panic while using drop/TLS/println:
This code output is:
My point of view is it also must be fixed in Rust language. rustc 1.26.0-nightly (adf2135 2018-03-17) |
Seems like this is happening again? |
We don't control the TLS implementation unfortunately. |
Bug is critical as rust is panic. I think the |
Thanks. |
Assigning P-medium, would be great to fix but not super-high-priority |
@alexcrichton Due to this bug I see no way to create libraries with TLS-variables that needs a destructor. |
Why do you need to print in the destructor of your type? |
Due to debugging and logging needs. |
I think a fact that drop trait is not working is not a println! issue. Without println! TLS-drop don't work as expected too. Windows7x64 |
Let's see an example without println!
But it write only:
main.rs:
Project1.cpp:
|
Some platforms do not run TLS destructors on the main thread. We have no control over that behavior. |
In my C++ project on this platform I freely used TLS variables destructors in main thread without errors. |
@DustinByfuglien that is #28129, not this issue. |
Yes it seems that issue can be related with this, as I marked before |
No, they are totally separate issues. This issue is "something bad happens when TLS destructors run", and the other issue is "TLS destructors don't run on the main thread". The only thing that's similar between them is that they both involve TLS. |
Both issues are platform-depending. I will not wonder if this issues may have a one internal cause. |
I have a peculiar use case here in that the test framework (cargo test) will only capture the output from the primary test thread, so we're gathering the logs from the other threads and then emit them (via the BTW, adding |
I tried this: std::panic::catch_unwind(|| {
println!("Something");
}); That doesn't work for me. I need to remove all |
If you try to println!-debug a drop method in a type which is being used in TLS, you can have problems because the TLS variable which holds the local stdout might already have been destroyed, turning your innocent print into a panic with "cannot access a TLS value during or after it is destroyed". This seems unnecessarily user-hostile.
(I solved the bug in my code, but I'm kinda curious what the recommended way to trace a drop method is. rt::util::dumb_print?)
The text was updated successfully, but these errors were encountered: