Skip to content

Commit 24bb607

Browse files
committed
Auto merge of #32282 - sfackler:panic-hook, r=alexcrichton
Adjustments to the panic hook API Rename `set_handler` and `take_handler` to `set_hook` and `take_hook` since we're not actually "handling" (i.e. fixing) anything. Also alter `set_hook` to take a `Box<Fn(&PanicInfo) + 'static + Sync + Send>` rather than a parameterized closure since there's otherwise no easy way to re-register a hook that came from `take_hook`. cc #30449 r? @aturon
2 parents 235d774 + 50fda1e commit 24bb607

File tree

5 files changed

+67
-55
lines changed

5 files changed

+67
-55
lines changed

src/libstd/panic.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ use sync::{Arc, Mutex, RwLock};
2323
use sys_common::unwind;
2424
use thread::Result;
2525

26-
pub use panicking::{take_handler, set_handler, PanicInfo, Location};
26+
pub use panicking::{take_hook, set_hook, PanicInfo, Location};
27+
28+
///
29+
#[rustc_deprecated(since = "1.9.0", reason = "renamed to set_hook")]
30+
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
31+
pub fn set_handler<F>(handler: F) where F: Fn(&PanicInfo) + 'static + Sync + Send {
32+
set_hook(Box::new(handler))
33+
}
34+
35+
///
36+
#[rustc_deprecated(since = "1.9.0", reason = "renamed to take_hook")]
37+
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
38+
pub fn take_handler() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
39+
take_hook()
40+
}
2741

2842
/// A marker trait which represents "panic safe" types in Rust.
2943
///

src/libstd/panicking.rs

+30-32
Original file line numberDiff line numberDiff line change
@@ -32,73 +32,71 @@ thread_local! {
3232
}
3333

3434
#[derive(Copy, Clone)]
35-
enum Handler {
35+
enum Hook {
3636
Default,
3737
Custom(*mut (Fn(&PanicInfo) + 'static + Sync + Send)),
3838
}
3939

40-
static HANDLER_LOCK: StaticRwLock = StaticRwLock::new();
41-
static mut HANDLER: Handler = Handler::Default;
40+
static HOOK_LOCK: StaticRwLock = StaticRwLock::new();
41+
static mut HOOK: Hook = Hook::Default;
4242
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
4343

44-
/// Registers a custom panic handler, replacing any that was previously
45-
/// registered.
44+
/// Registers a custom panic hook, replacing any that was previously registered.
4645
///
47-
/// The panic handler is invoked when a thread panics, but before it begins
48-
/// unwinding the stack. The default handler prints a message to standard error
46+
/// The panic hook is invoked when a thread panics, but before it begins
47+
/// unwinding the stack. The default hook prints a message to standard error
4948
/// and generates a backtrace if requested, but this behavior can be customized
50-
/// with the `set_handler` and `take_handler` functions.
49+
/// with the `set_hook` and `take_hook` functions.
5150
///
52-
/// The handler is provided with a `PanicInfo` struct which contains information
51+
/// The hook is provided with a `PanicInfo` struct which contains information
5352
/// about the origin of the panic, including the payload passed to `panic!` and
5453
/// the source code location from which the panic originated.
5554
///
56-
/// The panic handler is a global resource.
55+
/// The panic hook is a global resource.
5756
///
5857
/// # Panics
5958
///
6059
/// Panics if called from a panicking thread.
6160
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
62-
pub fn set_handler<F>(handler: F) where F: Fn(&PanicInfo) + 'static + Sync + Send {
61+
pub fn set_hook(hook: Box<Fn(&PanicInfo) + 'static + Sync + Send>) {
6362
if thread::panicking() {
64-
panic!("cannot modify the panic handler from a panicking thread");
63+
panic!("cannot modify the panic hook from a panicking thread");
6564
}
6665

67-
let handler = Box::new(handler);
6866
unsafe {
69-
let lock = HANDLER_LOCK.write();
70-
let old_handler = HANDLER;
71-
HANDLER = Handler::Custom(Box::into_raw(handler));
67+
let lock = HOOK_LOCK.write();
68+
let old_hook = HOOK;
69+
HOOK = Hook::Custom(Box::into_raw(hook));
7270
drop(lock);
7371

74-
if let Handler::Custom(ptr) = old_handler {
72+
if let Hook::Custom(ptr) = old_hook {
7573
Box::from_raw(ptr);
7674
}
7775
}
7876
}
7977

80-
/// Unregisters the current panic handler, returning it.
78+
/// Unregisters the current panic hook, returning it.
8179
///
82-
/// If no custom handler is registered, the default handler will be returned.
80+
/// If no custom hook is registered, the default hook will be returned.
8381
///
8482
/// # Panics
8583
///
8684
/// Panics if called from a panicking thread.
8785
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
88-
pub fn take_handler() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
86+
pub fn take_hook() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
8987
if thread::panicking() {
90-
panic!("cannot modify the panic handler from a panicking thread");
88+
panic!("cannot modify the panic hook from a panicking thread");
9189
}
9290

9391
unsafe {
94-
let lock = HANDLER_LOCK.write();
95-
let handler = HANDLER;
96-
HANDLER = Handler::Default;
92+
let lock = HOOK_LOCK.write();
93+
let hook = HOOK;
94+
HOOK = Hook::Default;
9795
drop(lock);
9896

99-
match handler {
100-
Handler::Default => Box::new(default_handler),
101-
Handler::Custom(ptr) => {Box::from_raw(ptr)} // FIXME #30530
97+
match hook {
98+
Hook::Default => Box::new(default_hook),
99+
Hook::Custom(ptr) => {Box::from_raw(ptr)} // FIXME #30530
102100
}
103101
}
104102
}
@@ -151,7 +149,7 @@ impl<'a> Location<'a> {
151149
}
152150
}
153151

154-
fn default_handler(info: &PanicInfo) {
152+
fn default_hook(info: &PanicInfo) {
155153
let panics = PANIC_COUNT.with(|s| s.get());
156154

157155
// If this is a double panic, make sure that we print a backtrace
@@ -224,10 +222,10 @@ pub fn on_panic(obj: &(Any+Send), file: &'static str, line: u32) {
224222
};
225223

226224
unsafe {
227-
let _lock = HANDLER_LOCK.read();
228-
match HANDLER {
229-
Handler::Default => default_handler(&info),
230-
Handler::Custom(ptr) => (*ptr)(&info),
225+
let _lock = HOOK_LOCK.read();
226+
match HOOK {
227+
Hook::Default => default_hook(&info),
228+
Hook::Custom(ptr) => (*ptr)(&info),
231229
}
232230
}
233231

src/test/run-pass/panic-handler-chain.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ static A: AtomicUsize = AtomicUsize::new(0);
1717
static B: AtomicUsize = AtomicUsize::new(0);
1818

1919
fn main() {
20-
panic::set_handler(|_| { A.fetch_add(1, Ordering::SeqCst); });
21-
let handler = panic::take_handler();
22-
panic::set_handler(move |info| {
20+
panic::set_hook(Box::new(|_| { A.fetch_add(1, Ordering::SeqCst); }));
21+
let hook = panic::take_hook();
22+
panic::set_hook(Box::new(move |info| {
2323
B.fetch_add(1, Ordering::SeqCst);
24-
handler(info);
25-
});
24+
hook(info);
25+
}));
2626

2727
let _ = thread::spawn(|| {
2828
panic!();

src/test/run-pass/panic-handler-flail-wildly.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ use std::panic;
1515
use std::thread;
1616

1717
fn a() {
18-
panic::set_handler(|_| println!("hello yes this is a"));
19-
panic::take_handler();
20-
panic::set_handler(|_| println!("hello yes this is a part 2"));
21-
panic::take_handler();
18+
panic::set_hook(Box::new(|_| println!("hello yes this is a")));
19+
panic::take_hook();
20+
panic::set_hook(Box::new(|_| println!("hello yes this is a part 2")));
21+
panic::take_hook();
2222
}
2323

2424
fn b() {
25-
panic::take_handler();
26-
panic::take_handler();
27-
panic::take_handler();
28-
panic::take_handler();
29-
panic::take_handler();
25+
panic::take_hook();
26+
panic::take_hook();
27+
panic::take_hook();
28+
panic::take_hook();
29+
panic::take_hook();
3030
panic!();
3131
}
3232

3333
fn c() {
34-
panic::set_handler(|_| ());
35-
panic::set_handler(|_| ());
36-
panic::set_handler(|_| ());
37-
panic::set_handler(|_| ());
38-
panic::set_handler(|_| ());
39-
panic::set_handler(|_| ());
34+
panic::set_hook(Box::new(|_| ()));
35+
panic::set_hook(Box::new(|_| ()));
36+
panic::set_hook(Box::new(|_| ()));
37+
panic::set_hook(Box::new(|_| ()));
38+
panic::set_hook(Box::new(|_| ()));
39+
panic::set_hook(Box::new(|_| ()));
4040
panic!();
4141
}
4242

src/test/run-pass/panic-handler-set-twice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use std::thread;
1818
static A: AtomicUsize = AtomicUsize::new(0);
1919

2020
fn main() {
21-
panic::set_handler(|_| ());
22-
panic::set_handler(|info| { A.fetch_add(1, Ordering::SeqCst); });
21+
panic::set_hook(Box::new(|_| ()));
22+
panic::set_hook(Box::new(|info| { A.fetch_add(1, Ordering::SeqCst); }));
2323

2424
let _ = thread::spawn(|| {
2525
panic!();

0 commit comments

Comments
 (0)