Last active
February 23, 2023 21:13
-
-
Save Ciantic/e3d16d983262be3487e6b7c90152f2df to your computer and use it in GitHub Desktop.
Why this panics? "fatal runtime error: thread local panicked on drop"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MyThreadWrapper { | |
thread: Option<std::thread::JoinHandle<()>>, | |
} | |
impl Drop for MyThreadWrapper { | |
fn drop(&mut self) { | |
if let Some(thread) = self.thread.take() { | |
let _ = thread.join(); | |
} | |
} | |
} | |
thread_local! { | |
static THREAD2: MyThreadWrapper = | |
MyThreadWrapper { thread: Some(std::thread::spawn(|| { | |
println!("Does this work"); | |
}))}; | |
} | |
fn main() { | |
THREAD2.with(|_| { | |
}); | |
} | |
/* | |
THIS HAPPENS USUALLY, BUT NOT ALWAYS, I THINK THIS IS SOME RACE CONDITION: | |
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /rustc/f3126500f25114ba4e0ac3e76694dd45a22de56d\library\std\src\thread\mod.rs:1458:40 | |
stack backtrace: | |
0: std::panicking::begin_panic_handler | |
at /rustc/f3126500f25114ba4e0ac3e76694dd45a22de56d/library\std\src\panicking.rs:575 | |
1: core::panicking::panic_fmt | |
at /rustc/f3126500f25114ba4e0ac3e76694dd45a22de56d/library\core\src\panicking.rs:64 | |
2: core::panicking::panic | |
at /rustc/f3126500f25114ba4e0ac3e76694dd45a22de56d/library\core\src\panicking.rs:114 | |
3: std::thread::JoinHandle<T>::join | |
4: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once | |
5: std::thread::local::fast::Key<T>::try_initialize | |
6: std::sys::windows::thread_local_dtor::run_keyless_dtors | |
at /rustc/f3126500f25114ba4e0ac3e76694dd45a22de56d/library\std\src\sys\windows\thread_local_dtor.rs:28 | |
7: std::sys::windows::thread_local_key::on_tls_callback | |
at /rustc/f3126500f25114ba4e0ac3e76694dd45a22de56d/library\std\src\sys\windows\thread_local_key.rs:246 | |
8: RtlGetCurrentDirectory_U | |
9: RtlActivateActivationContextUnsafeFast | |
10: LdrShutdownThread | |
11: LdrShutdownProcess | |
12: RtlExitUserProcess | |
13: ExitProcess | |
14: exit | |
15: exit | |
16: __scrt_common_main_seh | |
at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:295 | |
17: BaseThreadInitThunk | |
18: RtlUserThreadStart | |
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. | |
fatal runtime error: thread local panicked on drop | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment