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

Fix Plugin::build detection #8103

Merged
merged 3 commits into from
Mar 17, 2023
Merged

Fix Plugin::build detection #8103

merged 3 commits into from
Mar 17, 2023

Conversation

geieredgar
Copy link
Contributor

Objective

  • Currently, the detection of calling App::run inside Plugin::build fails if that plugin adds another plugin before calling run, because we don't keep track of recursive build calls.

Solution

  • Instead of using a simple boolean flag, use a counter to keep track of the number of currently building plugins.
  • Adjust the cant_call_app_run_from_plugin_build test to add another plugin before calling run.

@@ -765,9 +765,9 @@ impl App {
plugin_name: plugin.name().to_string(),
})?;
}
self.is_building_plugin = true;
self.building_plugin_counter += 1;
Copy link
Member

Choose a reason for hiding this comment

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

It's unlikely, but these should always be checked adds and subtractions. If this overflows (highly unlikely, though still incorrect), it can result in missing the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have to disagree, it's not highly unlikely, it's impossible. usize is large enough to store every memory address and for each recursion depth, we need to at least keep track of Box<dyn Plugin>. We will run out of memory addresses much sooner than we will reach usize::MAX.

crates/bevy_app/src/app.rs Outdated Show resolved Hide resolved
@james7132 james7132 added C-Bug An unexpected or incorrect behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it A-App Bevy apps and plugins labels Mar 16, 2023
@james7132 james7132 added this to the 0.10.1 milestone Mar 16, 2023
@@ -765,9 +765,9 @@ impl App {
plugin_name: plugin.name().to_string(),
})?;
}
self.is_building_plugin = true;
self.building_plugin_counter += 1;
Copy link
Member

Choose a reason for hiding this comment

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

Probably not something we need to fix in this PR, but this leaves App in an incorrect state if the plugin build panics. We may want to use bevy_utils::CallOnDrop here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OnDrop does not work here because we pass self to Plugin::build. I've used catch_unwind and unwind_resume instead. I am not sure if using AssertUnwindSafe here is incorrect, but I think this shouldn't be an issue because we are resuming the unwind before we can observe any broken invariants.

Copy link
Member

@mockersf mockersf left a comment

Choose a reason for hiding this comment

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

I would prefer to handle this through the type system, but this PR fixes the issue

@cart cart added this pull request to the merge queue Mar 17, 2023
Merged via the queue into bevyengine:main with commit 67afd21 Mar 17, 2023
Shfty pushed a commit to shfty-rust/bevy that referenced this pull request Mar 19, 2023
Shfty pushed a commit to shfty-rust/bevy that referenced this pull request Mar 19, 2023
mockersf pushed a commit that referenced this pull request Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-App Bevy apps and plugins C-Bug An unexpected or incorrect behavior 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.

None yet

5 participants