Skip to content

Plugin can add a SubApp to another SubApp (with added one dissappearing) #19447

@ahryshan

Description

@ahryshan

Bevy version

0.16.0

What you did

I've been reading bevy's source code and found this peculiar thing. When SubApp is being passed as App to Plugin::build, a plugin can call any app's function, including App::insert_sub_app. And then I tested that.

What went wrong

When a plugin is added to a SubApp the subapp is being passed to the Plugin::build as an App with SubApp::run_as_app workaroung. And that allows all functions of the App to be called on subapp. The problem with this is when a plugin adds a subapp to a subapp, this new subapp would be dropped in the end of run_as_app without any crash or even notifying the user.

Additional information

It happens because run_as_app is creating a "fake" App and mem::swaps the target subapp with this "fake" app's main subapp. And after provided closure is called with this app, app's main gets swapped back with the subapp.
So basically, if we have this situation:

before building plugin:
App -> SubAppA

after swap:
SubAppA (as App)

after Plugin::build:
SubAppA (as App) -> SubAppB

after swapping again
App -> SubAppA

The newly added subapp (SubAppB) is left behind on the "fake" app and gets dropped along with it when run_as_app returns.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-AppBevy apps and pluginsC-BugAn unexpected or incorrect behaviorS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions