bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Querying Window with .single() makes app panic on close

Open rparrett opened this issue 3 years ago • 7 comments

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

rparrett avatar Jan 20 '23 03:01 rparrett

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.

alice-i-cecile avatar Jan 20 '23 03:01 alice-i-cecile

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

rparrett avatar Jan 20 '23 03:01 rparrett

As far as I can tell, this affects any example where Window is queried, and single is called on that query.

rparrett avatar Jan 20 '23 03:01 rparrett

Was this broken before the Windows As Entities PR as well?

alice-i-cecile avatar Jan 20 '23 04:01 alice-i-cecile

I had assumed that was the culprit, but I just checked and confirmed.

rparrett avatar Jan 20 '23 04:01 rparrett

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() }

Aceeri avatar Jan 20 '23 07:01 Aceeri

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.

dlras2 avatar Feb 05 '25 02:02 dlras2