Skip to content

Use schedule_scope instead of a manual (broken) hokey-pokey#23735

Open
andriyDev wants to merge 3 commits intobevyengine:mainfrom
andriyDev:fix-schedule-hokey-pokey
Open

Use schedule_scope instead of a manual (broken) hokey-pokey#23735
andriyDev wants to merge 3 commits intobevyengine:mainfrom
andriyDev:fix-schedule-hokey-pokey

Conversation

@andriyDev
Copy link
Copy Markdown
Contributor

Objective

  • Previously, if there was an error during the schedule initialize, the schedule would not be re-inserted into the Schedules resource.
  • The bespoke hokey-pokey was confusing enough, and Alice even pointed it out! I just didn't know about this method :(

Solution

  • Use schedule_scope which does all the business of hokey-pokey, and this currently handles errors for us!

Testing

  • Added a test for this.

@andriyDev andriyDev added C-Bug An unexpected or incorrect behavior D-Trivial Nice and easy! A great choice to get started with Bevy A-Dev-Tools Tools used to debug Bevy applications. S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 9, 2026
@andriyDev andriyDev added this to the 0.19 milestone Apr 9, 2026
Copy link
Copy Markdown
Contributor

@urben1680 urben1680 left a comment

Choose a reason for hiding this comment

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

None of my notes are really blockers so this is an approve.

Comment on lines +103 to 108
let result = world.schedule_scope(label, |world, schedule| schedule.initialize(world));
let Some(build_metadata) = result? else {
return Err(
"The schedule has already been built, so we can't collect its system data".into(),
);
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Probably a matter of taste but I think this is a bit nicer:

let build_metadata = world
    .schedule_scope(label, |world, schedule| schedule.initialize(world))?
    .ok_or("The schedule has already been built, so we can't collect its system data")?;

Not using a commitable suggestion here because I am unsure if the compiler is happy with that. If you do need an .into() on the str, you would want to use ok_or_else instead.

Comment on lines 93 to 97
let labels = schedules
.iter()
.map(|schedule| schedule.1.label())
.collect::<Vec<_>>();
let mut label_to_build_metadata = HashMap::new();
Copy link
Copy Markdown
Contributor

@urben1680 urben1680 Apr 9, 2026

Choose a reason for hiding this comment

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

I hope there is no cursed situation where initializing schedules creates new schedules as their labels are not included here yet. 🙃 Or equally cursed, remove schedules that would force you to use try_schedule_scope.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think that's possible. Schedule::initialize is only about initializing the graph data structure, not the user systems or anything. So unless bevy_ecs itself does something evil, this should be safe. I don't think we need to defend against this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But the docs say the systems get initialized here, so you could do funky stuff on custom System/Systemparam/WorldQuery impls.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh you're totally right! My mistake!

Maybe it is just better to use try_schedule_scope just in case...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If custom impl actually do add a new schedule, it is not unsafe if it does not get initialized here, right? I would shrug it off as totally unrealistic to happen, but when it gets to UB it might be worth a second thought.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No it's not unsafe, but it might panic. That's all. Not horrific, but not ideal.

Copy link
Copy Markdown
Contributor

@kfc35 kfc35 left a comment

Choose a reason for hiding this comment

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

This looks good to me

I can’t speak to whether systems creating/removing schedules is something we should be mindful of… I’m leaning towards no but I’m open to being corrected

@kfc35 kfc35 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 Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Dev-Tools Tools used to debug Bevy applications. C-Bug An unexpected or incorrect behavior D-Trivial Nice and easy! A great choice to get started with Bevy S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants