Querying Window with .single() makes app panic on close
Bevy version
latest main
Relevant system information
AdapterInfo { name: "Apple M1 Max", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }
SystemInfo { os: "MacOS 13.1 ", kernel: "22.2.0", cpu: "Apple M1 Max", core_count: "10", memory: "64.0 GiB" }
What you did
cargo run --example bevymark- click x to close window
Also, here's a minimum reproducer:
use bevy::prelude::*;
fn main() {
App::new().add_plugins(DefaultPlugins).add_system(sys).run();
}
fn sys(query: Query<&Window>) {
let window = query.single();
}
What went wrong
thread 'Compute Task Pool (2)' panicked at 'called `Result::unwrap()` on an `Err` value: NoEntities("bevy_ecs::query::state::QueryState<&bevy_window::window::Window>")', examples/stress_tests/bevymark.rs:224:26
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2023-01-20T03:11:43.479485Z INFO bevy_window::system: No windows are open, exiting
thread 'Compute Task Pool (1)' panicked at 'called `Result::unwrap()` on an `Err` value: NoEntities("bevy_ecs::query::state::QueryState<&bevy_window::window::Window>")', examples/stress_tests/bevymark.rs:157:26
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /Users/me/src/bevy/crates/bevy_tasks/src/task_pool.rs:354:49
Does this only occur on bevymark? I've seen this happen for ages across virtually all apps, although I don't think it's consistent.
No, but it does seem to happen consistently with (so far)
- bevymark
- contributors
- mouse_grab
- parallel_query
- resizing
- scale_factor_override
- window_resizing
- window_settings
As far as I can tell, this affects any example where Window is queried, and single is called on that query.
Was this broken before the Windows As Entities PR as well?
I had assumed that was the culprit, but I just checked and confirmed.
Tbh I'm not entirely sure the best way to get around this, maybe just replacing
let mut window = windows.single_mut();
// ->
let mut window = if let Ok(window) = windows.get_single_mut() {
window
} else {
return;
};
Or maybe having a run condition on some systems of fn single_window(windows: Query<&Window>) -> bool { windows.get_single().is_ok() }
This seems to still be happening (or is happening again). E.g. the window_settings example is displaying this behavior where presumably close_when_requested appears to be despawning the primary window before toggle_theme, which panics when the window is closed.