Skip to content

Commit f76a1bb

Browse files
ejrhalice-i-ceciledloukadakis
authored andcommitted
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 6a3120f commit f76a1bb

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
@@ -466,24 +466,23 @@ impl<M: Message> ApplicationHandler<M> for WinitAppRunnerState<M> {
466466
// invisible window creation. https://github.com/bevyengine/bevy/issues/18027
467467
#[cfg(target_os = "windows")]
468468
{
469-
WINIT_WINDOWS.with_borrow(|winit_windows| {
470-
let headless = winit_windows.windows.is_empty();
471-
let exiting = self.app_exit.is_some();
472-
let reactive = matches!(self.update_mode, UpdateMode::Reactive { .. });
473-
let all_invisible = winit_windows
474-
.windows
475-
.iter()
476-
.all(|(_, w)| !w.is_visible().unwrap_or(false));
477-
if !exiting
478-
&& (self.startup_forced_updates > 0
479-
|| headless
480-
|| all_invisible
481-
|| reactive
482-
|| self.window_event_received)
483-
{
484-
self.redraw_requested(event_loop);
485-
}
486-
});
469+
fn headless_or_all_invisible() -> bool {
470+
WINIT_WINDOWS.with_borrow(|winit_windows| {
471+
winit_windows
472+
.windows
473+
.iter()
474+
.all(|(_, w)| !w.is_visible().unwrap_or(false))
475+
})
476+
}
477+
478+
if !self.app_exit.is_some()
479+
&& (self.startup_forced_updates > 0
480+
|| matches!(self.update_mode, UpdateMode::Reactive { .. })
481+
|| self.window_event_received
482+
|| headless_or_all_invisible())
483+
{
484+
self.redraw_requested(event_loop);
485+
}
487486
}
488487
}
489488

@@ -1023,10 +1022,10 @@ mod tests {
10231022
app.add_systems(
10241023
Update,
10251024
move |mut window: Single<(Entity, &mut Window)>,
1026-
mut window_backend_scale_factor_changed: MessageWriter<
1027-
WindowBackendScaleFactorChanged,
1028-
>,
1029-
mut window_scale_factor_changed: MessageWriter<WindowScaleFactorChanged>| {
1025+
mut window_backend_scale_factor_changed: MessageWriter<
1026+
WindowBackendScaleFactorChanged,
1027+
>,
1028+
mut window_scale_factor_changed: MessageWriter<WindowScaleFactorChanged>| {
10301029
react_to_scale_factor_change(
10311030
window.0,
10321031
&mut window.1,

0 commit comments

Comments
 (0)