Skip to content

Panic on unwrap when calling despawn_recursive() #3195

Closed
@gzp79

Description

Bevy version

0.5, main@f3d4be3

Operating system & version

Windows, but I think all.

What you did

When I spawn a scene as a child and despawn the root before the scene is loaded, and unwrap on None happens.

Sample code - check the timeout parameter. In my case the swap was triggered by user input and if user is fast a crash may happen.

use bevy::{core::FixedTimestep, pbr::AmbientLight, prelude::*};

fn main() {
    let timeout
    // if asset is not loaded, sometimes it panics for missing entity
     = 0.1;
    // if asset is loaded all is fine
    //= 30.0;

    App::build()
        .insert_resource(AmbientLight {
            color: Color::WHITE,
            brightness: 1.0 / 5.0f32,
        })
        .insert_resource(Msaa { samples: 4 })
        .insert_resource(State::default())
        .add_plugins(DefaultPlugins)
        //.add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new())
        .add_startup_system(setup.system())
        .add_startup_system(update.system())
        .add_system_set(
            SystemSet::new()
                .with_run_criteria(FixedTimestep::step(timeout))
                .with_system(update.system()),
        )
        //.add_system(update.system())
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn_bundle(PerspectiveCameraBundle {
        transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
        ..Default::default()
    });

    commands.spawn_bundle(LightBundle {
        transform: Transform::from_xyz(3.0, 5.0, 3.0),
        ..Default::default()
    });
}

#[derive(Default)]
struct State {
    id: u32,
    root: Option<Entity>,
}

fn update(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    mut scene_spawner: ResMut<SceneSpawner>,
    mut current: ResMut<State>,
) {
    let root = commands.spawn().id();
    let (id, scene) = match current.id {
        0 => (1, "models/FlightHelmet/FlightHelmet.gltf#Scene0"),
        1 => (2, "models/AlienCake/alien.glb#Scene0"),
        2 => (0, "models/AlienCake/cakeBirthday.glb#Scene0"),
        _ => unreachable!(),
    };
    current.id = id;
    println!("created root {:?} for {}({})", root, current.id, scene);

    let scene_handle: Handle<Scene> = asset_server.load(scene);
    scene_spawner.spawn_as_child(scene_handle, root);
    if let Some(old) = current.root.replace(root) {
        println!("remove: {:?}", old);
        commands.entity(old).despawn_recursive();
    }
}

backtrace:

created root 57v0 for 2(models/AlienCake/alien.glb#Scene0)
remove: 2v3
thread 'main' panicked at 'Entity does not exist', d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\world\mod.rs:214:37
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\/library\std\src\panicking.rs:498
   1: core::panicking::panic_fmt
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\/library\core\src\panicking.rs:106
   2: core::panicking::panic_display<str>
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\/library\core\src\panicking.rs:62
   3: core::option::expect_failed
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\/library\core\src\option.rs:1637
   4: enum$<core::option::Option<bevy_ecs::world::entity_ref::EntityMut>, 1, 18446744073709551615, Some>::expect<bevy_ecs::world::entity_ref::EntityMut>
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\core\src\option.rs:709
   5: bevy_ecs::world::World::entity_mut
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\world\mod.rs:214
   6: bevy_ecs::system::commands::impl$7::write<bevy_transform::components::children::Children>
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\system\commands.rs:351
   7: bevy_ecs::system::commands::CommandQueue::apply
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\system\commands.rs:24
   8: bevy_ecs::system::system_param::impl$21::apply
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\system\system_param.rs:458
   9: bevy_ecs::system::system_param::impl$90::apply<bevy_ecs::system::commands::CommandQueue,bevy_ecs::query::state::QueryState<tuple$<bevy_ecs::entity::Entity,ref$<bevy_transform::components::parent::PreviousParent> >,bevy_ecs::query::filter::Without<bevy_tra
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\system\system_param.rs:1010
  10: bevy_ecs::system::into_system::impl$4::apply_buffers<tuple$<>,tuple$<>,tuple$<bevy_ecs::system::commands::Commands,bevy_ecs::system::query::Query<tuple$<bevy_ecs::entity::Entity,ref$<bevy_transform::components::parent::PreviousParent> >,bevy_ecs::query::f
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\system\into_system.rs:161
  11: bevy_ecs::schedule::stage::impl$1::run
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\schedule\stage.rs:834
  12: bevy_ecs::schedule::Schedule::run_once
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\schedule\mod.rs:201
  13: bevy_ecs::schedule::impl$1::run
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.5.0\src\schedule\mod.rs:219
  14: bevy_app::app::App::update
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_app-0.5.0\src\app.rs:58
  15: bevy_winit::winit_runner_with::closure$1
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_winit-0.5.0\src\lib.rs:485
  16: winit::platform_impl::platform::event_loop::impl$2::run_return::closure$0<tuple$<>,bevy_winit::winit_runner_with::closure$1>
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop.rs:203
  17: alloc::boxed::impl$45::call_mut<tuple$<enum$<winit::event::Event<tuple$<> > >,ref_mut$<enum$<winit::event_loop::ControlFlow> > >,dyn$<core::ops::function::FnMut<tuple$<enum$<winit::event::Event<tuple$<> > >,ref_mut$<enum$<winit::event_loop::ControlFlow> >
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\alloc\src\boxed.rs:1701
  18: winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0<tuple$<> >
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop\runner.rs:245
  19: core::panic::unwind_safe::impl$23::call_once<tuple$<>,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>      
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\core\src\panic\unwind_safe.rs:271
  20: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>,tuple$<> >
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\std\src\panicking.rs:406
  21: winit::platform_impl::platform::dark_mode::should_apps_use_dark_mode::impl$0::deref
  22: std::panicking::try<tuple$<>,core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0> >
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\std\src\panicking.rs:370
  23: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>,tuple$<> >
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\std\src\panic.rs:133
  24: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::catch_unwind<tuple$<>,tuple$<>,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop\runner.rs:152
  25: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::call_event_handler<tuple$<> >
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop\runner.rs:239
  26: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::move_state_to<tuple$<> >
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop\runner.rs:341
  27: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::main_events_cleared<tuple$<> >
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop\runner.rs:227
  28: winit::platform_impl::platform::event_loop::flush_paint_messages<tuple$<> >
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop.rs:676
  29: winit::platform_impl::platform::event_loop::thread_event_target_callback::closure$0<tuple$<> >
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop.rs:1967
  30: core::panic::unwind_safe::impl$23::call_once<isize,winit::platform_impl::platform::event_loop::thread_event_target_callback::closure$0>
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\core\src\panic\unwind_safe.rs:271
  31: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure$0>,isize>
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\std\src\panicking.rs:406
  32: winit::platform_impl::platform::dark_mode::should_apps_use_dark_mode::impl$0::deref
  33: std::panicking::try<isize,core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure$0> >
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\std\src\panicking.rs:370
  34: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure$0>,isize>
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\std\src\panic.rs:133
  35: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::catch_unwind<tuple$<>,isize,winit::platform_impl::platform::event_loop::thread_event_target_callback::closure$0>
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop\runner.rs:152
  36: winit::platform_impl::platform::event_loop::thread_event_target_callback<tuple$<> >
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop.rs:2151
  37: DefSubclassProc
  38: DefSubclassProc
  39: CallWindowProcW
  40: DispatchMessageW
  41: SendMessageTimeoutW
  42: KiUserCallbackDispatcher
  43: NtUserDispatchMessage
  44: DispatchMessageW
  45: winit::platform_impl::platform::event_loop::EventLoop<tuple$<> >::run_return<tuple$<>,bevy_winit::winit_runner_with::closure$1>
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop.rs:218
  46: winit::platform_impl::platform::event_loop::EventLoop<tuple$<> >::run<tuple$<>,bevy_winit::winit_runner_with::closure$1>
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop.rs:188
  47: winit::event_loop::EventLoop<tuple$<> >::run<tuple$<>,bevy_winit::winit_runner_with::closure$1>
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\event_loop.rs:154
  48: bevy_winit::run<bevy_winit::winit_runner_with::closure$1>
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_winit-0.5.0\src\lib.rs:171
  49: bevy_winit::winit_runner_with
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_winit-0.5.0\src\lib.rs:493
  50: bevy_winit::winit_runner
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_winit-0.5.0\src\lib.rs:211
  51: core::ops::function::Fn::call<void (*)(bevy_app::app::App),tuple$<bevy_app::app::App> >
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\core\src\ops\function.rs:70
  52: alloc::boxed::impl$46::call<tuple$<bevy_app::app::App>,dyn$<core::ops::function::Fn<tuple$<bevy_app::app::App>,assoc$<Output,tuple$<> > > >,alloc::alloc::Global>
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\alloc\src\boxed.rs:1708
  53: bevy_app::app::App::run
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_app-0.5.0\src\app.rs:68
  54: bevy_app::app_builder::AppBuilder::run
             at d:\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_app-0.5.0\src\app_builder.rs:49
  55: sample::main
             at .\src\main.rs:10
  56: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/936238a92b2f9d6e7afe7dda69b4afd903f96399\library\core\src\ops\function.rs:227

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions