Skip to content

Commit b1c1aac

Browse files
ElliottjPiercemockersf
authored andcommitted
Ensure spawning related entities in an OnAdd observer downstream of a 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.
1 parent 30a022f commit b1c1aac

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

crates/bevy_ecs/src/observer/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,23 @@ mod tests {
16481648
assert_eq!(vec!["event", "event"], world.resource::<Order>().0);
16491649
}
16501650

1651+
// Originally for https://github.com/bevyengine/bevy/issues/18452
1652+
#[test]
1653+
fn observer_modifies_relationship() {
1654+
fn on_add(trigger: Trigger<OnAdd, A>, mut commands: Commands) {
1655+
commands
1656+
.entity(trigger.target())
1657+
.with_related::<crate::hierarchy::ChildOf>(|rsc| {
1658+
rsc.spawn_empty();
1659+
});
1660+
}
1661+
1662+
let mut world = World::new();
1663+
world.add_observer(on_add);
1664+
world.spawn(A);
1665+
world.flush();
1666+
}
1667+
16511668
// Regression test for https://github.com/bevyengine/bevy/issues/14467
16521669
// Fails prior to https://github.com/bevyengine/bevy/pull/15398
16531670
#[test]

crates/bevy_ecs/src/world/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ impl World {
11741174

11751175
// SAFETY: command_queue is not referenced anywhere else
11761176
if !unsafe { self.command_queue.is_empty() } {
1177-
self.flush_commands();
1177+
self.flush();
11781178
entity_location = self
11791179
.entities()
11801180
.get(entity)

0 commit comments

Comments
 (0)