Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Disable panic hooks in Release mode #889

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions godot-core/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ pub fn is_class_runtime(is_tool: bool) -> bool {
// ----------------------------------------------------------------------------------------------------------------------------------------------
// Panic handling

#[cfg(debug_assertions)]
#[derive(Debug)]
struct GodotPanicInfo {
line: u32,
Expand Down Expand Up @@ -328,12 +329,15 @@ where
F: FnOnce() -> R + std::panic::UnwindSafe,
S: std::fmt::Display,
{
#[cfg(debug_assertions)]
let info: Arc<Mutex<Option<GodotPanicInfo>>> = Arc::new(Mutex::new(None));

// Back up previous hook, set new one
let prev_hook = std::panic::take_hook();
{
// Back up previous hook, set new one.
#[cfg(debug_assertions)]
let prev_hook = {
let info = info.clone();
let prev_hook = std::panic::take_hook();

std::panic::set_hook(Box::new(move |panic_info| {
if let Some(location) = panic_info.location() {
*info.lock().unwrap() = Some(GodotPanicInfo {
Expand All @@ -345,30 +349,38 @@ where
eprintln!("panic occurred, but can't get location information");
}
}));
}

// Run code that should panic, restore hook
prev_hook
};

// Run code that should panic, restore hook.
let panic = std::panic::catch_unwind(code);

// Restore the previous panic hook if in Debug mode.
#[cfg(debug_assertions)]
std::panic::set_hook(prev_hook);

match panic {
Ok(result) => Ok(result),
Err(err) => {
// Flush, to make sure previous Rust output (e.g. test announcement, or debug prints during app) have been printed
// TODO write custom panic handler and move this there, before panic backtrace printing
// TODO write custom panic handler and move this there, before panic backtrace printing.
flush_stdout();

let guard = info.lock().unwrap();
let info = guard.as_ref().expect("no panic info available");

if print {
godot_error!(
"Rust function panicked at {}:{}.\n Context: {}",
info.file,
info.line,
error_context()
);
//eprintln!("Backtrace:\n{}", info.backtrace);
// Handle panic info only in Debug mode.
#[cfg(debug_assertions)]
{
let guard = info.lock().unwrap();
let info = guard.as_ref().expect("no panic info available");
if print {
godot_error!(
"Rust function panicked at {}:{}.\n Context: {}",
info.file,
info.line,
error_context()
);
//eprintln!("Backtrace:\n{}", info.backtrace);
}
}

let msg = extract_panic_message(err);
Expand Down
Loading