Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Fix RefCell panic in thermal/keyboard state callbacks (#49187)
morgankrey9eb6fc098aad
diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rsindex 4c7c68942ed..b0412b7d993 100644--- a/crates/gpui/src/app.rs+++ b/crates/gpui/src/app.rs@@ -732,12 +732,15 @@ impl App {let app = Rc::downgrade(&app);move || {if let Some(app) = app.upgrade() {- let cx = &mut app.borrow_mut();- cx.keyboard_layout = cx.platform.keyboard_layout();- cx.keyboard_mapper = cx.platform.keyboard_mapper();- cx.keyboard_layout_observers- .clone()- .retain(&(), move |callback| (callback)(cx));+ // Use try_borrow_mut because this callback can be called asynchronously+ // from macOS while the app is already borrowed during an update.+ if let Ok(mut cx) = app.try_borrow_mut() {+ cx.keyboard_layout = cx.platform.keyboard_layout();+ cx.keyboard_mapper = cx.platform.keyboard_mapper();+ cx.keyboard_layout_observers+ .clone()+ .retain(&(), move |callback| (callback)(&mut cx));+ }}}}));@@ -746,10 +749,13 @@ impl App {let app = Rc::downgrade(&app);move || {if let Some(app) = app.upgrade() {- let cx = &mut app.borrow_mut();- cx.thermal_state_observers- .clone()- .retain(&(), move |callback| (callback)(cx));+ // Use try_borrow_mut because this callback can be called asynchronously+ // from macOS while the app is already borrowed during an update.+ if let Ok(mut cx) = app.try_borrow_mut() {+ cx.thermal_state_observers+ .clone()+ .retain(&(), move |callback| (callback)(&mut cx));+ }}}}));