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

Panic at parent != previous When Adding a Child to a Parent When It's Already a Child #6668

Closed
zicklag opened this issue Nov 17, 2022 · 0 comments
Labels
A-Hierarchy Parent-child entity hierarchies C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash

Comments

@zicklag
Copy link
Member

zicklag commented Nov 17, 2022

Bevy version

Bevy 0.8.1, and Bevy 9f51651

What you did

use bevy::prelude::*;

fn main() -> anyhow::Result<()> {
    let mut world = World::new();

    let parent = world.spawn().id();
    let child = world.spawn().id();
    world.entity_mut(parent).push_children(&[child]);
    world.entity_mut(parent).push_children(&[child]);

    Ok(())
}

What went wrong

thread 'main' panicked at 'assertion failed: parent != previous', crates/bevy_hierarchy/src/child_builder.rs:54:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

IMHO this shouldn't panic.

Additional information

I opened this as a bug because I think something needs to change, even if that is simply the panic message. I got this message occasionally in my game, but it wasn't clear what the actual root cause was without looking deeper into what the Bevy code was trying to do. It turns out it's quite simple: if you try to add something as a child, when it's already a child, it panics here.

If we really think this should panic, then the message should be something like "attempted to add a child to a parent, when it was already a child".

I can see the following reasons to make this panic:

  • If you are trying to add something as a child, when it's already a child, then it might be a logic error in the game, and if it is, you'll want to know about it. It could help you find bugs in your game you might not otherwise notice.
    • For instance, the fact that this happens in my game might mean I've done something wrong in my snapshot-restore system.
  • There is the question of how pushing a child that is already in the list effects the child order. For instance, usually a push-child would put the child at the end, but if it's already in the list, does it get removed from where it is, and then pushed to the end?

But I think the reasons for not panicking are probably stronger:

  • It's subjectively unintuitive that this panics. I haven't done anything that is fundamentally wrong, I've just told it to put it in the state it was already in.
  • If you know that something must be the child of another thing, it may be easier just to say that and push the child, without checking whether or not it is already a child.
  • Since updates to the hierarchy are handled by command buffer flushes, two separate systems could independently realize that an entity should be the child of the same parent, both push the same child to the same parent, and then cause a panic because both commands got flushed in the same stage.
    • While you should maybe be avoiding this scenario in your game anyway, fundamentally, the operation isn't inconsistent or wrong. It's just redundant.
@zicklag zicklag added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Nov 17, 2022
@zicklag zicklag changed the title parent != previous When Adding a Child to a Parent When It's Already a Child Panic at parent != previous When Adding a Child to a Parent When It's Already a Child Nov 17, 2022
@james7132 james7132 added A-Hierarchy Parent-child entity hierarchies and removed S-Needs-Triage This issue needs to be labelled labels Nov 17, 2022
@james7132 james7132 added the P-Crash A sudden unexpected crash label Nov 26, 2022
@bors bors bot closed this as completed in 70d7f80 Nov 28, 2022
taiyoungjang pushed a commit to taiyoungjang/bevy that referenced this issue Dec 15, 2022
# Objective

* Fix bevyengine#6668
* There is no need to panic when a parenting operation is redundant, as no invalid state is entered.

## Solution

Make `push_children` idempotent.
alradish pushed a commit to alradish/bevy that referenced this issue Jan 22, 2023
# Objective

* Fix bevyengine#6668
* There is no need to panic when a parenting operation is redundant, as no invalid state is entered.

## Solution

Make `push_children` idempotent.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
# Objective

* Fix bevyengine#6668
* There is no need to panic when a parenting operation is redundant, as no invalid state is entered.

## Solution

Make `push_children` idempotent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Hierarchy Parent-child entity hierarchies C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants