Fix missing crash dialog for panics thrown inside requestAnimationFrame loop (#3788)

* add crash dialog in RAF path

* removed panic!()

* removed redundant string allocationa dn duplication between function

* fixed allocation

* removed bloated code
This commit is contained in:
Jill Chhagnani
2026-02-22 17:27:23 +00:00
committed by GitHub
parent 0531769c41
commit 9d83998463
2 changed files with 72 additions and 15 deletions
+58 -4
View File
@@ -24,6 +24,7 @@ thread_local! {
pub static EDITOR: Mutex<Option<editor::application::Editor>> = const { Mutex::new(None) };
pub static MESSAGE_BUFFER: std::cell::RefCell<Vec<Message>> = const { std::cell::RefCell::new(Vec::new()) };
pub static EDITOR_HANDLE: Mutex<Option<editor_api::EditorHandle>> = const { Mutex::new(None) };
pub static PANIC_DIALOG_MESSAGE_CALLBACK: std::cell::RefCell<Option<js_sys::Function>> = const { std::cell::RefCell::new(None) };
}
/// Initialize the backend
@@ -72,12 +73,65 @@ pub fn panic_hook(info: &panic::PanicHookInfo) {
log::error!("{info}");
EDITOR_HANDLE.with(|editor_handle| {
let mut guard = editor_handle.lock();
if let Ok(Some(handle)) = guard.as_deref_mut() {
handle.send_frontend_message_to_js_rust_proxy(FrontendMessage::DisplayDialogPanic { panic_info: info.to_string() });
// Prefer using the raw JS callback to avoid mutex lock contention inside the panic hook.
if let Err(info) = send_panic_dialog_via_callback(info) {
send_panic_dialog_deferred(info);
}
}
fn send_panic_dialog_via_callback(panic_info: String) -> Result<(), String> {
let message = FrontendMessage::DisplayDialogPanic { panic_info };
let message_type = message.to_discriminant().local_name();
let Ok(message_data) = serde_wasm_bindgen::to_value(&message) else {
log::error!("Failed to serialize crash dialog panic message");
let FrontendMessage::DisplayDialogPanic { panic_info } = message else {
unreachable!("Message variant changed unexpectedly")
};
return Err(panic_info);
};
PANIC_DIALOG_MESSAGE_CALLBACK.with(|callback| {
let callback_ref = callback.borrow();
let Some(callback) = callback_ref.as_ref() else {
let FrontendMessage::DisplayDialogPanic { panic_info } = message else {
unreachable!("Message variant changed unexpectedly")
};
return Err(panic_info);
};
if let Err(error) = callback.call2(&JsValue::null(), &JsValue::from(message_type), &message_data) {
log::error!("Failed to send crash dialog panic message to JS: {:?}", error);
let FrontendMessage::DisplayDialogPanic { panic_info } = message else {
unreachable!("Message variant changed unexpectedly")
};
return Err(panic_info);
}
Ok(())
})
}
#[cfg(not(feature = "native"))]
fn send_panic_dialog_deferred(panic_info: String) {
let callback = Closure::once_into_js(move || {
if send_panic_dialog_via_callback(panic_info).is_err() {
log::error!("Failed to send crash dialog after panic because the editor handle is unavailable");
}
});
let Some(window) = web_sys::window() else {
log::error!("Failed to schedule crash dialog after panic because no window exists");
return;
};
if window.set_timeout_with_callback_and_timeout_and_arguments_0(callback.unchecked_ref(), 0).is_err() {
log::error!("Failed to schedule crash dialog after panic with setTimeout");
}
}
#[cfg(feature = "native")]
fn send_panic_dialog_deferred(_panic_info: String) {
// Native builds do not use `setTimeout`, so just log the failure in the caller's context.
}
#[wasm_bindgen]