Skip to content

Commit 4c99840

Browse files
authored
Unrolled build for #148687
Rollup merge of #148687 - joboet:nonpoison-hook-rwlock, r=ChrisDenton std: use a non-poisoning `RwLock` for the panic hook The code ignored poison errors using `PoisonError` anyway.
2 parents ab67c37 + 52fd48c commit 4c99840

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

library/std/src/panicking.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::io::try_set_output_capture;
2222
use crate::mem::{self, ManuallyDrop};
2323
use crate::panic::{BacktraceStyle, PanicHookInfo};
2424
use crate::sync::atomic::{Atomic, AtomicBool, Ordering};
25-
use crate::sync::{PoisonError, RwLock};
25+
use crate::sync::nonpoison::RwLock;
2626
use crate::sys::backtrace;
2727
use crate::sys::stdio::panic_output;
2828
use crate::{fmt, intrinsics, process, thread};
@@ -144,13 +144,9 @@ pub fn set_hook(hook: Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send>) {
144144
panic!("cannot modify the panic hook from a panicking thread");
145145
}
146146

147-
let new = Hook::Custom(hook);
148-
let mut hook = HOOK.write().unwrap_or_else(PoisonError::into_inner);
149-
let old = mem::replace(&mut *hook, new);
150-
drop(hook);
151-
// Only drop the old hook after releasing the lock to avoid deadlocking
152-
// if its destructor panics.
153-
drop(old);
147+
// Drop the old hook after changing the hook to avoid deadlocking if its
148+
// destructor panics.
149+
drop(HOOK.replace(Hook::Custom(hook)));
154150
}
155151

156152
/// Unregisters the current panic hook and returns it, registering the default hook
@@ -188,11 +184,7 @@ pub fn take_hook() -> Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send> {
188184
panic!("cannot modify the panic hook from a panicking thread");
189185
}
190186

191-
let mut hook = HOOK.write().unwrap_or_else(PoisonError::into_inner);
192-
let old_hook = mem::take(&mut *hook);
193-
drop(hook);
194-
195-
old_hook.into_box()
187+
HOOK.replace(Hook::Default).into_box()
196188
}
197189

198190
/// Atomic combination of [`take_hook`] and [`set_hook`]. Use this to replace the panic handler with
@@ -238,7 +230,7 @@ where
238230
panic!("cannot modify the panic hook from a panicking thread");
239231
}
240232

241-
let mut hook = HOOK.write().unwrap_or_else(PoisonError::into_inner);
233+
let mut hook = HOOK.write();
242234
let prev = mem::take(&mut *hook).into_box();
243235
*hook = Hook::Custom(Box::new(move |info| hook_fn(&prev, info)));
244236
}
@@ -822,7 +814,7 @@ fn panic_with_hook(
822814
crate::process::abort();
823815
}
824816

825-
match *HOOK.read().unwrap_or_else(PoisonError::into_inner) {
817+
match *HOOK.read() {
826818
// Some platforms (like wasm) know that printing to stderr won't ever actually
827819
// print anything, and if that's the case we can skip the default
828820
// hook. Since string formatting happens lazily when calling `payload`

0 commit comments

Comments
 (0)