Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawning related entities in an OnAdd observer downstream of a World::spawn in a Command causes a crash #18452

Closed
Jaso333 opened this issue Mar 20, 2025 · 2 comments · Fixed by #18545
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior P-Regression Functionality that used to work but no longer does. Add a test for this! S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Milestone

Comments

@Jaso333
Copy link
Contributor

Jaso333 commented Mar 20, 2025

Bevy version

main

What you did

Created an OnAdd observer for a component that adds related (ChildOf) entities using Commands. Then, spawn an entity with that component in an implementation of Command::apply using World::spawn. Here is the MRE (excluding App creation and observer/system insertion):

#[derive(Component)]
struct MyComponent;

fn on_add(trigger: Trigger<OnAdd, MyComponent>, mut commands: Commands) {
    commands
        .entity(trigger.target())
        .with_related::<ChildOf>(|rsc| {
            rsc.spawn_empty();
        });
}

struct MyCommand;

impl Command for MyCommand {
    fn apply(self, world: &mut World) -> () {
        world.spawn(MyComponent);
    }
}

fn startup(mut commands: Commands) {
    commands.queue(MyCommand);
}

What went wrong

It is claimed that the entity doesn't exist. The following error is emitted, and the app crashes:

thread 'main' panicked at C:\Users\Jason\.cargo\git\checkouts\bevy-50d7e162b728c6c6\821f6fa\crates\bevy_ecs\src\error\handler.rs:129:1:
Encountered an error in command `<bevy_ecs::system::commands::entity_command::insert<bevy_ecs::hierarchy::ChildOf>::{{closure}} as bevy_ecs::error::command_handling::CommandWithEntity<core::result::Result<(), bevy_ecs::world::error::EntityMutableFetchError>>>::with_entity::{{closure}}`: EntityDoesNotExist(EntityDoesNotExistError { entity: 11v1#4294967307, details: EntityDoesNotExistDetails { location: MaybeLocation { marker: PhantomData<core::option::Option<&core::panic::location::Location>> } } })

Additional information

The same thing happens with on_add hooks directly on the component as opposed to an observer. If I spawn the component using Commands instead of World, it works as expected.

In version 0.15.x, I was able to perform spawning using this exact pattern without this error.

@Jaso333 Jaso333 added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Mar 20, 2025
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events S-Needs-Investigation This issue requires detective work to figure out what's going wrong P-Regression Functionality that used to work but no longer does. Add a test for this! and removed S-Needs-Triage This issue needs to be labelled labels Mar 20, 2025
@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Mar 20, 2025
@Jaso333
Copy link
Contributor Author

Jaso333 commented Mar 20, 2025

To add to this: if I change world.spawn(MyComponent) to world.commands().spawn(MyComponent), it works.

@ElliottjPierce
Copy link
Contributor

I'll start investigating this since I don't think anybody else is. Stop me if I'm stepping on your toes.

ElliottjPierce added a commit to ElliottjPierce/bevy that referenced this issue Mar 25, 2025
github-merge-queue bot pushed a commit that referenced this issue Mar 25, 2025
… World::spawn in a Command does not cause a crash (#18545)

# Objective

fixes #18452.

## Solution

Spawning used to flush commands only, but those commands can reserve
entities. Now, spawning flushes everything, including reserved entities.
I checked, and this was the only place where `flush_commands` is used
instead of `flush` by mistake.

## Testing

I simplified the MRE from #18452 into its own test, which fails on main,
but passes on this branch.
mockersf pushed a commit that referenced this issue Mar 25, 2025
… World::spawn in a Command does not cause a crash (#18545)

# Objective

fixes #18452.

## Solution

Spawning used to flush commands only, but those commands can reserve
entities. Now, spawning flushes everything, including reserved entities.
I checked, and this was the only place where `flush_commands` is used
instead of `flush` by mistake.

## Testing

I simplified the MRE from #18452 into its own test, which fails on main,
but passes on this branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior P-Regression Functionality that used to work but no longer does. Add a test for this! S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Projects
None yet
3 participants