Skip to content

Despawning an entity that has a relationship pair causes a crash on Relationship::on_replace #19373

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

Open
hukasu opened this issue May 26, 2025 · 0 comments
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@hukasu
Copy link
Contributor

hukasu commented May 26, 2025

Bevy version

0.16

What you did

#[derive(Component)]
#[relationship(relationship_target = AT)]
struct A(Entity);

#[derive(Component)]
#[relationship_target(relationship = A, linked_spawn)]
struct AT(Vec<Entity>);

#[derive(Component)]
#[relationship(relationship_target = BT)]
struct B(Entity);

#[derive(Component)]
#[relationship_target(relationship = B)]
struct BT(Vec<Entity>);

#[test]
fn remove_entity_with_multiple_relationships() {
    let mut world = World::new();

    let a = world.spawn_empty().id();
    let b = world.spawn_empty().id();

    world.entity_mut(a).insert(B(b));
    assert_eq!(world.entity(b).get::<BT>().unwrap().0[0], a);

    world.entity_mut(b).insert(A(a));
    assert_eq!(world.entity(a).get::<AT>().unwrap().0[0], b);

    world.despawn(a);
}

What went wrong

Crash report

thread 'remove_entity_with_multiple_relationships' panicked at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/error/handler.rs:141:1:
Encountered an error in command `<<relationship::B as bevy_ecs::relationship::Relationship>::on_replace::{{closure}} as bevy_ecs::error::command_handling::CommandWithEntity<core::result::Result<(), bevy_ecs::world::error::EntityMutableFetchError>>>::with_entity::{{closure}}`: The entity with ID 1v1 does not exist (enable `track_location` feature for more details)
   1: <T as core::convert::Into<U>>::into
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/core/src/convert/mod.rs:767:9
   2: <C as bevy_ecs::error::command_handling::HandleError<core::result::Result<T,E>>>::handle_error_with::{{closure}}
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/error/command_handling.rs:37:17
   3: <F as bevy_ecs::system::commands::command::Command<Out>>::apply
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/system/commands/command.rs:62:9
   4: bevy_ecs::world::command_queue::RawCommandQueue::push::{{closure}}
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/command_queue.rs:173:25
   5: core::ops::function::FnOnce::call_once
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
   6: bevy_ecs::world::command_queue::RawCommandQueue::apply_or_drop_queued::{{closure}}
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/command_queue.rs:263:26
   7: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272:9
   8: std::panicking::try::do_call
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/std/src/panicking.rs:589:40
   9: std::panicking::try
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/std/src/panicking.rs:552:19
  10: std::panic::catch_unwind
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/std/src/panic.rs:359:14
  11: bevy_ecs::world::command_queue::RawCommandQueue::apply_or_drop_queued
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/command_queue.rs:268:30
  12: bevy_ecs::world::World::flush_commands
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/mod.rs:2934:17
  13: bevy_ecs::world::World::flush
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/mod.rs:2957:9
  14: bevy_ecs::world::entity_ref::EntityWorldMut::despawn_with_caller
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/entity_ref.rs:2609:15
  15: bevy_ecs::world::World::despawn_with_caller
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/mod.rs:1424:9
  16: bevy_ecs::world::World::despawn
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/mod.rs:1393:29
  17: relationship::remove_entity_with_multiple_relationships
             at ./tests/relationship.rs:32:5
  18: relationship::remove_entity_with_multiple_relationships::{{closure}}
             at ./tests/relationship.rs:20:47
  19: core::ops::function::FnOnce::call_once
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  20: core::ops::function::FnOnce::call_once
             at /rustc/4d051fb306e661654d088892e02e69b8c0c39d43/library/core/src/ops/function.rs:250:5
note: Some "noisy" backtrace lines have been filtered out. Run with `BEVY_BACKTRACE=full` for a verbose backtrace.

stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/4d051fb306e661654d088892e02e69b8c0c39d43/library/std/src/panicking.rs:697:5
   1: core::panicking::panic_fmt
             at /rustc/4d051fb306e661654d088892e02e69b8c0c39d43/library/core/src/panicking.rs:75:14
   2: bevy_ecs::error::handler::panic
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/error/handler.rs:129:9
   3: bevy_ecs::error::handler::panic{{reify.shim}}
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/error/handler.rs:141:1
   4: <C as bevy_ecs::error::command_handling::HandleError<core::result::Result<T,E>>>::handle_error_with::{{closure}}
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/error/command_handling.rs:36:25
   5: <F as bevy_ecs::system::commands::command::Command<Out>>::apply
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/system/commands/command.rs:62:9
   6: bevy_ecs::world::command_queue::RawCommandQueue::push::{{closure}}
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/command_queue.rs:173:25
   7: core::ops::function::FnOnce::call_once
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
   8: bevy_ecs::world::command_queue::RawCommandQueue::apply_or_drop_queued::{{closure}}
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/command_queue.rs:263:26
   9: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272:9
  10: std::panicking::try::do_call
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/std/src/panicking.rs:589:40
  11: std::panicking::try
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/std/src/panicking.rs:552:19
  12: std::panic::catch_unwind
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/std/src/panic.rs:359:14
  13: bevy_ecs::world::command_queue::RawCommandQueue::apply_or_drop_queued
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/command_queue.rs:268:30
  14: bevy_ecs::world::World::flush_commands
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/mod.rs:2934:17
  15: bevy_ecs::world::World::flush
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/mod.rs:2957:9
  16: bevy_ecs::world::entity_ref::EntityWorldMut::despawn_with_caller
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/entity_ref.rs:2609:15
  17: bevy_ecs::world::World::despawn_with_caller
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/mod.rs:1424:9
  18: bevy_ecs::world::World::despawn
             at /home/hukasu/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/world/mod.rs:1393:29
  19: relationship::remove_entity_with_multiple_relationships
             at ./tests/relationship.rs:32:5
  20: relationship::remove_entity_with_multiple_relationships::{{closure}}
             at ./tests/relationship.rs:20:47
  21: core::ops::function::FnOnce::call_once
             at /nix/store/x1nk497xq60bxmbfsi6baspb7mdawbna-rust-default-1.89.0-nightly-2025-05-19/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  22: core::ops::function::FnOnce::call_once
             at /rustc/4d051fb306e661654d088892e02e69b8c0c39d43/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@hukasu hukasu added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels May 26, 2025
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events P-Crash A sudden unexpected crash S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! and removed S-Needs-Triage This issue needs to be labelled labels May 26, 2025
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-Crash A sudden unexpected crash S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
Development

No branches or pull requests

2 participants