Skip to content

Commit cc4398e

Browse files
ejrhalice-i-ceciledloukadakis
authored
Avoid a "RefCell already borrowed" error with WINIT_WINDOWS (#21338)
# Objective Fixes #21319. ## Solution In the "about_to_wait" state of Bevy's winit event loop, we borrow WINIT_WINDOWS only to check the conditions for a redraw, and perform the call after that check is concluded. ## Testing Tested by confirming that the RefCell error no longer occurs in my application. In my application, the error occurred when the `bevy-egui-inspector` crate was used. I do not know whether the original reporter of #21319 was also using third-party Bevy plugins that might have caused the issue. It would be good if they could check whether this change fixes it for them. Tested on Windows 11, where the bug is known to occur. The code that was changed is only included in Windows builds so there is little need to test on other platforms. --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: Dimitrios Loukadakis <[email protected]>
1 parent 885dda7 commit cc4398e

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

crates/bevy_winit/src/state.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -469,24 +469,23 @@ impl<M: Message> ApplicationHandler<M> for WinitAppRunnerState<M> {
469469
// invisible window creation. https://github.com/bevyengine/bevy/issues/18027
470470
#[cfg(target_os = "windows")]
471471
{
472-
WINIT_WINDOWS.with_borrow(|winit_windows| {
473-
let headless = winit_windows.windows.is_empty();
474-
let exiting = self.app_exit.is_some();
475-
let reactive = matches!(self.update_mode, UpdateMode::Reactive { .. });
476-
let all_invisible = winit_windows
477-
.windows
478-
.iter()
479-
.all(|(_, w)| !w.is_visible().unwrap_or(false));
480-
if !exiting
481-
&& (self.startup_forced_updates > 0
482-
|| headless
483-
|| all_invisible
484-
|| reactive
485-
|| self.window_event_received)
486-
{
487-
self.redraw_requested(event_loop);
488-
}
489-
});
472+
fn headless_or_all_invisible() -> bool {
473+
WINIT_WINDOWS.with_borrow(|winit_windows| {
474+
winit_windows
475+
.windows
476+
.iter()
477+
.all(|(_, w)| !w.is_visible().unwrap_or(false))
478+
})
479+
}
480+
481+
if !self.app_exit.is_some()
482+
&& (self.startup_forced_updates > 0
483+
|| matches!(self.update_mode, UpdateMode::Reactive { .. })
484+
|| self.window_event_received
485+
|| headless_or_all_invisible())
486+
{
487+
self.redraw_requested(event_loop);
488+
}
490489
}
491490
}
492491

@@ -1039,10 +1038,10 @@ mod tests {
10391038
app.add_systems(
10401039
Update,
10411040
move |mut window: Single<(Entity, &mut Window)>,
1042-
mut window_backend_scale_factor_changed: MessageWriter<
1043-
WindowBackendScaleFactorChanged,
1044-
>,
1045-
mut window_scale_factor_changed: MessageWriter<WindowScaleFactorChanged>| {
1041+
mut window_backend_scale_factor_changed: MessageWriter<
1042+
WindowBackendScaleFactorChanged,
1043+
>,
1044+
mut window_scale_factor_changed: MessageWriter<WindowScaleFactorChanged>| {
10461045
react_to_scale_factor_change(
10471046
window.0,
10481047
&mut window.1,

0 commit comments

Comments
 (0)