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

Segfault while adding a state #2464

Closed
64kramsystem opened this issue Jul 13, 2021 · 13 comments
Closed

Segfault while adding a state #2464

64kramsystem opened this issue Jul 13, 2021 · 13 comments
Labels
A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior O-Linux Specific to the Linux desktop operating system S-Needs-Investigation This issue requires detective work to figure out what's going wrong

Comments

@64kramsystem
Copy link
Contributor

64kramsystem commented Jul 13, 2021

Bevy version

0.5.0

Operating system & version

Ubuntu 20.04 MATE, tested on two separate machines (Nvidia/Intel).

Rust: rustc 1.55.0-nightly (432e145bd 2021-07-10)
Other Rust tested: rustc 1.53.0 (53cb7b09b 2021-06-17)

What you did

Listing follows. Note that this is incorrect logic, but I believe that this should not segfault regardless.

The segfault reproduces 100% of the times, on two machines, both on stable and nightly.

use bevy::prelude::*;

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum AppState {
    MainMenu,
    InGame,
}

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_state(AppState::MainMenu)
        .add_state(AppState::InGame)
        .run();
}

What you expected to happen

Panic, but no segfault.

What actually happened

Stack trace, ending with:

[1]    12345 segmentation fault  cargo run
@64kramsystem 64kramsystem added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Jul 13, 2021
@bjorn3
Copy link
Contributor

bjorn3 commented Jul 13, 2021

What is the stacktrace? Does it reproduce when you only add a single or no states?

@bjorn3 bjorn3 added the P-Crash A sudden unexpected crash label Jul 13, 2021
@alice-i-cecile alice-i-cecile added S-Needs-Investigation This issue requires detective work to figure out what's going wrong A-ECS Entities, components, systems, and events and removed S-Needs-Triage This issue needs to be labelled labels Jul 13, 2021
@64kramsystem
Copy link
Contributor Author

Happens only when a second state is added. Full stacktrace here:

