Skip to content
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

Reduce runtime panics through SystemParam validation #15276

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

MiniaczQ
Copy link
Contributor

@MiniaczQ MiniaczQ commented Sep 17, 2024

Objective

The goal of this PR is to introduce SystemParam validation in order to reduce runtime panics.

Fixes #15265

Solution

SystemParam now has a new method validate_param(...) -> bool, which takes immutable variants of get_param arguments. The returned value indicates whether the parameter can be acquired from the world. If parameters cannot be acquired for a system, it won't be executed, similarly to run conditions. This reduces panics when using params like Res, ResMut, etc. as well as allows for new, ergonomic params like various kinds of Single (Query::single) or NonEmptyQuery.

Param validation happens at the level of executors. System validation happens after updating archetypes and checking access, but before checking system/system set run conditions. (Technically, we can run validation right after updating archetypes and before checking access, but that'd require a bigger refactor in the executors.) The operation short circuits and marks system as ready, skipping all system and system set run conditions evaluation.

System and system set run conditions are validated per system/system set, if params are invalid the conditions are marked as not met.

Warning about system skipping will be part of another PR.

Testing

Two executor tests check that all executors:

  • skip systems which have invalid parameters,
    • piped systems get skipped together,
    • dependent systems still run correctly.
  • skip systems with invalid run conditions
    • system conditions have invalid parameters,
    • system set conditions have invalid parameters.

Migration Guide

  • Manual SystemParam implementations need to implement validate_param.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use C-Needs-Release-Note Work that should be called out in the blog due to impact X-Blessed Has a large architectural impact or tradeoffs, but the design has been endorsed by decision makers C-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Sep 17, 2024
@alice-i-cecile
Copy link
Member

The opinions of the SME-ECS (@JoJoJet @maniwani @james7132 and myself) and frankly the rest of the ECS community are very positive on the broad direction of this work, so I'm marking it as X-Blessed.

@MiniaczQ MiniaczQ marked this pull request as ready for review September 18, 2024 14:46
@MiniaczQ
Copy link
Contributor Author

MiniaczQ commented Sep 18, 2024

validate_param is technically not unsafe, but we might want to mark it as so.
In theory, this method shouldn't access component values, despite having a readonly world access.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very pleased about how non-intrusive this is!

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As follow-up, we should make the warning behavior configurable, as discussed in #15265.

@MiniaczQ
Copy link
Contributor Author

Blocked on #15289 (if we want miri resolved)
Also needs D-Unsafe tag

@alice-i-cecile alice-i-cecile added the D-Unsafe Touches with unsafe code in some way label Sep 18, 2024
github-merge-queue bot pushed a commit that referenced this pull request Sep 19, 2024
# Objective

- I was running miri locally to check the UB in #15276 and it detected
an unrelated memory leak, due to the `RawCommandQueue` changes. (I
probably should have turned the leak detection off because we do
purposely leak interned string labels and I assume that's why CI didn't
detect it.)

## Solution

- The memory allocated to `RawCommandQueue` needs to be manually
dropped. This was being done for `bytes` and `cursor`, but was missed
for `panic_recovery`.

## Testing

- Ran miri locally and the related memory leaks errors when away.
@Victoronz
Copy link
Contributor

Blocked on #15309, #15289 is no longer relevant

@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Sep 19, 2024
@alice-i-cecile alice-i-cecile added this to the 0.15 milestone Sep 19, 2024
@iiYese
Copy link
Contributor

iiYese commented Sep 20, 2024

I'll decline to review, too deep in the ECS internals for my experience. Can't say anything confidently.

@iiYese iiYese removed their request for review September 20, 2024 11:11
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-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide C-Needs-Release-Note Work that should be called out in the blog due to impact C-Usability A simple quality-of-life change that makes Bevy easier to use D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Blessed Has a large architectural impact or tradeoffs, but the design has been endorsed by decision makers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Systems should be skipped if their resources cannot be fetched
5 participants