@@ -22,7 +22,7 @@ use crate::io::try_set_output_capture;
2222use crate :: mem:: { self , ManuallyDrop } ;
2323use crate :: panic:: { BacktraceStyle , PanicHookInfo } ;
2424use crate :: sync:: atomic:: { Atomic , AtomicBool , Ordering } ;
25- use crate :: sync:: { PoisonError , RwLock } ;
25+ use crate :: sync:: nonpoison :: RwLock ;
2626use crate :: sys:: backtrace;
2727use crate :: sys:: stdio:: panic_output;
2828use 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