Skip to content

Commit 8253eab

Browse files
authored
Allow DiagnosticsBuffer to be applied even if DiagnosticsStore is missing. (#21550)
# Objective - Workaround #21549. ## Solution - Avoid panicking in `DiagnosticsBuffer::apply` if the world is missing the `DiagnosticsStore`. - Clear the buffer if `DiagnosticsStore` is missing to prevent growing the buffer. ## Testing - None.
1 parent 16a6a96 commit 8253eab

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

crates/bevy_diagnostic/src/diagnostic.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,17 @@ impl SystemBuffer for DiagnosticsBuffer {
381381
_system_meta: &bevy_ecs::system::SystemMeta,
382382
world: &mut bevy_ecs::world::World,
383383
) {
384-
let mut diagnostics = world.resource_mut::<DiagnosticsStore>();
384+
let Some(mut diagnostics) = world.get_resource_mut::<DiagnosticsStore>() else {
385+
// `SystemBuffer::apply` is called even if the system never runs. If a user uses
386+
// `If<Diagnostics>`, this buffer will be applied even if we are missing
387+
// `DiagnosticsStore`. So be permissive to allow these cases. See
388+
// https://github.com/bevyengine/bevy/issues/21549 for more.
389+
390+
// Clear the buffer since we have nowhere to put those metrics and we don't want them to
391+
// grow without bound.
392+
self.0.clear();
393+
return;
394+
};
385395
for (path, measurement) in self.0.drain() {
386396
if let Some(diagnostic) = diagnostics.get_mut(&path) {
387397
diagnostic.add_measurement(measurement);

0 commit comments

Comments
 (0)