Skip to content

Commit

Permalink
Use try_insert to prevent panics (#299)
Browse files Browse the repository at this point in the history
* Use `try_insert` to prevent `panic`s

When using `insert` instead of `try_insert` on `EntityCommand`, it can
happen that the system panics, when the entity does not exist, even
when the `EntityCommand` has been accessed with `get_entity(...)`.
The following example demonstrates the problem:

```rust
if let Some(mut entity_commands) = commands.get_entity(entity) {
    // at this point some other system despawns `entity` concurrently
    entity_commands.insert(bundle);
    //              ^^^^^^ this will panic
}
```

This is also documented in the official bevy docs:
https://docs.rs/bevy/0.12.1/bevy/ecs/system/struct.Commands.html#method.get_entity

* replace other uses of insert with try_insert

* Update changelog

---------

Co-authored-by: Jan Riemer <[email protected]>
Co-authored-by: Aevyrie <[email protected]>
  • Loading branch information
3 people authored Feb 24, 2024
1 parent fd08e46 commit b551d21
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# UNRELEASED

- Fixed: replaced uses of `.insert` with `.try_insert`, where they could potentially panic.
- Fixed: replace all `.single` calls with matched `.get_single` calls to avoid crashing in
environments where there is no window available
- Fixed: sprite picking depth is now consistent with other picking backends.
Expand Down
2 changes: 1 addition & 1 deletion backends/bevy_picking_egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn update_settings(
.remove::<bevy_picking_selection::NoDeselect>(),
false => commands
.entity(entity)
.insert(bevy_picking_selection::NoDeselect),
.try_insert(bevy_picking_selection::NoDeselect),
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_picking_core/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub fn update_interactions(
if let Ok(mut interaction) = interact.get_mut(hovered_entity) {
*interaction = new_interaction;
} else if let Some(mut entity_commands) = commands.get_entity(hovered_entity) {
entity_commands.insert(new_interaction);
entity_commands.try_insert(new_interaction);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_picking_highlight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub fn get_initial_highlight_asset<T: Asset>(
match highlighting_query.get_mut(entity) {
Ok(Some(mut highlighting)) => highlighting.initial = material.to_owned(),
_ => {
commands.entity(entity).insert(InitialHighlight {
commands.entity(entity).try_insert(InitialHighlight {
initial: material.to_owned(),
});
}
Expand Down

0 comments on commit b551d21

Please sign in to comment.