$ RUST_BACKTRACE=full cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/bug`
thread 'main' panicked at 'Run criteria &bevy_ecs::schedule::state::state_cleaner<bug::AppState> is labelled with Some(DriverLabel(TypeId { t: 3940695263965142806 })), which is already in use. Consider using `RunCriteriaDescriptorCoercion::label_discard_if_duplicate().', /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.5.0/src/schedule/stage.rs:345:66
stack backtrace:
   0:     0x563fbcd29a60 - std::backtrace_rs::backtrace::libunwind::trace::h706b838f5bbd876b
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5
   1:     0x563fbcd29a60 - std::backtrace_rs::backtrace::trace_unsynchronized::hc15f29ae7822b7b8
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x563fbcd29a60 - std::sys_common::backtrace::_print_fmt::h7c580c971f91926c
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x563fbcd29a60 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h2a772198c4032452
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x563fbcd4cf0c - core::fmt::write::h9a6d9c74526a6c1b
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/fmt/mod.rs:1115:17
   5:     0x563fbcd26755 - std::io::Write::write_fmt::h00f38d9eb2c32e02
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/io/mod.rs:1663:15
   6:     0x563fbcd2badb - std::sys_common::backtrace::_print::h5b3c171e6f864ae4
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x563fbcd2badb - std::sys_common::backtrace::print::h93b9e9ed2a98e611
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x563fbcd2badb - std::panicking::default_hook::{{closure}}::hd8da92bb68d520c5
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:208:50
   9:     0x563fbcd2b5b1 - std::panicking::default_hook::hdbc8b2951c5afbab
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:225:9
  10:     0x563fbcd2c1a4 - std::panicking::rust_panic_with_hook::h8a4c841655926f4e
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:622:17
  11:     0x563fbcd2bc87 - std::panicking::begin_panic_handler::{{closure}}::h471b6bc74904b7b0
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:519:13
  12:     0x563fbcd29f5c - std::sys_common::backtrace::__rust_end_short_backtrace::h309f5c707f29932f
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/sys_common/backtrace.rs:141:18
  13:     0x563fbcd2bbe9 - rust_begin_unwind
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:515:5
  14:     0x563fbae137db - std::panicking::begin_panic_fmt::h124dfb9fde553188
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:457:5
  15:     0x563fbcb5894f - bevy_ecs::schedule::stage::SystemStage::initialize_systems::{{closure}}::hc29d10f39b15ac9b
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.5.0/src/schedule/stage.rs:345:66
  16:     0x563fbcb51b85 - core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut::he34a0aef0db9d4fa
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/ops/function.rs:269:13
  17:     0x563fbcab8ebd - core::iter::traits::iterator::Iterator::find_map::check::{{closure}}::hbfc9d4fc285a927f
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/iter/traits/iterator.rs:2410:32
  18:     0x563fbcb46098 - <core::iter::adapters::enumerate::Enumerate<I> as core::iter::traits::iterator::Iterator>::try_fold::enumerate::{{closure}}::h43aceab2bb095c38
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/iter/adapters/enumerate.rs:83:27
  19:     0x563fbcad8cbb - core::iter::traits::iterator::Iterator::try_fold::h6df390e2f1b13e0d
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/iter/traits/iterator.rs:1997:21
  20:     0x563fbcb45f84 - <core::iter::adapters::enumerate::Enumerate<I> as core::iter::traits::iterator::Iterator>::try_fold::he3224c9094327980
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/iter/adapters/enumerate.rs:89:9
  21:     0x563fbcb468f3 - core::iter::traits::iterator::Iterator::find_map::h97f13735d7f215d7
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/iter/traits/iterator.rs:2416:9
  22:     0x563fbcae3971 - <core::iter::adapters::filter_map::FilterMap<I,F> as core::iter::traits::iterator::Iterator>::next::hf1b412f367fa5e10
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/iter/adapters/filter_map.rs:61:9
  23:     0x563fbcaff454 - alloc::vec::Vec<T,A>::extend_desugared::ha15b10b0a02e8b26
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/alloc/src/vec/mod.rs:2550:35
  24:     0x563fbcb0c19b - <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend::hb2f3c0f63a421141
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/alloc/src/vec/spec_extend.rs:18:9
  25:     0x563fbcafb3b7 - <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter::h9acae62690a1921a
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/alloc/src/vec/spec_from_iter_nested.rs:37:9
  26:     0x563fbcb0cb1c - <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter::h35f92767aa9d096b
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/alloc/src/vec/spec_from_iter.rs:33:9
  27:     0x563fbcb0ac60 - <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter::h834ae5c447a2511c
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/alloc/src/vec/mod.rs:2453:9
  28:     0x563fbcaf027c - core::iter::traits::iterator::Iterator::collect::h26399af54026beff
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/iter/traits/iterator.rs:1748:9
  29:     0x563fbcb57a54 - bevy_ecs::schedule::stage::SystemStage::initialize_systems::hd9e3f3c5dc8c2066
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.5.0/src/schedule/stage.rs:334:29
  30:     0x563fbcb61b13 - <bevy_ecs::schedule::stage::SystemStage as bevy_ecs::schedule::stage::Stage>::run::haf31d07331dd8847
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.5.0/src/schedule/stage.rs:751:13
  31:     0x563fbcac0c00 - bevy_ecs::schedule::Schedule::run_once::h9f1360aa6725311a
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.5.0/src/schedule/mod.rs:201:13
  32:     0x563fbcac0c70 - <bevy_ecs::schedule::Schedule as bevy_ecs::schedule::stage::Stage>::run::h901a644918ac118e
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.5.0/src/schedule/mod.rs:219:21
  33:     0x563fbca77c78 - bevy_app::app::App::update::h813dae167fe16a39
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_app-0.5.0/src/app.rs:58:9
  34:     0x563fbb9e65ae - bevy_winit::winit_runner_with::{{closure}}::h26c9a8952e143b87
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_winit-0.5.0/src/lib.rs:485:17
  35:     0x563fbb9bc2ab - winit::platform_impl::platform::sticky_exit_callback::h5df4bf2672ca4e6c
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.24.0/src/platform_impl/linux/mod.rs:736:5
  36:     0x563fbb9cc438 - winit::platform_impl::platform::x11::EventLoop<T>::run_return::h04faa297c917d567
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.24.0/src/platform_impl/linux/x11/mod.rs:303:17
  37:     0x563fbb9cd575 - winit::platform_impl::platform::x11::EventLoop<T>::run::hdb161f8a88ad47c8
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.24.0/src/platform_impl/linux/x11/mod.rs:398:9
  38:     0x563fbb9bbf31 - winit::platform_impl::platform::EventLoop<T>::run::h68d2f30ab2a3e758
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.24.0/src/platform_impl/linux/mod.rs:652:56
  39:     0x563fbb9e21c0 - winit::event_loop::EventLoop<T>::run::hcbf36016cf654536
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.24.0/src/event_loop.rs:154:9
  40:     0x563fbb9e5a30 - bevy_winit::run::hdd2f2341b982ef14
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_winit-0.5.0/src/lib.rs:171:5
  41:     0x563fbb9e618b - bevy_winit::winit_runner_with::h0ff23ca1e607fb08
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_winit-0.5.0/src/lib.rs:493:9
  42:     0x563fbb9e5ada - bevy_winit::winit_runner::h1017041231995ff5
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_winit-0.5.0/src/lib.rs:211:5
  43:     0x563fbb9ada71 - core::ops::function::Fn::call::h749ccd8e40f89460
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/ops/function.rs:70:5
  44:     0x563fbca777d8 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h9000110b8377ae44
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/alloc/src/boxed.rs:1586:9
  45:     0x563fbca77d91 - bevy_app::app::App::run::h8361f6af4e095160
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_app-0.5.0/src/app.rs:68:9
  46:     0x563fbca7144e - bevy_app::app_builder::AppBuilder::run::ha4f6bda8ec446a1d
                               at /home/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_app-0.5.0/src/app_builder.rs:49:9
  47:     0x563fbae1831d - bug::main::hedbea69ce2652951
                               at /tmp/bug/src/main.rs:10:5
  48:     0x563fbae1d62b - core::ops::function::FnOnce::call_once::h1467eef5861215f5
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/ops/function.rs:227:5
  49:     0x563fbae17ffe - std::sys_common::backtrace::__rust_begin_short_backtrace::ha5d94c356ebcc9f2
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/sys_common/backtrace.rs:125:18
  50:     0x563fbae1d991 - std::rt::lang_start::{{closure}}::h5fcd425e30d35421
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/rt.rs:63:18
  51:     0x563fbcd2c6aa - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::hcbe5785d54a49ce2
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/core/src/ops/function.rs:259:13
  52:     0x563fbcd2c6aa - std::panicking::try::do_call::h1c15e7ee8563c29d
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:401:40
  53:     0x563fbcd2c6aa - std::panicking::try::hb8b07cef009cb4fc
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:365:19
  54:     0x563fbcd2c6aa - std::panic::catch_unwind::h65a8efbe596bcbb1
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panic.rs:434:14
  55:     0x563fbcd2c6aa - std::rt::lang_start_internal::{{closure}}::h66ded7ab0f18e9fb
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/rt.rs:45:48
  56:     0x563fbcd2c6aa - std::panicking::try::do_call::h29cafa65efb3f3f3
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:401:40
  57:     0x563fbcd2c6aa - std::panicking::try::h412fc5378b4dbfc4
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panicking.rs:365:19
  58:     0x563fbcd2c6aa - std::panic::catch_unwind::h555def6aac5bf192
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/panic.rs:434:14
  59:     0x563fbcd2c6aa - std::rt::lang_start_internal::hfff48202d7fee6e3
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/rt.rs:45:20
  60:     0x563fbae1d960 - std::rt::lang_start::h8a16cb18a48d83db
                               at /rustc/8b87e85394aa583b01e53aef06343dd0749a3324/library/std/src/rt.rs:62:5
  61:     0x563fbae183ac - main
  62:     0x7fddbb43c0b3 - __libc_start_main
  63:     0x563fbae1622e - _start
  64:                0x0 - <unknown>
[1]    123456 segmentation fault  RUST_BACKTRACE=full cargo run

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Jul 15, 2021

I just tried it on Windows 10 (bevy 0.5) and got the same error message with a standard panic.

I wonder why that's getting turned into a segfault instead...

@bjorn3
Copy link
Contributor

bjorn3 commented Jul 15, 2021

I suspect there is either a bug somewhere in wgpu's handling of destroying the vulkan context when unwinding causing UB manifesting as SIGSEGV in this case. Or an ordering issue between destorying the vulkan context by wgpu and the X11/Wayland context in winit caused by bevy.

@alice-i-cecile
Copy link
Member

Agreed. @saveriomiroddi does this occur without DefaultPlugins?

Does it occur for other panics, such as the one produced by this snippet?

use bevy::prelude::*;

struct UninitResource;

fn resource_system(res: Res<UninitResource>) {}

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_system(resource_system.system())
        .run();
}

@64kramsystem
Copy link
Contributor Author

64kramsystem commented Jul 15, 2021

Agreed. @saveriomiroddi does this occur without DefaultPlugins?

Does it occur for other panics, such as the one produced by this snippet?

use bevy::prelude::*;

struct UninitResource;

fn resource_system(res: Res<UninitResource>) {}

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_system(resource_system.system())
        .run();
}

So!

  1. reference code, without DefaultPlugins -> no segfault;
  2. code provided by @alice-i-cecile -> segfault;
  3. code provided by @alice-i-cecile, without DefaultPlugins -> no segfault.

Note that this time, I've tested only on one machine (Nvidia).

@alice-i-cecile
Copy link
Member

Okay sweet, so it's probably a GPU / windowing cleanup issue like @bjorn3 commented. If you happen to have other OS's for the same machine installed that would be great, otherwise we can see if we can get other Linux folks to reproduce it.

@alice-i-cecile alice-i-cecile added A-Windowing Platform-agnostic interface layer to run your app in O-Linux Specific to the Linux desktop operating system and removed A-ECS Entities, components, systems, and events P-Crash A sudden unexpected crash labels Jul 16, 2021
@bjorn3
Copy link
Contributor

bjorn3 commented Jul 16, 2021

I believe I have seen this crash in other issues where a panic happened.

@64kramsystem
Copy link
Contributor Author

64kramsystem commented Jul 16, 2021

Okay sweet, so it's probably a GPU / windowing cleanup issue like @bjorn3 commented. If you happen to have other OS's for the same machine installed that would be great, otherwise we can see if we can get other Linux folks to reproduce it.

Other OS's like Windows? If so, I can do that on the other machine (which had the same behavior; my workstation has a tricky setup). It'll take a bit, since I have to prepare a Windows to go flash key and run the cross-compiled executable on it, but I can.

@alice-i-cecile
Copy link
Member

@saveriomiroddi that would be much appreciated. It would help us start to narrow this down and verify that it's a driver / OS issue, rather than a hardware problem.

@64kramsystem
Copy link
Contributor Author

@saveriomiroddi that would be much appreciated. It would help us start to narrow this down and verify that it's a driver / OS issue, rather than a hardware problem.

So, I've run the program on Windows 10 x64, on one of the two machines that segfaults when running it on Linux, and it didn't segfault. I'm not 100% sure how a hypothetical segfault would be, but I didn't see anything aside the panic in the console (and a window flashing for a very brief moment).

@djeedai
Copy link
Contributor

djeedai commented Oct 2, 2021

In case this is not clear for others hitting this bug, or what appears to be it at first if a search for the error message led you on this bug, the bug here is I think about the segfault instead of panic, not about the code being wrong in the first place. But it's nonetheless very easy to write wrong code due to add_state() being a bit of a misnomer in my opinion (arguable, I agree). So posting the correct code in case anyone looks for the error message and finds this bug. This doesn't fix the bug but works around it:

Only call add_state() once.

I was hitting the same thing, yet the official examples work. Looking carefully, all 4 examples using state call add_state() once and once only, whereas this bug and my use are both calling it twice at least. I believe this is just a misunderstanding of what add_state() is supposed to do, which is probably not add a state but instead set the initial state. Removing the duplicate call in my case fixes the issue. Other states are reached by state transitioning (see state.rs for example).

Note that to confirm I'm hitting the same callstack and error message with a second add_state(), and the error message could do with some more clarity.

@nicopap
Copy link
Contributor

nicopap commented Apr 18, 2023

States got fully reworked in #6587 . This is not an issue anymore.

@nicopap nicopap closed this as completed Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior O-Linux Specific to the Linux desktop operating system S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Projects
None yet
Development

No branches or pull requests

5 participants