Skip to content

Commit 16dc750

Browse files
mockersfalice-i-cecilekfc35
authored
fix freezes on AppExit on macos (#23838)
# Objective - Fix #23313 - On macOS, sending `AppExit` sometimes freezes the app ## Solution - This was caused by a race condition between the main thread and the render thread both holding an `Arc` to the window - Make sure more things drop on the main thread in the correct order ## Testing - Ran the repro a few times, it used to freezes before, it doesn't after --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Kevin Chen <chen.kevin.f@gmail.com>
1 parent 3d04eeb commit 16dc750

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

crates/bevy_winit/src/system.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,19 @@ pub(crate) fn despawn_windows(
270270
}
271271
}
272272

273-
// On macOS, when exiting, we need to tell the rendering thread the windows are about to
274-
// close to ensure that they are dropped on the main thread. Otherwise, the app will hang.
273+
// On macOS, many things need to be dropped on the main thread, or the app will hang:
274+
// - notify the rendering thread the windows are about to close
275+
// - take the `WindowWrapper`s out of `WINIT_WINDOWS` and into the local `windows_to_drop`
275276
if !exit_event_reader.is_empty() {
276277
exit_event_reader.clear();
277-
for window in window_entities.iter() {
278-
closing_event_writer.write(WindowClosing { window });
279-
}
278+
WINIT_WINDOWS.with_borrow_mut(|winit_windows| {
279+
for window in window_entities.iter() {
280+
closing_event_writer.write(WindowClosing { window });
281+
if let Some(wrapper) = winit_windows.remove_window(window) {
282+
windows_to_drop.push(wrapper);
283+
}
284+
}
285+
});
280286
}
281287
}
282288

0 commit comments

Comments
 (0)