From 6b9e93ac6d612991d6ca87e0ffcedd108e075152 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 22 Apr 2022 15:01:19 -0700 Subject: [PATCH] Fix double-borrow crash by calling window activated callback asynchronously --- crates/gpui/src/platform/mac/window.rs | 44 +++++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index 67a9a0939fad7db1f6b65f7773039c8198f864b0..f887bf706ca1152f1694d56836235ff4bb741c70 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -614,22 +614,42 @@ extern "C" fn window_did_resize(this: &Object, _: Sel, _: id) { extern "C" fn window_did_become_key(this: &Object, _: Sel, _: id) { let window_state = unsafe { get_window_state(this) }; - let mut window_state_borrow = window_state.as_ref().borrow_mut(); - if let Some(mut callback) = window_state_borrow.activate_callback.take() { - drop(window_state_borrow); - callback(true); - window_state.borrow_mut().activate_callback = Some(callback); - }; + window_state + .as_ref() + .borrow() + .executor + .spawn({ + let window_state = window_state.clone(); + async move { + let mut window_state_borrow = window_state.as_ref().borrow_mut(); + if let Some(mut callback) = window_state_borrow.activate_callback.take() { + drop(window_state_borrow); + callback(true); + window_state.borrow_mut().activate_callback = Some(callback); + }; + } + }) + .detach(); } extern "C" fn window_did_resign_key(this: &Object, _: Sel, _: id) { let window_state = unsafe { get_window_state(this) }; - let mut window_state_borrow = window_state.as_ref().borrow_mut(); - if let Some(mut callback) = window_state_borrow.activate_callback.take() { - drop(window_state_borrow); - callback(false); - window_state.borrow_mut().activate_callback = Some(callback); - }; + window_state + .as_ref() + .borrow() + .executor + .spawn({ + let window_state = window_state.clone(); + async move { + let mut window_state_borrow = window_state.as_ref().borrow_mut(); + if let Some(mut callback) = window_state_borrow.activate_callback.take() { + drop(window_state_borrow); + callback(false); + window_state.borrow_mut().activate_callback = Some(callback); + }; + } + }) + .detach(); } extern "C" fn close_window(this: &Object, _: Sel) {