Fix unsound observer initialization - #25656
Merged
alice-i-cecile merged 3 commits intoSep 5, 2026
Merged
Conversation
alice-i-cecile
requested changes
Sep 2, 2026
alice-i-cecile
left a comment
Member
There was a problem hiding this comment.
Can you add a regression test for the pathological observer please? That seems like a nice little addition to the miri test suite. Seems like a nice fix otherwise though!
alice-i-cecile
approved these changes
Sep 2, 2026
hymm
approved these changes
Sep 2, 2026
Member
|
CI failure, then I'll merge <3 |
Contributor
Author
Augh, conditional compilation again! Hopefully that push fixed it. There is some danger that I'll make a PR soon to make |
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Sep 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
Fix potential UB during observer initialization that was noticed in #25507 (comment).
When calling
System::initializeon an observer system, we pass&mut Worldwhile also holding a&mutto the observer system itself. This is usually fine, since none of the first-party systems or system params will do anything invalid. Even adding observers is fine because the system is stored behind a box and won't be moved if we reallocate a table. But it's valid for a system to despawn the observer during its owninitialize, which would then deallocate theSystemwhile it was runninginitialize!Solution
Store the system in the
Observercomponent in anOption, and remove it before callinginitialize, so that we can hold&mut Worldwithout conflict.Note that we were already doing that for conditions, so this does not need an additional component lookup. I was even able to remove one lookup by replacing the system and conditions at the same time.
Also skip the call to
register_observerif the observer was despawned before then. I didn't investigate whether that call would actually cause problems, but I can't see any way that it would be helpful in that case.