Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ScheduleRunnerPlugin after default plugin crashes at startup #221

Closed
lukasschlueter opened this issue Aug 17, 2020 · 4 comments · Fixed by #444
Closed

Adding ScheduleRunnerPlugin after default plugin crashes at startup #221

lukasschlueter opened this issue Aug 17, 2020 · 4 comments · Fixed by #444
Labels
C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash

Comments

@lukasschlueter
Copy link

lukasschlueter commented Aug 17, 2020

Thanks for your work!

I've been experimenting a bit and stumbled across the ScheduleRunnerPlugin. However, when the plugin is added after the default plugins, it crashes on startup. When adding the default plugins after the ScheduleRunnerPlugin, it works fine.

Minimal example:

use std::time::Duration;

use bevy::prelude::*;

fn main() {
    App::build()
        .add_default_plugins()
        .add_plugin(bevy::app::ScheduleRunnerPlugin::run_loop(
            Duration::from_secs_f64(1.0 / 60.0),
        ))
        .run();
}

Fails with:

Short Backtrace
C:/Users/Lukas/.cargo/bin/cargo.exe run --color=always --package pong --bin crash
   Compiling pong v0.1.0 (D:\Lukas\Documents\Rust\GD50\pong)
    Finished dev [unoptimized + debuginfo] target(s) in 4.77s
     Running `target\debug\crash.exe`
thread 'main' panicked at 'Resource does not exist bevy_input::keyboard::KeyboardInputState', C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resources.rs:157:32
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:475
   1: std::panicking::begin_panic_fmt
             at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:429
   2: bevy_ecs::resource::resources::{{impl}}::get_unsafe_ref::{{closure}}<bevy_input::keyboard::KeyboardInputState>
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resources.rs:157
   3: core::option::Option<core::ptr::non_null::NonNull<bevy_input::keyboard::KeyboardInputState>>::unwrap_or_else
             at C:\Users\Lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\option.rs:409
   4: bevy_ecs::resource::resources::Resources::get_unsafe_ref
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resources.rs:144
   5: bevy_ecs::resource::resource_query::{{impl}}::get<bevy_input::keyboard::KeyboardInputState>
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resource_query.rs:221
   6: bevy_ecs::resource::resource_query::{{impl}}::get
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resource_query.rs:255
   7: bevy_ecs::resource::resources::Resources::query_system
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resources.rs:139
   8: bevy_ecs::system::into_system::{{impl}}::system::{{closure}}
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\system\into_system.rs:178
   9: bevy_ecs::system::into_system::{{impl}}::run<bevy_ecs::system::into_system::QuerySystemState,closure-0,closure-1,closure-2,closure-3>
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\system\into_system.rs:60
  10: bevy_ecs::schedule::schedule::Schedule::run
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\schedule\schedule.rs:139
  11: bevy_app::schedule_runner::{{impl}}::build::{{closure}}
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_app\src\schedule_runner.rs:60
  12: alloc::boxed::{{impl}}::call
             at C:\Users\Lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\alloc\src\boxed.rs:1039
  13: bevy_app::app::App::run
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_app\src\app.rs:73
  14: bevy_app::app_builder::AppBuilder::run
             at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_app\src\app_builder.rs:43
  15: crash::main
             at .\src\crash.rs:6
  16: core::ops::function::FnOnce::call_once<fn(),tuple<>>
             at C:\Users\Lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:233
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Full backtrace
C:/Users/Lukas/.cargo/bin/cargo.exe run --color=always --package pong --bin crash
    Finished dev [unoptimized + debuginfo] target(s) in 0.85s
     Running `target\debug\crash.exe`
thread 'main' panicked at 'Resource does not exist bevy_input::keyboard::KeyboardInputState', C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resources.rs:157:32
stack backtrace:
   0:     0x7ff6ec2ca6e9 - std::backtrace_rs::backtrace::dbghelp::trace
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\..\..\backtrace\src\backtrace\dbghelp.rs:98
   1:     0x7ff6ec2ca6e9 - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ff6ec2ca6e9 - std::sys_common::backtrace::_print_fmt
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\sys_common\backtrace.rs:79
   3:     0x7ff6ec2ca6e9 - std::sys_common::backtrace::_print::{{impl}}::fmt
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\sys_common\backtrace.rs:58
   4:     0x7ff6ec2e180c - core::fmt::write
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\core\src\fmt\mod.rs:1117
   5:     0x7ff6ec2c57ec - std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\io\mod.rs:1510
   6:     0x7ff6ec2cd61b - std::sys_common::backtrace::_print
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\sys_common\backtrace.rs:61
   7:     0x7ff6ec2cd61b - std::sys_common::backtrace::print
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\sys_common\backtrace.rs:48
   8:     0x7ff6ec2cd61b - std::panicking::default_hook::{{closure}}
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:200
   9:     0x7ff6ec2cd268 - std::panicking::default_hook
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:219
  10:     0x7ff6ec2cde0f - std::panicking::rust_panic_with_hook
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:569
  11:     0x7ff6ec2cd975 - std::panicking::begin_panic_handler::{{closure}}
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:476
  12:     0x7ff6ec2caf9f - std::sys_common::backtrace::__rust_end_short_backtrace<closure-0,!>
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\sys_common\backtrace.rs:153
  13:     0x7ff6ec2cd929 - std::panicking::begin_panic_handler
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:475
  14:     0x7ff6ec2cd8dc - std::panicking::begin_panic_fmt
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:429
  15:     0x7ff6ec913ac6 - bevy_ecs::resource::resources::{{impl}}::get_unsafe_ref::{{closure}}<bevy_input::keyboard::KeyboardInputState>
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resources.rs:157
  16:     0x7ff6ec9126fc - core::option::Option<core::ptr::non_null::NonNull<bevy_input::keyboard::KeyboardInputState>>::unwrap_or_else
                               at C:\Users\Lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\option.rs:409
  17:     0x7ff6ec9126fc - bevy_ecs::resource::resources::Resources::get_unsafe_ref
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resources.rs:144
  18:     0x7ff6ec9126fc - bevy_ecs::resource::resource_query::{{impl}}::get<bevy_input::keyboard::KeyboardInputState>
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resource_query.rs:221
  19:     0x7ff6ec515fc6 - bevy_ecs::resource::resource_query::{{impl}}::get
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resource_query.rs:255
  20:     0x7ff6ec515fc6 - bevy_ecs::resource::resources::Resources::query_system
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\resource\resources.rs:139
  21:     0x7ff6ec515fc6 - bevy_ecs::system::into_system::{{impl}}::system::{{closure}}
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\system\into_system.rs:178
  22:     0x7ff6ec515fc6 - bevy_ecs::system::into_system::{{impl}}::run<bevy_ecs::system::into_system::QuerySystemState,closure-0,closure-1,closure-2,closure-3>
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\system\into_system.rs:60
  23:     0x7ff6ec31cc53 - bevy_ecs::schedule::schedule::Schedule::run
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_ecs\src\schedule\schedule.rs:139
  24:     0x7ff6ec2b12c4 - bevy_app::schedule_runner::{{impl}}::build::{{closure}}
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_app\src\schedule_runner.rs:60
  25:     0x7ff6ec2af1bc - alloc::boxed::{{impl}}::call
                               at C:\Users\Lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\alloc\src\boxed.rs:1039
  26:     0x7ff6ec2af1bc - bevy_app::app::App::run
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_app\src\app.rs:73
  27:     0x7ff6ec2b0db0 - bevy_app::app_builder::AppBuilder::run
                               at C:\Users\Lukas\.cargo\git\checkouts\bevy-f7ffde730c324c74\99e39b5\crates\bevy_app\src\app_builder.rs:43
  28:     0x7ff6ec2a5485 - crash::main
                               at D:\Lukas\Documents\Rust\GD50\pong\src\crash.rs:6
  29:     0x7ff6ec2a13db - core::ops::function::FnOnce::call_once<fn(),tuple<>>
                               at C:\Users\Lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:233
  30:     0x7ff6ec2a5a5b - std::sys_common::backtrace::__rust_begin_short_backtrace<fn(),tuple<>>
                               at C:\Users\Lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\sys_common\backtrace.rs:137
  31:     0x7ff6ec2a57e1 - std::rt::lang_start::{{closure}}<tuple<>>
                               at C:\Users\Lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:66
  32:     0x7ff6ec2ce13e - core::ops::function::impls::{{impl}}::call_once
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\library\core\src\ops\function.rs:286
  33:     0x7ff6ec2ce13e - std::panicking::try::do_call
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:373
  34:     0x7ff6ec2ce13e - std::panicking::try
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panicking.rs:337
  35:     0x7ff6ec2ce13e - std::panic::catch_unwind
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\panic.rs:394
  36:     0x7ff6ec2ce13e - std::rt::lang_start_internal
                               at /rustc/7e6d6e5f535321c2223f044caba16f97b825009c\/library\std\src\rt.rs:51
  37:     0x7ff6ec2a57b3 - std::rt::lang_start<tuple<>>
                               at C:\Users\Lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:65
  38:     0x7ff6ec2a54f0 - main
  39:     0x7ff6ecb2a930 - invoke_main
                               at d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  40:     0x7ff6ecb2a930 - __scrt_common_main_seh
                               at d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  41:     0x7ffd13a97bd4 - BaseThreadInitThunk
  42:     0x7ffd13bece51 - RtlUserThreadStart
error: process didn't exit successfully: `target\debug\crash.exe` (exit code: 101)

bevy version: 0.1.2 and 99e39b5
rustup toolchain: nightly-x86_64-pc-windows-msvc
rust version: rustc 1.47.0-nightly (7e6d6e5f5 2020-08-16)

Kind regards,
Lukas

Sidenote: as far as I can tell, ScheduleRunnerPlugin waits a fixed amount of time between each update but does not schedule updates in a fixed interval (1/60 is not necessarily 60 fps). I assume this is either intended or being planed (maybe with #125) but for the moment,

// this app loops forever at 60 fps
is technically not correct :-)

@karroffel karroffel added C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash labels Aug 18, 2020
@lukasschlueter
Copy link
Author

After some more experimentation, it seems that ScheduleRunnerPlugin is incompatible with winit (hence the crash).

When adding ScheduleRunnerPlugin before .add_default_plugins(), it doesn't crash but it also doesn't apply the schedule. This is because the WinitPlugin also calls set_runner:

app
// TODO: It would be great to provide a raw winit WindowEvent here, but the lifetime on it is
// stopping us. there are plans to remove the lifetime: https://github.com/rust-windowing/winit/pull/1456
// .add_event::<winit::event::WindowEvent>()
.init_resource::<WinitWindows>()
.set_runner(winit_runner);

So when using winit, currently the only way to limit the fps is using vsync: true in the WindowDescriptor. Maybe the solution is to add a new field fps to WindowDescriptor?

@cart
Copy link
Member

cart commented Aug 22, 2020

Yeah ScheduleRunnerPlugin is an alternative to winit.

I think the fix here is to add some sort of "frame limiting" config, which will handled by #125.

I think we should just scope this issue to resolving the "fixed interval" issue with ScheduleRunnerPlugin.

@cart
Copy link
Member

cart commented Aug 22, 2020

Closed by #233

(im happy to re-open this if you think I missed something)

@cart cart closed this as completed Aug 22, 2020
@smokku
Copy link
Member

smokku commented Sep 3, 2020

I encountered this issue when running headless.

The root cause is that when not using bevy_winit::winit_runner local resources of Local<T> are not initialized.

winit_runner somehow triggers ResourceQuery:initialize() and when it is not used, this trait implementation does not run.

Following simple program is enough to reproduce the issue:

use bevy::{app::ScheduleRunnerPlugin, prelude::*};
use std::time::Duration;

fn main() {
    App::build()
        .add_plugin(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(
            1.0 / 60.0,
        )))
        .add_system(counter.system())
        .run();
}

fn counter(mut state: Local<CounterState>) {
    println!("{}", state.count);
    state.count += 1;
}

#[derive(Default)]
struct CounterState {
    count: u32,
}
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/local_test`
thread 'main' panicked at 'Resource does not exist local_test::CounterState', /home/smoku/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/macros.rs:16:9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants