Skip to content

Avoid panic when responding to ColliderConstructor[Hierarchy]#992

Open
eswartz wants to merge 1 commit into
avianphysics:mainfrom
eswartz:avoid-collider-panic
Open

Avoid panic when responding to ColliderConstructor[Hierarchy]#992
eswartz wants to merge 1 commit into
avianphysics:mainfrom
eswartz:avoid-collider-panic

Conversation

@eswartz

@eswartz eswartz commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Objective

  • Avoid a panic when an entity has ColliderConstructor[Hierarchy] but is despawned soon.

The command that inserts the new Collider or deletes the old ColliderConstructor... raises a panic if the target entity happened to be removed.

Inserting case:

ncountered an error in command `<bevy_ecs::system::commands::entity_command::insert<avian3d::collision::collider::parry::Collider>::{{closure}} as bevy_ecs::error::command_handling::CommandWithEntity<core::result::Result<(), bevy_ecs::world::error::EntityMutableFetchError>>>::with_entity::{{closure}}`: Entity despawned: The entity with ID 792v7 is invalid; its index now has generation 8.
Note that interacting with a despawned entity is the most common cause of this error but there are others

    If you were attempting to apply a command to this entity,
    and want to handle this error gracefully, consider using `EntityCommands::queue_handled` or `queue_silenced`.
   0: from<bevy_ecs::world::error::EntityMutableFetchError>
             at /home/ejs/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.18.1/src/error/bevy_error.rs:115:28
   1: into<bevy_ecs::world::error::EntityMutableFetchError, bevy_ecs::error::bevy_error::BevyError>
             at /home/ejs/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/convert/mod.rs:778:9
   2: {closure#0}<bevy_ecs::error::command_handling::{impl#3}::with_entity::{closure_env#0}<bevy_ecs::system::commands::entity_command::insert::{closure_env#0}<avian3d::collision::collider::parry::Collider>>, (), bevy_ecs::world::error::EntityMutableFetchError>
             at /home/ejs/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.18.1/src/error/command_handling.rs:48:21
   3: apply<bevy_ecs::error::command_handling::{impl#0}::handle_error::{closure_env#0}<bevy_ecs::error::command_handling::{impl#3}::with_entity::{closure_env#0}<bevy_ecs::system::commands::entity_command::insert::{closure_env#0}<avian3d::collision::collider::parry::Collider>>, (), bevy_ecs::world::error::EntityMutableFetchError>, ()>
             at /home/ejs/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.18.1/src/system/commands/command.rs:62:9
   4: {closure#0}<bevy_ecs::error::command_handling::{impl#0}::handle_error::{closure_env#0}<bevy_ecs::error::command_handling::{impl#3}::with_entity::{closure_env#0}<bevy_ecs::system::commands::entity_command::insert::{closure_env#0}<avian3d::collision::collider::parry::Collider>>, (), bevy_ecs::world::error::EntityMutableFetchError>>

Deleting case:

2026-06-05T16:34:37.047191Z  WARN bevy_ecs::error::handler: Encountered an error in command `<bevy_ecs::system::commands::entity_command::remove<avian3d::collision::collider::constructor::ColliderConstructor>::{{closure}} as bevy_ecs::error::command_handling::CommandWithEntity<core::result::Result<(), bevy_ecs::world::error::EntityMutableFetchError>>>::with_entity::{{closure}}`: Entity despawned: The entity with ID 861v1 is invalid; its index now has generation 2.
Note that interacting with a despawned entity is the most common cause of this error but there are others

    If you were attempting to apply a command to this entity,
    and want to handle this error gracefully, consider using `EntityCommands::queue_handled` or `queue_silenced`.
   0: from<bevy_ecs::world::error::EntityMutableFetchError>
             at /home/ejs/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.18.1/src/error/bevy_error.rs:115:28
   1: into<bevy_ecs::world::error::EntityMutableFetchError, bevy_ecs::error::bevy_error::BevyError>
             at /home/ejs/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/convert/mod.rs:778:9
   2: {closure#0}<bevy_ecs::error::command_handling::{impl#3}::with_entity::{closure_env#0}<bevy_ecs::system::commands::entity_command::remove::{closure_env#0}<avian3d::collision::collider::constructor::ColliderConstructor>>, (), bevy_ecs::world::error::EntityMutableFetchError>
             at /home/ejs/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.18.1/src/error/command_handling.rs:36:21
...

Solution

  • Use try_insert and try_remove as recommended.

Testing

  • Did you test these changes? If so, how?

Tested with my own project which was triggering this.

@Jondolf Jondolf added C-Bug Something isn't working A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality P-Crash A sudden unexpected crash labels Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality C-Bug Something isn't working P-Crash A sudden unexpected crash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants