Skip to content

Commit 3d04a33

Browse files
committed
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
1 parent a5e15f9 commit 3d04a33

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

src/libstd/panicking.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use any::Any;
1717
use cell::RefCell;
1818
use rt::{backtrace, unwind};
1919
use sys::stdio::Stderr;
20-
use thread;
20+
use sys_common::thread_info;
2121

2222
// Defined in this module instead of old_io::stdio so that the unwinding
2323
thread_local! {
@@ -35,8 +35,8 @@ pub fn on_panic(obj: &(Any+Send), file: &'static str, line: u32) {
3535
}
3636
};
3737
let mut err = Stderr::new();
38-
let thread = thread::current();
39-
let name = thread.name().unwrap_or("<unnamed>");
38+
let thread = thread_info::current_thread();
39+
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
4040
let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
4141
match prev {
4242
Some(mut stderr) => {

src/libstd/sys/common/thread_info.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ struct ThreadInfo {
2525
thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(None) }
2626

2727
impl ThreadInfo {
28-
fn with<R, F>(f: F) -> R where F: FnOnce(&mut ThreadInfo) -> R {
28+
fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
2929
if THREAD_INFO.state() == LocalKeyState::Destroyed {
30-
panic!("Use of std::thread::current() is not possible after \
31-
the thread's local data has been destroyed");
30+
return None
3231
}
3332

3433
THREAD_INFO.with(move |c| {
@@ -38,16 +37,16 @@ impl ThreadInfo {
3837
thread: NewThread::new(None),
3938
})
4039
}
41-
f(c.borrow_mut().as_mut().unwrap())
40+
Some(f(c.borrow_mut().as_mut().unwrap()))
4241
})
4342
}
4443
}
4544

46-
pub fn current_thread() -> Thread {
45+
pub fn current_thread() -> Option<Thread> {
4746
ThreadInfo::with(|info| info.thread.clone())
4847
}
4948

50-
pub fn stack_guard() -> usize {
49+
pub fn stack_guard() -> Option<usize> {
5150
ThreadInfo::with(|info| info.stack_guard)
5251
}
5352

src/libstd/sys/unix/stack_overflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ mod imp {
8181
// We're calling into functions with stack checks
8282
stack::record_sp_limit(0);
8383

84-
let guard = thread_info::stack_guard();
84+
let guard = thread_info::stack_guard().unwrap_or(0);
8585
let addr = (*info).si_addr as usize;
8686

8787
if guard == 0 || addr < guard - PAGE_SIZE || addr >= guard {

src/libstd/thread/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,9 @@ pub fn scoped<'a, T, F>(f: F) -> JoinGuard<'a, T> where
421421
/// Gets a handle to the thread that invokes it.
422422
#[stable(feature = "rust1", since = "1.0.0")]
423423
pub fn current() -> Thread {
424-
thread_info::current_thread()
424+
thread_info::current_thread().expect("use of std::thread::current() is not \
425+
possible after the thread's local \
426+
data has been destroyed")
425427
}
426428

427429
/// Cooperatively gives up a timeslice to the OS scheduler.

src/test/run-pass/issue-24313.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::thread;
12+
use std::env;
13+
use std::process::Command;
14+
15+
struct Handle(i32);
16+
17+
impl Drop for Handle {
18+
fn drop(&mut self) { panic!(); }
19+
}
20+
21+
thread_local!(static HANDLE: Handle = Handle(0));
22+
23+
fn main() {
24+
let args = env::args().collect::<Vec<_>>();
25+
if args.len() == 1 {
26+
let out = Command::new(&args[0]).arg("test").output().unwrap();
27+
assert!(!out.status.success());
28+
let stderr = std::str::from_utf8(&out.stderr).unwrap();
29+
assert!(stderr.contains("panicked at 'explicit panic'"),
30+
"bad failure message:\n{}\n", stderr);
31+
} else {
32+
// TLS dtors are not always run on process exit
33+
thread::spawn(|| {
34+
HANDLE.with(|h| {
35+
println!("{}", h.0);
36+
});
37+
}).join().unwrap();
38+
}
39+
}
40+

0 commit comments

Comments
 (0)