From 9a8e93aee684fdd7cd20f0e6752ad457fab058b8 Mon Sep 17 00:00:00 2001 From: Marc Espin Date: Mon, 6 Jul 2026 00:20:38 +0200 Subject: [PATCH 1/2] chore(winit): Switch from hooking to catch unwind --- crates/freya-winit/src/lib.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/crates/freya-winit/src/lib.rs b/crates/freya-winit/src/lib.rs index 32835f43a..14005df62 100644 --- a/crates/freya-winit/src/lib.rs +++ b/crates/freya-winit/src/lib.rs @@ -59,20 +59,6 @@ pub fn launch(mut launch_config: LaunchConfig) { }; use winit::event_loop::EventLoop; - #[cfg(all(not(debug_assertions), not(target_os = "android")))] - { - let previous_hook = std::panic::take_hook(); - std::panic::set_hook(Box::new(move |panic_info| { - rfd::MessageDialog::new() - .set_title("Fatal Error") - .set_description(&panic_info.to_string()) - .set_level(rfd::MessageLevel::Error) - .show(); - previous_hook(panic_info); - std::process::exit(1); - })); - } - let event_loop = launch_config.event_loop.take().unwrap_or_else(|| { EventLoop::::with_user_event() .build() @@ -181,5 +167,26 @@ pub fn launch(mut launch_config: LaunchConfig) { } } + #[cfg(all(not(debug_assertions), not(target_os = "android")))] + { + let run_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + event_loop.run_app(&mut renderer).unwrap(); + })); + if let Err(panic_payload) = run_result { + let description = panic_payload + .downcast_ref::<&str>() + .map(|message| message.to_string()) + .or_else(|| panic_payload.downcast_ref::().cloned()) + .unwrap_or_else(|| "The application panicked.".to_string()); + rfd::MessageDialog::new() + .set_title("Fatal Error") + .set_description(&description) + .set_level(rfd::MessageLevel::Error) + .show(); + std::process::exit(1); + } + } + + #[cfg(any(debug_assertions, target_os = "android"))] event_loop.run_app(&mut renderer).unwrap(); } From 54db5a9fc8a899f55f71e50239beb3673fcaf99a Mon Sep 17 00:00:00 2001 From: Marc Espin Date: Mon, 6 Jul 2026 22:28:06 +0200 Subject: [PATCH 2/2] tidy up --- crates/freya-winit/src/lib.rs | 48 +++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/crates/freya-winit/src/lib.rs b/crates/freya-winit/src/lib.rs index 14005df62..b3d39a902 100644 --- a/crates/freya-winit/src/lib.rs +++ b/crates/freya-winit/src/lib.rs @@ -48,7 +48,32 @@ pub mod tray { /// /// If a custom event loop was provided via [`LaunchConfig::with_event_loop`], it will be used. /// Otherwise a default one is created. -pub fn launch(mut launch_config: LaunchConfig) { +pub fn launch(launch_config: LaunchConfig) { + #[cfg(all(not(debug_assertions), not(target_os = "android")))] + { + let run_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + launch_inner(launch_config); + })); + if let Err(panic_payload) = run_result { + let description = panic_payload + .downcast_ref::<&str>() + .map(|message| message.to_string()) + .or_else(|| panic_payload.downcast_ref::().cloned()) + .unwrap_or_else(|| "The application panicked.".to_string()); + rfd::MessageDialog::new() + .set_title("Fatal Error") + .set_description(&description) + .set_level(rfd::MessageLevel::Error) + .show(); + std::process::exit(1); + } + } + + #[cfg(any(debug_assertions, target_os = "android"))] + launch_inner(launch_config); +} + +fn launch_inner(mut launch_config: LaunchConfig) { use std::collections::HashMap; use freya_core::integration::*; @@ -167,26 +192,5 @@ pub fn launch(mut launch_config: LaunchConfig) { } } - #[cfg(all(not(debug_assertions), not(target_os = "android")))] - { - let run_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - event_loop.run_app(&mut renderer).unwrap(); - })); - if let Err(panic_payload) = run_result { - let description = panic_payload - .downcast_ref::<&str>() - .map(|message| message.to_string()) - .or_else(|| panic_payload.downcast_ref::().cloned()) - .unwrap_or_else(|| "The application panicked.".to_string()); - rfd::MessageDialog::new() - .set_title("Fatal Error") - .set_description(&description) - .set_level(rfd::MessageLevel::Error) - .show(); - std::process::exit(1); - } - } - - #[cfg(any(debug_assertions, target_os = "android"))] event_loop.run_app(&mut renderer).unwrap(); }