Skip to content

Commit dd9efd9

Browse files
authored
gpui: Fix send_file_drop_event holding mutex during event callback (zed-industries#50173)
`send_file_drop_event` was the only event dispatch site in window.rs that called the event callback while still holding the `MacWindowState` mutex. every other callback site in the file uses the established `take() / drop(lock) / callback()` pattern which releases the lock before invoking user code. this is a latent code quality issue: no deadlock occurs today because the current file-drop callback chain does not reenter `MacWindowState`'s mutex (`Window::mouse_position()` returns a cached field `Window::refresh()` only marks a dirty flag and `cx.activate()` uses a separate `MacPlatform` mutex). but `parking_lot::Mutex` is not entering so any future platform call added inside a filedrop handler would deadlock immediately and silently. the fix brings `send_file_drop_event` in line with the rest of the file by taking the callback out of the state dropping the lock invoking the callback then reacquiring the lock to restore the callback and update state. Release Notes: - N/A
1 parent 820774a commit dd9efd9

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

crates/gpui_macos/src/window.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,19 +2551,20 @@ fn send_file_drop_event(
25512551
window_state: Arc<Mutex<MacWindowState>>,
25522552
file_drop_event: FileDropEvent,
25532553
) -> bool {
2554-
let mut window_state = window_state.lock();
2555-
let window_event_callback = window_state.event_callback.as_mut();
2556-
if let Some(callback) = window_event_callback {
2557-
let external_files_dragged = match file_drop_event {
2558-
FileDropEvent::Entered { .. } => Some(true),
2559-
FileDropEvent::Exited => Some(false),
2560-
_ => None,
2561-
};
2554+
let external_files_dragged = match file_drop_event {
2555+
FileDropEvent::Entered { .. } => Some(true),
2556+
FileDropEvent::Exited => Some(false),
2557+
_ => None,
2558+
};
25622559

2560+
let mut lock = window_state.lock();
2561+
if let Some(mut callback) = lock.event_callback.take() {
2562+
drop(lock);
25632563
callback(PlatformInput::FileDrop(file_drop_event));
2564-
2564+
let mut lock = window_state.lock();
2565+
lock.event_callback = Some(callback);
25652566
if let Some(external_files_dragged) = external_files_dragged {
2566-
window_state.external_files_dragged = external_files_dragged;
2567+
lock.external_files_dragged = external_files_dragged;
25672568
}
25682569
true
25692570
} else {

0 commit comments

Comments
 (0)