Skip to content

Prevent exclusive systems from being used as observers #19033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025

Conversation

chescock
Copy link
Contributor

@chescock chescock commented May 2, 2025

Objective

Prevent using exclusive systems as observers. Allowing them is unsound, because observers are only expected to have DeferredWorld access, and the observer infrastructure will keep pointers that are invalidated by the creation of &mut World.

See https://github.com/bevyengine/bevy/actions/runs/14778342801/job/41491517847?pr=19011 for a MIRI failure in a recent PR caused by an exclusive system being used as an observer in a test.

Solution

Have Observer::new panic if System::is_exclusive() is true. Document that method, and methods that call it, as panicking.

(It should be possible to express this in the type system so that the calls won't even compile, but I did not want to attempt that.)

Testing

Added a unit test that calls World::add_observer with an exclusive system.

@alice-i-cecile alice-i-cecile added this to the 0.17 milestone May 2, 2025
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events P-Unsound A bug that results in undefined compiler behavior X-Uncontroversial This work is generally agreed upon S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 2, 2025
@mcobzarenco
Copy link
Contributor

Is there any chance this can be backported to 0.16.1? Given it's a soundness issue I was thinking

@chescock
Copy link
Contributor Author

chescock commented May 5, 2025

I realized this might require a migration guide if anyone is doing this today... but then I thought it would be even better to put the guide in the error message so that anyone trying this in the future also gets guidance. So I did that instead!

@hukasu
Copy link
Contributor

hukasu commented May 5, 2025

having it on the error is very smart, but i don't think it removes the need of the migration guide

@alice-i-cecile alice-i-cecile modified the milestones: 0.17, 0.16.1 May 5, 2025
@alice-i-cecile alice-i-cecile modified the milestones: 0.16.1, 0.17 May 5, 2025
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 5, 2025
@alice-i-cecile
Copy link
Member

(It should be possible to express this in the type system so that the calls won't even compile, but I did not want to attempt that.)

Agreed, we should tackle this in follow-up at some point.

@mockersf mockersf added this pull request to the merge queue May 5, 2025
Merged via the queue into bevyengine:main with commit 5f936ae May 5, 2025
34 checks passed
andrewzhurov pushed a commit to andrewzhurov/bevy that referenced this pull request May 17, 2025
)

# Objective

Prevent using exclusive systems as observers. Allowing them is unsound,
because observers are only expected to have `DeferredWorld` access, and
the observer infrastructure will keep pointers that are invalidated by
the creation of `&mut World`.

See
https://github.com/bevyengine/bevy/actions/runs/14778342801/job/41491517847?pr=19011
for a MIRI failure in a recent PR caused by an exclusive system being
used as an observer in a test.

## Solution

Have `Observer::new` panic if `System::is_exclusive()` is true. Document
that method, and methods that call it, as panicking.

(It should be possible to express this in the type system so that the
calls won't even compile, but I did not want to attempt that.)

## Testing

Added a unit test that calls `World::add_observer` with an exclusive
system.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior P-Unsound A bug that results in undefined compiler behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants