Plugin can add a SubApp to another SubApp (with added one dissappearing)
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## 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::swap`s 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.
Contributor guide
Research direction
Start by reading SubApp::run_as_app and the App::insert_sub_app path involved when Plugin::build receives a SubApp as an App. Reproduce the nested-subapp case and trace the swap boundaries; done means the behavior is defined and a newly added SubApp is no longer silently dropped.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- game-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100