Skip to content

Commit

Permalink
report a command system origin in case of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed May 19, 2021
1 parent 3c96131 commit 4c87867
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ trace_chrome = ["bevy_internal/trace_chrome"]
trace = ["bevy_internal/trace"]
wgpu_trace = ["bevy_internal/wgpu_trace"]

bevy_command_panic_origin = ["bevy_internal/bevy_command_panic_origin"]

# Image format support for texture loading (PNG and HDR are enabled by default)
hdr = ["bevy_internal/hdr"]
png = ["bevy_internal/png"]
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ categories = ["game-engines", "data-structures"]

[features]
trace = []
command_panic_origin = []
default = ["bevy_reflect"]

[dependencies]
Expand Down
23 changes: 21 additions & 2 deletions crates/bevy_ecs/src/system/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use crate::{
world::World,
};
use bevy_utils::tracing::debug;
use std::marker::PhantomData;
#[cfg(feature = "command_panic_origin")]
use bevy_utils::tracing::error;
#[cfg(feature = "command_panic_origin")]
use std::panic::{self, AssertUnwindSafe};
use std::{borrow::Cow, marker::PhantomData};

/// A [`World`] mutation.
pub trait Command: Send + Sync + 'static {
Expand All @@ -15,7 +19,9 @@ pub trait Command: Send + Sync + 'static {
/// A queue of [`Command`]s.
#[derive(Default)]
pub struct CommandQueue {
commands: Vec<Box<dyn Command>>,
pub(crate) commands: Vec<Box<dyn Command>>,
#[allow(unused)]
pub(crate) system_name: Option<Cow<'static, str>>,
}

impl CommandQueue {
Expand All @@ -24,7 +30,20 @@ impl CommandQueue {
pub fn apply(&mut self, world: &mut World) {
world.flush();
for command in self.commands.drain(..) {
// TODO: replace feature by proper error handling from commands
#[cfg(not(feature = "command_panic_origin"))]
command.write(world);
#[cfg(feature = "command_panic_origin")]
if panic::catch_unwind(AssertUnwindSafe(|| {
command.write(world);
}))
.is_err()
{
if let Some(system_name) = &self.system_name {
error!("panic while applying a command from {}", system_name);
}
panic!("panic applying a command");
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_ecs/src/system/into_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ impl SystemState {
pub fn set_non_send(&mut self) {
self.is_send = false;
}

pub fn name(&self) -> Cow<'static, str> {
self.name.clone()
}
}

/// Conversion trait to turn something into a [`System`].
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,11 @@ impl<'a> SystemParam for Commands<'a> {
unsafe impl SystemParamState for CommandQueue {
type Config = ();

fn init(_world: &mut World, _system_state: &mut SystemState, _config: Self::Config) -> Self {
Default::default()
fn init(_world: &mut World, system_state: &mut SystemState, _config: Self::Config) -> Self {
CommandQueue {
system_name: Some(system_state.name()),
..Default::default()
}
}

fn apply(&mut self, world: &mut World) {
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ x11 = ["bevy_winit/x11"]
# enable rendering of font glyphs using subpixel accuracy
subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"]

# enable tracing system origin of commands in case of command failure
bevy_command_panic_origin = ["bevy_ecs/command_panic_origin"]

# enable systems that allow for automated testing on CI
bevy_ci_testing = ["bevy_app/bevy_ci_testing"]

Expand Down

0 comments on commit 4c87867

Please sign in to comment.