bevyengine / bevyengine/bevy

ObserverSystem cannot be an ExclusiveSystem and fail at runtime

Open
#14,507 4 comments 1 reaction 0 assignees View on GitHub
A-ECS C-Docs C-Usability
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

Bevy version 0.14

A function that can be used with an Observer that takes an ExclusiveSystemParam will panic at runtime when invoked.
```rs
#[derive(Event)]
struct Foo;

fn main() {
App::new()
.add_plugins(MinimalPlugins.set(ScheduleRunnerPlugin::run_once()))
.observe(|_: In>, _: &mut World| {
println!("Hello, World!");
})
.add_systems(Update, |mut commands: Commands| {
commands.trigger(Foo);
})
.run();
}
```
This will panic when using `Commands::trigger` or `World::trigger` even though in either case I would expect exclusive world access at the point of invocation. Looking slightly deeper, this is likely because `Trigger` holds a `&'w E: Event` rather than owning the event such that it is decoupled from the world.

The panic produces the error:
> thread 'main' panicked at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/exclusive_function_system.rs:99:9:
Cannot run exclusive systems with a shared World reference

And its backtrace (RUST_BACKTRACE=1):
```
stack backtrace:
0: rust_begin_unwind
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/std/src/panicking.rs:665:5
1: core::panicking::panic_fmt
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/panicking.rs:74:14
2: as bevy_ecs::system::system::System>::run_unsafe
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/exclusive_function_system.rs:99:9
3: bevy_ecs::observer::runner::observer_system_runner
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/observer/runner.rs:409:9
4: bevy_ecs::observer::Observers::invoke::{{closure}}
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/observer/mod.rs:192:13
5: core::ops::function::impls:: for &mut F>::call_mut
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/ops/function.rs:294:13
6: core::iter::traits::iterator::Iterator::for_each::call::{{closure}}
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/iter/traits/iterator.rs:815:29
7: as core::iter::traits::iterator::Iterator>::fold::{{closure}}
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.14.5/src/map.rs:4749:13
8: hashbrown::raw::inner::RawIterRange::fold_impl
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.14.5/src/raw/mod.rs:3880:23
9: as core::iter::traits::iterator::Iterator>::fold
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.14.5/src/raw/mod.rs:4151:18
10: as core::iter::traits::iterator::Iterator>::fold
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.14.5/src/map.rs:4747:9
11: core::iter::traits::iterator::Iterator::for_each
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/iter/traits/iterator.rs:818:9
12: bevy_ecs::observer::Observers::invoke
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/observer/mod.rs:204:9
13: bevy_ecs::world::deferred_world::DeferredWorld::trigger_observers_with_data
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/deferred_world.rs:368:9
14: bevy_ecs::observer::trigger_event::trigger_event
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/observer/trigger_event.rs:61:13
15: as bevy_ecs::world::Command>::apply
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/observer/trigger_event.rs:20:9
16: bevy_ecs::world::command_queue::RawCommandQueue::push::{{closure}}
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/command_queue.rs:182:25
17: core::ops::function::FnOnce::call_once
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/ops/function.rs:250:5
18: bevy_ecs::world::command_queue::RawCommandQueue::apply_or_drop_queued::{{closure}}
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/command_queue.rs:272:26
19: as core::ops::function::FnOnce<()>>::call_once
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/panic/unwind_safe.rs:272:9
20: std::panicking::try::do_call
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/std/src/panicking.rs:557:40
21: ___rust_try
22: std::panicking::try
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/std/src/panicking.rs:521:19
23: std::panic::catch_unwind
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/std/src/panic.rs:350:14
24: bevy_ecs::world::command_queue::RawCommandQueue::apply_or_drop_queued
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/command_queue.rs:265:26
25: bevy_ecs::world::command_queue::CommandQueue::apply
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/command_queue.rs:100:13
26: ::apply
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/command_queue.rs:327:9
27: as bevy_ecs::system::system_param::SystemParam>::apply
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/system_param.rs:1040:9
28: <(P0,P1) as bevy_ecs::system::system_param::SystemParam>::apply
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/system_param.rs:1456:19
29: bevy_ecs::system::commands::_::::apply
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/commands/mod.rs:115:13
30: <(P0,) as bevy_ecs::system::system_param::SystemParam>::apply
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/system_param.rs:1456:19
31: as bevy_ecs::system::system::System>::apply_deferred
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/function_system.rs:542:9
32: bevy_ecs::schedule::executor::multi_threaded::apply_deferred::{{closure}}
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/schedule/executor/multi_threaded.rs:704:13
33: core::ops::function::FnOnce::call_once
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/ops/function.rs:250:5
34: as core::ops::function::FnOnce<()>>::call_once
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/panic/unwind_safe.rs:272:9
35: std::panicking::try::do_call
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/std/src/panicking.rs:557:40
36: ___rust_try
37: std::panicking::try
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/std/src/panicking.rs:521:19
38: std::panic::catch_unwind
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/std/src/panic.rs:350:14
39: bevy_ecs::schedule::executor::multi_threaded::apply_deferred
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/schedule/executor/multi_threaded.rs:703:19
40: ::run
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/schedule/executor/multi_threaded.rs:240:23
41: bevy_ecs::schedule::schedule::Schedule::run
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/schedule/schedule.rs:386:9
42: bevy_ecs::world::World::try_run_schedule::{{closure}}
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/mod.rs:2743:55
43: bevy_ecs::world::World::try_schedule_scope
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/mod.rs:2676:21
44: bevy_ecs::world::World::try_run_schedule
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/mod.rs:2743:9
45: bevy_app::main_schedule::Main::run_main::{{closure}}
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.14.0/src/main_schedule.rs:227:25
46: bevy_ecs::world::World::resource_scope
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/mod.rs:1886:22
47: bevy_app::main_schedule::Main::run_main
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.14.0/src/main_schedule.rs:225:9
48: core::ops::function::FnMut::call_mut
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/ops/function.rs:166:5
49: core::ops::function::impls:: for &mut F>::call_mut
at /rustc/8bfcae730a5db2438bbda72796175bba21427be1/library/core/src/ops/function.rs:294:13
50: Out>>::run::call_inner
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/exclusive_function_system.rs:218:21
51: Out>>::run
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/exclusive_function_system.rs:221:17
52: as bevy_ecs::system::system::System>::run::{{closure}}
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/exclusive_function_system.rs:111:23
53: bevy_ecs::world::World::last_change_tick_scope
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/world/mod.rs:2215:9
54: as bevy_ecs::system::system::System>::run
at .cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.14.0/src/system/exclusive_function_system.rs:103:9
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.