Skip to content

Commit 27e20df

Browse files
committed
Stageless: move final apply outside of spawned executor (#7445)
# Objective - After the multithreaded executor finishes running all the systems, we apply the buffers for any system that hasn't applied it's buffers. This is a courtesy apply for users who forget to order their systems before a apply_system_buffers. When checking stageless, it was found that this apply_system_buffers was running on the executor thread instead of the world's thread. This is a problem because anything with world access should be able to access nonsend resources. ## Solution - Move the final apply_system_buffers outside of the executor and outside of the scope, so it runs on the same thread that schedule.run is called on.
1 parent 3632076 commit 27e20df

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

crates/bevy_ecs/src/schedule_v3/executor/multi_threaded.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,6 @@ impl SystemExecutor for MultiThreadedExecutor {
178178
self.rebuild_active_access();
179179
}
180180
}
181-
182-
// SAFETY: all systems have completed
183-
let world = unsafe { &mut *world.get() };
184-
apply_system_buffers(&self.unapplied_systems, systems, world);
185-
self.unapplied_systems.clear();
186-
debug_assert!(self.unapplied_systems.is_clear());
187-
188-
debug_assert!(self.ready_systems.is_clear());
189-
debug_assert!(self.running_systems.is_clear());
190-
self.active_access.clear();
191-
self.evaluated_sets.clear();
192-
self.skipped_systems.clear();
193-
self.completed_systems.clear();
194181
};
195182

196183
#[cfg(feature = "trace")]
@@ -199,6 +186,20 @@ impl SystemExecutor for MultiThreadedExecutor {
199186
let executor = executor.instrument(executor_span);
200187
scope.spawn(executor);
201188
});
189+
190+
// Do one final apply buffers after all systems have completed
191+
// SAFETY: all systems have completed, and so no outstanding accesses remain
192+
let world = unsafe { &mut *world.get() };
193+
apply_system_buffers(&self.unapplied_systems, systems, world);
194+
self.unapplied_systems.clear();
195+
debug_assert!(self.unapplied_systems.is_clear());
196+
197+
debug_assert!(self.ready_systems.is_clear());
198+
debug_assert!(self.running_systems.is_clear());
199+
self.active_access.clear();
200+
self.evaluated_sets.clear();
201+
self.skipped_systems.clear();
202+
self.completed_systems.clear();
202203
}
203204
}
204205

0 commit comments

Comments
 (0)