From d98ab4faf869ff0430ad73260b13ef8e473ef212 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 15 Apr 2015 12:27:05 -0700 Subject: std: Don't assume thread::current() works on panic Inspecting the current thread's info may not always work due to the TLS value having been destroyed (or is actively being destroyed). The code for printing a panic message assumed, however, that it could acquire the thread's name through this method. Instead this commit propagates the `Option` outwards to allow the `std::panicking` module to handle the case where the current thread isn't present. While it solves the immediate issue of #24313, there is still another underlying issue of panicking destructors in thread locals will abort the process. Closes #24313 --- src/libstd/sys/common/thread_info.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/libstd/sys/common') diff --git a/src/libstd/sys/common/thread_info.rs b/src/libstd/sys/common/thread_info.rs index 22cb5943371..ae55bae37aa 100644 --- a/src/libstd/sys/common/thread_info.rs +++ b/src/libstd/sys/common/thread_info.rs @@ -25,10 +25,9 @@ struct ThreadInfo { thread_local! { static THREAD_INFO: RefCell> = RefCell::new(None) } impl ThreadInfo { - fn with(f: F) -> R where F: FnOnce(&mut ThreadInfo) -> R { + fn with(f: F) -> Option where F: FnOnce(&mut ThreadInfo) -> R { if THREAD_INFO.state() == LocalKeyState::Destroyed { - panic!("Use of std::thread::current() is not possible after \ - the thread's local data has been destroyed"); + return None } THREAD_INFO.with(move |c| { @@ -38,16 +37,16 @@ impl ThreadInfo { thread: NewThread::new(None), }) } - f(c.borrow_mut().as_mut().unwrap()) + Some(f(c.borrow_mut().as_mut().unwrap())) }) } } -pub fn current_thread() -> Thread { +pub fn current_thread() -> Option { ThreadInfo::with(|info| info.thread.clone()) } -pub fn stack_guard() -> usize { +pub fn stack_guard() -> Option { ThreadInfo::with(|info| info.stack_guard) } -- cgit 1.4.1-3-g733a5