From d4e0f73ffe0b6513322b0bb8f6652c067a97e5a0 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 13 Jul 2022 10:19:46 +0200 Subject: [PATCH 1/3] Drop window borrow before calling `makeKeyAndOrderFront` We're seeing some stack traces where calling `makeKeyAndOrderFront` could invoke `setFrameSize`, which is causing a double borrow. --- crates/gpui/src/platform/mac/window.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index 5e6b3b9c190fa6ff16a2cfee0c245a35fea0b444..294f169c45618d5b4c0c887c3df1d712378018e0 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -402,7 +402,8 @@ impl platform::Window for Window { } fn activate(&self) { - unsafe { msg_send![self.0.borrow().native_window, makeKeyAndOrderFront: nil] } + let window = self.0.borrow().native_window; + unsafe { msg_send![window, makeKeyAndOrderFront: nil] } } fn set_title(&mut self, title: &str) { From c53fa4941a00b6ee2abc81febaea8ff751725c81 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 13 Jul 2022 10:37:55 +0200 Subject: [PATCH 2/3] Ensure no borrows are held when activating window or creating prompts --- crates/gpui/src/platform/mac/window.rs | 27 ++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index 294f169c45618d5b4c0c887c3df1d712378018e0..ef892ec316469786a039e7ef5ec819bcfc4f473b 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -392,18 +392,33 @@ impl platform::Window for Window { }); let block = block.copy(); let native_window = self.0.borrow().native_window; - let _: () = msg_send![ - alert, - beginSheetModalForWindow: native_window - completionHandler: block - ]; + self.0 + .borrow() + .executor + .spawn(async move { + let _: () = msg_send![ + alert, + beginSheetModalForWindow: native_window + completionHandler: block + ]; + }) + .detach(); + done_rx } } fn activate(&self) { let window = self.0.borrow().native_window; - unsafe { msg_send![window, makeKeyAndOrderFront: nil] } + self.0 + .borrow() + .executor + .spawn(async move { + unsafe { + let _: () = msg_send![window, makeKeyAndOrderFront: nil]; + } + }) + .detach(); } fn set_title(&mut self, title: &str) { From 8e2e5b5cf097391b9a4c803922c90951e87b3926 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 13 Jul 2022 10:38:34 +0200 Subject: [PATCH 3/3] Don't borrow window state mutably until we need to invoke resize callback --- crates/gpui/src/platform/mac/window.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index ef892ec316469786a039e7ef5ec819bcfc4f473b..43ce09ffffe2bf01292c52da42b456c9a7b9baab 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -802,7 +802,7 @@ extern "C" fn view_did_change_backing_properties(this: &Object, _: Sel) { extern "C" fn set_frame_size(this: &Object, _: Sel, size: NSSize) { let window_state = unsafe { get_window_state(this) }; - let mut window_state_borrow = window_state.as_ref().borrow_mut(); + let window_state_borrow = window_state.as_ref().borrow(); if window_state_borrow.size() == vec2f(size.width as f32, size.height as f32) { return; @@ -822,6 +822,8 @@ extern "C" fn set_frame_size(this: &Object, _: Sel, size: NSSize) { let _: () = msg_send![window_state_borrow.layer, setDrawableSize: drawable_size]; } + drop(window_state_borrow); + let mut window_state_borrow = window_state.borrow_mut(); if let Some(mut callback) = window_state_borrow.resize_callback.take() { drop(window_state_borrow); callback();