Skip to content

Commit

Permalink
Fix PipeSystem panicking with exclusive systems (bevyengine#6698)
Browse files Browse the repository at this point in the history
Without this fix, piped systems containing exclusive systems fail to run, giving a runtime panic.
With this PR, running piped systems that contain exclusive systems now works.

## Explanation of the bug

This is because, unless overridden, the default implementation of `run` from the `System` trait simply calls `run_unsafe`. That is not valid for exclusive systems. They must always be called via `run`, as `run_unsafe` takes `&World` instead of `&mut World`.

Trivial reproduction example:
```rust
fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_system(exclusive.pipe(another))
        .run();
}

fn exclusive(_world: &mut World) {}
fn another() {}
```
If you run this, you will get a panic 'Cannot run exclusive systems with a shared World reference' and the backtrace shows how bevy (correctly) tries to call the `run` method (because the system is exclusive), but it is the implementation from the `System` trait (because `PipeSystem` does not have its own), which calls `run_unsafe` (incorrect):
 - 3: <bevy_ecs::system::system_piping::PipeSystem<SystemA,SystemB> as bevy_ecs::system::system::System>::run_unsafe
 - 4: bevy_ecs::system::system::System::run
  • Loading branch information
inodentry authored and ItsDoot committed Feb 1, 2023
1 parent 157a60f commit 1bf496f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/bevy_ecs/src/system/system_piping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ impl<SystemA: System, SystemB: System<In = SystemA::Out>> System for PipeSystem<
self.system_b.run_unsafe(out, world)
}

// needed to make exclusive systems work
fn run(&mut self, input: Self::In, world: &mut World) -> Self::Out {
let out = self.system_a.run(input, world);
self.system_b.run(out, world)
}

fn apply_buffers(&mut self, world: &mut World) {
self.system_a.apply_buffers(world);
self.system_b.apply_buffers(world);
Expand Down

0 comments on commit 1bf496f

Please sign in to comment.