Skip to content

Commit 27afffc

Browse files
authored
fix: panic hook failed to restore after being suppressed (#2734)
* fix: panic hook failed to restore after being suppressed * chore: add changeset
1 parent 38b0dd9 commit 27afffc

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

.changeset/honest-actors-smoke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rspack/binding": patch
3+
---
4+
5+
fix: panic hook failed to restore after being suppressed

crates/rspack_error/src/catch_unwind.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,26 @@ pub mod PanicStrategy {
4343

4444
#[inline]
4545
fn panic_hook_handler<S: PanicStrategy::S, R>(f: impl FnOnce() -> R) -> R {
46-
PANIC_HOOK.with(|hook| {
47-
if !S::is_suppressed() {
48-
*hook.borrow_mut() = Some(std::panic::take_hook());
49-
}
50-
});
46+
let prev = std::panic::take_hook();
5147
std::panic::set_hook(Box::new(move |info| {
52-
PANIC_HOOK.with(|hook| {
53-
if let Some(hook) = &*hook.borrow() {
54-
hook(info);
55-
}
56-
});
5748
PANIC_INFO_AND_BACKTRACE.with(|bt| {
5849
*bt.borrow_mut() = Some((
5950
info.to_string(),
6051
std::backtrace::Backtrace::force_capture().to_string(),
6152
));
6253
});
54+
if !S::is_suppressed() {
55+
prev(info);
56+
}
6357
}));
6458
let result = f();
65-
PANIC_HOOK.with(|hook| {
66-
if let Some(hook) = hook.borrow_mut().take() {
67-
std::panic::set_hook(hook);
68-
}
69-
});
59+
let _ = std::panic::take_hook();
7060

7161
result
7262
}
7363

74-
type PanicHook = Box<dyn Fn(&std::panic::PanicInfo<'_>) + 'static + Sync + Send>;
75-
7664
thread_local! {
7765
static PANIC_INFO_AND_BACKTRACE: RefCell<Option<(String, String)>> = RefCell::new(None);
78-
static PANIC_HOOK: RefCell<Option<PanicHook>> = RefCell::new(None);
7966
}
8067

8168
pub fn catch_unwind<S: PanicStrategy::S, R>(f: impl FnOnce() -> R) -> Result<R> {

0 commit comments

Comments
 (0)