Skip to content

Potential surprising "entity does not exist" panic due to asynchronous command execution #7118

@infmagic2047

Description

@infmagic2047

Bevy version

0.9.1

What went wrong

These two systems look innocent on their own, but may cause panic if used together.

fn insert(mut commands: Commands, query: Query<Entity, With<Marker>>, ...) {
    for entity in query.iter() {
        if ... {
            // We obtained the entity from the query, so it must be valid, right?
            commands.entity(entity).insert(Foo);
        }
    }
}

fn despawn(mut commands: Commands, query: Query<Entity, With<Marker>>, ...) {
    for entity in query.iter() {
        if ... {
            // This command may execute before the insertion and cause a panic there!
            commands.entity(entity).despawn();
        }
    }
}

This panic is hard to avoid in user code, as there is no way to know if there is a pending despawn command for a certain entity.

Additional information

The Insert command is, as far as I know, the only place where a missing entity in command execution is turned into a panic. We should at least replace the panic with a soft error or warning like the other commands.

We are also currently inconsistent in the way missing entities are reported. Insert uses panics, Remove uses no error reporting at all (we may want to change this), Despawn uses warnings, and DespawnRecursive use debug messages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-UsabilityA targeted quality-of-life change that makes Bevy easier to use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions