Skip to content

Panic when despawning entity with child scene just after creation #2406

@benoitryder

Description

@benoitryder

Bevy version

main branch (71bf07f)

Operating system & version

OS: Windows 10
Rust toolchain: stable-x86_64-pc-windows-gnu 1.52.1

What you did

Despawning an entity with a GLTF scene child one update after its creation leads to a panic.
Here is minimal code reproducing the issue.

use bevy::{
    prelude::*,
};

#[derive(Default)]
struct State {
  entity: Option<Entity>,
  delay: usize,
}


fn update(
    mut commands: Commands,
    assets: Res<AssetServer>,
    mut state: Local<State>,
) {
    if let Some(id) = state.entity {
        if state.delay == 0 {
            commands.entity(id).despawn_recursive();
            state.entity = None;
        } else {
            state.delay -= 1;
        }
    } else {
        let id = commands
            .spawn()
            .with_children(|parent| {
                let handle: Handle<Scene> = assets.load("alien.glb#Scene0");  // no panic with "alien.glb"
                parent.spawn_scene(handle);
            }).id();
        state.entity = Some(id);
        state.delay = 0;  // no panic when set to 1
    }
}

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

What you expected to happen

Entity is despawned, no panic.
Sample program above will spawn/despawn entities indefinitely.

What actually happened

After the first despawn, program panics with an Entity does not exist error and the following backtrace (truncated):

thread 'main' panicked at 'Entity does not exist', C:\Users\user\bevy\crates\bevy_ecs\src\world\mod.rs:215:37
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53\/library\std\src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53\/library\core\src/panicking.rs:92:14
   2: core::option::expect_failed
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53\/library\core\src/option.rs:1321:5
   3: core::option::Option<T>::expect
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53\library\core\src/option.rs:349:21
   4: bevy_ecs::world::World::entity_mut
             at C:\Users\user\bevy\crates\bevy_transform/C:\Users\user\bevy\crates\bevy_ecs\src\world/mod.rs:215:9
   5: <bevy_ecs::system::commands::Insert<T> as bevy_ecs::system::commands::Command>::write
             at C:\Users\user\bevy\crates\bevy_transform/C:\Users\user\bevy\crates\bevy_ecs\src\system/commands.rs:356:9
   6: bevy_ecs::system::commands::CommandQueue::apply
             at C:\Users\user\bevy\crates\bevy_ecs/C:\Users\user\bevy\crates\bevy_ecs\src\system/commands.rs:27:13
   7: <bevy_ecs::system::commands::CommandQueue as bevy_ecs::system::system_param::SystemParamState>::apply
             at C:\Users\user\bevy\crates\bevy_ecs/C:\Users\user\bevy\crates\bevy_ecs\src\system/system_param.rs:489:9
   8: <(P0,P1,P2,P3) as bevy_ecs::system::system_param::SystemParamState>::apply
             at C:\Users\user\bevy\crates\bevy_transform/C:\Users\user\bevy\crates\bevy_ecs\src\system/system_param.rs:1107:19
   9: <bevy_ecs::system::function_system::FunctionSystem<In,Out,Param,Marker,F> as bevy_ecs::system::system::System>::apply_buffers
             at C:\Users\user\bevy\crates\bevy_transform/C:\Users\user\bevy\crates\bevy_ecs\src\system/function_system.rs:338:9
  10: <bevy_ecs::schedule::stage::SystemStage as bevy_ecs::schedule::stage::Stage>::run
             at C:\Users\user\bevy\crates\bevy_ecs/C:\Users\user\bevy\crates\bevy_ecs\src\schedule/stage.rs:830:25
  11: bevy_ecs::schedule::Schedule::run_once
             at C:\Users\user\bevy\crates\bevy_ecs/C:\Users\user\bevy\crates\bevy_ecs\src\schedule/mod.rs:208:13
  12: <bevy_ecs::schedule::Schedule as bevy_ecs::schedule::stage::Stage>::run
             at C:\Users\user\bevy\crates\bevy_ecs/C:\Users\user\bevy\crates\bevy_ecs\src\schedule/mod.rs:226:21
  13: bevy_app::app::App::update
             at C:\Users\user\bevy\crates\bevy_app/C:\Users\user\bevy\crates\bevy_app\src/app.rs:62:9
<snipped>

Additional information

  • alien.glb is from bevy's assets, but I reproduced with other .glb files
  • It does not panic when using alien.glb asset path instead of alien.glb#Scene0.
  • It does not panic when delaying the despawn_recursive() to the next system call (initialize state.delay to 1 after spawn in sample code).
  • I'm not familiar with entity parenting, nor if it could be a known limitation of GLTF scenes. May I have misused something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ScenesComposing and serializing ECS objectsP-CrashA sudden unexpected crash

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions