Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Defer native window close cleanup to avoid reentrant app borrow (#66181)
Anthony Eid370591f3d178
diff --git a/crates/gpui/src/platform/test/window.rs b/crates/gpui/src/platform/test/window.rsindex 5692b1e7cb8..4a8ccc67ee4 100644--- a/crates/gpui/src/platform/test/window.rs+++ b/crates/gpui/src/platform/test/window.rs@@ -30,6 +30,7 @@ pub(crate) struct TestWindowState {sprite_atlas: Arc<dyn PlatformAtlas>,renderer: Option<Box<dyn PlatformHeadlessRenderer>>,pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,+ close_callback: Option<Box<dyn FnOnce()>>,hit_test_window_control_callback: Option<Box<dyn FnMut() -> Option<WindowControlArea>>>,input_callback: Option<Box<dyn FnMut(PlatformInput) -> DispatchEventResult>>,active_status_change_callback: Option<Box<dyn FnMut(bool)>>,@@ -104,6 +105,7 @@ impl TestWindow {edited: false,document_path: None,should_close_handler: None,+ close_callback: None,hit_test_window_control_callback: None,input_callback: None,active_status_change_callback: None,@@ -135,6 +137,13 @@ impl TestWindow {start_external_drag_result: false,})))}+ pub fn simulate_close(&self) {+ let callback = self.0.lock().close_callback.take();+ if let Some(callback) = callback {+ callback();+ }+ }+pub fn simulate_scheduled_frame(&self) -> bool {let callback = {let mut state = self.0.lock();@@ -534,7 +543,9 @@ impl PlatformWindow for TestWindow {self.0.lock().should_close_handler = Some(callback);}- fn on_close(&self, _callback: Box<dyn FnOnce()>) {}+ fn on_close(&self, callback: Box<dyn FnOnce()>) {+ self.0.lock().close_callback = Some(callback);+ }fn on_hit_test_window_control(&self, callback: Box<dyn FnMut() -> Option<WindowControlArea>>) {self.0.lock().hit_test_window_control_callback = Some(callback);diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rsindex ad0e7e067cf..a3b1b1b66c7 100644--- a/crates/gpui/src/window.rs+++ b/crates/gpui/src/window.rs@@ -1761,12 +1761,22 @@ impl Window {platform_window.on_close(Box::new({let window_id = handle.window_id();- let mut cx = cx.to_async();+ let cx = cx.to_async();move || {- let _ = handle.update(&mut cx, |_, window, _| window.remove_window());- let _ = cx.update(|cx| {- SystemWindowTabController::remove_tab(cx, window_id);- });+ // Native destruction can re-enter while the app is borrowed.+ // Schedule both operations without borrowing the app here.+ cx.spawn(async move |cx| {+ cx.update(|cx| {+ // App-driven closure may already have removed the window.+ if cx.windows.contains_key(window_id) {+ handle+ .update(cx, |_, window, _| window.remove_window())+ .log_err();+ }+ SystemWindowTabController::remove_tab(cx, window_id);+ });+ })+ .detach();}}));platform_window.on_request_frame(Box::new({@@ -8022,6 +8032,19 @@ mod tests {.expect("window exists");}+ #[gpui::test]+ fn test_native_close_defers_app_access(cx: &mut TestAppContext) {+ let window = cx.add_window(|_, _| EmptyView);+ let platform_window = cx.test_window(window.into());+ {+ let app = cx.app.borrow_mut();+ platform_window.simulate_close();+ assert!(app.windows.contains_key(window.window_id()));+ }+ cx.run_until_parked();+ assert!(!cx.app.borrow().windows.contains_key(window.window_id()));+ }+#[cfg(feature = "profiler")]#[gpui::test]fn test_frame_requests_and_skips_are_journaled(cx: &mut TestAppContext) {