bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.09k stars 3.56k forks source link

`World::clear_entities` causes observer logic to break safety #14648

Open Jacob-JB opened 2 months ago

Jacob-JB commented 2 months ago

Bevy version

0.14.1

What you did

I keep an additional app in a non send resource (not a sub app) which is sent to an async compute task for physics rollback.

Before the task is started all entities are cleared

app.world_mut().clear_entities();

The app then updates once. ScheduleRunnerPlugin is not added to the app.

app.update();

Stack trace is here log.txt

If I replace clear_entities() with

loop {
    let Some(entity) = app.world().iter_entities().next() else {
        break;
    };
    let entity = entity.id();
    app.world_mut().despawn(entity);
}

Then there is no panic.

What went wrong

There is clearly a difference between calling World::despawn and World::clear_entities. Using the latter is causing the new observer logic to break safety in an unsafe block. Even if I'm not using bevy as intended here I shouldn't be able to do this with safe methods.

alice-i-cecile commented 2 months ago

I would like to remove World::clear_entities TBH. This is mostly a footgun, and not very useful.

This issue relates to #13120.