Skip to content

Commit 5c8f1b8

Browse files
committed
Abort for GC thread panic (#221)
The code is mainly from `mmrk-ruby`. We hijack the Rust panic hook to make sure that if the GC threads panic, the process will be aborted. The current behavior is that when a GC thread panics, the process hangs and wait for that GC thread to finish its work. This should be ported to `master`.
1 parent e40e4ca commit 5c8f1b8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

mmtk/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,37 @@ macro_rules! early_return_for_current_gc {
181181
}
182182
};
183183
}
184+
185+
pub(crate) fn set_panic_hook() {
186+
let old_hook = std::panic::take_hook();
187+
188+
std::panic::set_hook(Box::new(move |panic_info| {
189+
if crate::collection::is_gc_thread() {
190+
eprintln!("ERROR: An MMTk GC thread panicked. This is a bug.");
191+
eprintln!("{panic_info}");
192+
193+
let bt = std::backtrace::Backtrace::capture();
194+
match bt.status() {
195+
std::backtrace::BacktraceStatus::Unsupported => {
196+
eprintln!("Backtrace is unsupported.")
197+
}
198+
std::backtrace::BacktraceStatus::Disabled => {
199+
eprintln!("Backtrace is disabled.");
200+
eprintln!(
201+
"run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
202+
);
203+
}
204+
std::backtrace::BacktraceStatus::Captured => {
205+
eprintln!("{bt}");
206+
}
207+
s => {
208+
eprintln!("Unknown backtrace status: {s:?}");
209+
}
210+
}
211+
212+
std::process::abort();
213+
} else {
214+
old_hook(panic_info);
215+
}
216+
}));
217+
}

0 commit comments

Comments
 (0)