bevyengine / bevyengine/bevy

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

Open
#14,648 1 comment 0 reactions 0 assignees View on GitHub
A-ECS C-Bug S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## 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](https://github.com/user-attachments/files/16536112/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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.