Panic when calling spawn_batch with an OnAdd observer which spawns entities
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 161
Description
## Bevy version
Tested on 0.16.0 and the main branch (673e70c72e492206457ecc65d1896130aac9f19d)
## What you did
The following code reproduces the issue:
```rust
use bevy::prelude::*;
#[derive(Component, Clone, Copy)]
struct Pawn;
fn main() -> AppExit {
App::new()
.add_observer(spawn_child)
.add_systems(Update, spawn_batch)
.run()
}
fn spawn_batch(mut commands: Commands) {
commands.spawn_batch([Pawn; 2]);
}
fn spawn_child(_: Trigger, mut commands: Commands) {
commands.spawn_empty();
}
```
## What went wrong
I expected this code to spawn 4 entities - two `Pawn` entities from the spawn_batch call, and two empty entities spawned by the observer. Instead it panics
```
thread 'main' panicked at crates\bevy_ecs\src\entity\mod.rs:804:9:
flush() needs to be called before this operation is legal
stack backtrace:
0: std::panicking::begin_panic_handler
at /rustc/17067e9ac6d7ecb70e50f92c1944e545188d2359/library\std\src\panicking.rs:697
1: core::panicking::panic_fmt
at /rustc/17067e9ac6d7ecb70e50f92c1944e545188d2359/library\core\src\panicking.rs:75
2: bevy_ecs::entity::Entities::verify_flushed
at .\crates\bevy_ecs\src\entity\mod.rs:804
3: bevy_ecs::entity::Entities::alloc
at .\crates\bevy_ecs\src\entity\mod.rs:812
4: bevy_ecs::bundle::BundleSpawner::spawn
at .\crates\bevy_ecs\src\bundle.rs:1792
5: bevy_ecs::world::spawn_batch::impl$2::next >
at .\crates\bevy_ecs\src\world\spawn_batch.rs:76
6: core::iter::traits::iterator::impl$0::next > >
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\iter\traits\iterator.rs:4049
7: bevy_ecs::world::spawn_batch::impl$1::drop >
at .\crates\bevy_ecs\src\world\spawn_batch.rs:59
8: core::ptr::drop_in_place > >
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ptr\mod.rs:523
9: bevy_ecs::system::commands::command::spawn_batch::closure$0 >
at .\crates\bevy_ecs\src\system\commands\command.rs:77
10: bevy_ecs::system::commands::command::impl$0::apply >,tuple$<> >
at .\crates\bevy_ecs\src\system\commands\command.rs:62
11: bevy_ecs::world::command_queue::impl$4::push::closure$0 > >
at .\crates\bevy_ecs\src\world\command_queue.rs:173
12: core::ops::function::FnOnce::call_once > >,tuple$,enum2$,bevy_ecs::world::command_queue::impl$4::apply_or_drop_queued::closure_env$0>
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\panic\unwind_safe.rs:272
15: std::panicking::try::do_call,tuple$<> >
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:589
16: std::panic::catch_unwind,tuple$<> >
17: std::panicking::try
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:552
18: std::panic::catch_unwind,tuple$<> >
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panic.rs:359
19: bevy_ecs::world::command_queue::RawCommandQueue::apply_or_drop_queued
at .\crates\bevy_ecs\src\world\command_queue.rs:268
20: bevy_ecs::world::command_queue::CommandQueue::apply
at .\crates\bevy_ecs\src\world\command_queue.rs:94
21: bevy_ecs::world::command_queue::impl$6::apply
at .\crates\bevy_ecs\src\world\command_queue.rs:326
22: bevy_ecs::system::system_param::impl$22::apply
at .\crates\bevy_ecs\src\system\system_param.rs:1314
23: bevy_ecs::system::system_param::impl$103::apply,ref$ >
at .\crates\bevy_ecs\src\system\system_param.rs:2101
24: bevy_ecs::system::commands::_::impl$0::apply
at .\crates\bevy_ecs\src\system\commands\mod.rs:155
25: bevy_ecs::system::system_param::impl$101::apply
at .\crates\bevy_ecs\src\system\system_param.rs:2101
26: bevy_ecs::system::function_system::impl$7::apply_deferred
at .\crates\bevy_ecs\src\system\function_system.rs:748
27: bevy_ecs::system::schedule_system::impl$1::apply_deferred >
at .\crates\bevy_ecs\src\system\schedule_system.rs:75
28: bevy_ecs::schedule::executor::multi_threaded::apply_deferred::closure$0
at .\crates\bevy_ecs\src\schedule\executor\multi_threaded.rs:800
29: core::ops::function::FnOnce::call_once >
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:250
30: core::panic::unwind_safe::impl$25::call_once,bevy_ecs::schedule::executor::multi_threaded::apply_deferred::closure_env$0>
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\panic\unwind_safe.rs:272
31: std::panicking::try::do_call,tuple$<> >
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:589
32: std::panic::catch_unwind,tuple$<> >
33: std::panicking::try
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:552
34: std::panic::catch_unwind,tuple$<> >
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panic.rs:359
35: bevy_ecs::schedule::executor::multi_threaded::apply_deferred
at .\crates\bevy_ecs\src\schedule\executor\multi_threaded.rs:799
36: bevy_ecs::schedule::executor::multi_threaded::impl$2::run
at .\crates\bevy_ecs\src\schedule\executor\multi_threaded.rs:302
37: bevy_ecs::schedule::schedule::Schedule::run
at .\crates\bevy_ecs\src\schedule\schedule.rs:448
38: bevy_ecs::world::impl$4::try_run_schedule::closure$0 > >
at .\crates\bevy_ecs\src\world\mod.rs:3495
39: bevy_ecs::world::World::try_schedule_scope,bevy_ecs::intern::Interned >,bevy_ecs::world::impl$4::try_run_schedule::closure_env$0 >
at .\crates\bevy_ecs\src\world\mod.rs:3428
40: bevy_ecs::world::World::try_run_schedule > >
at .\crates\bevy_ecs\src\world\mod.rs:3495
41: bevy_app::main_schedule::impl$2::run_main::closure$1
at .\crates\bevy_app\src\main_schedule.rs:296
42: bevy_ecs::world::World::try_resource_scope,bevy_app::main_schedule::impl$2::run_main::closure_env$1>
at .\crates\bevy_ecs\src\world\mod.rs:2599
43: bevy_ecs::world::World::resource_scope,bevy_app::main_schedule::impl$2::run_main::closure_env$1>
at .\crates\bevy_ecs\src\world\mod.rs:2562
44: bevy_app::main_schedule::Main::run_main
at .\crates\bevy_app\src\main_schedule.rs:294
45: core::ops::function::FnMut::call_mut,bevy_ecs::system::system_param::Local),tuple$,bevy_ecs::system::system_param::Local > >
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:166
46: core::ops::function::impls::impl$3::call_mut,bevy_ecs::system::system_param::Local >,void (*)(ref_mut$,bevy_ecs::system::system_param::Local)>
at C:\Users\andre\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:294
47: bevy_ecs::system::exclusive_function_system::impl$5::run::call_inner,bevy_ecs::system::system_param::Local,ref_mut$,bevy_ecs::system::system_param::Local)> >
at .\crates\bevy_ecs\src\system\exclusive_function_system.rs:259
48: bevy_ecs::system::exclusive_function_system::impl$5::run,void (*)(ref_mut$,bevy_ecs::system::system_param::Local),bevy_ecs::system::system_param::Local >
at .\crates\bevy_ecs\src\system\exclusive_function_system.rs:262
49: bevy_ecs::system::exclusive_function_system::impl$2::run_unsafe::closure$0),void (*)(ref_mut$,bevy_ecs::system::system_param::Local)>
at .\crates\bevy_ecs\src\system\exclusive_function_system.rs:134
50: bevy_ecs::world::World::last_change_tick_scope,bevy_ecs::system::exclusive_function_system::impl$2::run_unsafe::closure_env$0),void (*)(ref_mut$,bevy_ecs::system::system_
at .\crates\bevy_ecs\src\world\mod.rs:2950
51: bevy_ecs::system::exclusive_function_system::impl$2::run_unsafe),void (*)(ref_mut$,bevy_ecs::system::system_param::Local)>
at .\crates\bevy_ecs\src\system\exclusive_function_system.rs:126
52: bevy_ecs::system::schedule_system::impl$1::run_unsafe),void (*)(ref_mut$,bevy_ecs::system::system_param::Local
at .\crates\bevy_ecs\src\system\schedule_system.rs:69
53: bevy_ecs::system::system::System::run_without_applying_deferred),void (*)(r
at .\crates\bevy_ecs\src\system\system.rs:111
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Encountered a panic when applying buffers for system `my_app::spawn_batch`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
```
Contributor guide
Assessment
This issue has not been assessed yet.