-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
bug: Fix stackoverflow on asset reload. #21619
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
base: main
Are you sure you want to change the base?
bug: Fix stackoverflow on asset reload. #21619
Conversation
There is a way to create a circular dependency graph with asset loaders. I don't know how I managed to do it. I have tried to create a minimal example, but it has not exhibited the error yet. This commit has two fixes: one at the recursion point, and one at the insertion point. Check for self loops. Warn on self detection and do not loop. Issue warning when an asset wants to mark itself as a dependency and do not allow inserting itself as a dependent.
This seems like a reasonable fix but I'd really like to add a test along with this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also seems sensible to me, but I would also like to block on a test for this.
Understood. Are there any asset-loader tests that I might emulate? |
Pretty much all the tests in bevy/crates/bevy_asset/src/lib.rs Line 1918 in 3dd5d70
|
Thank you for the test reference. They've been very helpful. Coming up with a test has been tricky but instructive. I finally figured out how to provoke the error. If I use an immediate load in an asset loader that tries to load its own asset path, it will cause a stack overflow, which is the behavior I originally saw. A asset loader with a self-path deferred load's does not provoke an error or overflow, but it does behave differently than a normal asset. It will emit asset I'll try to get these tests in with some better fixes tomorrow night. |
Objective
Fix the stackoverflow on asset reload when asset contains its own path as a dependency.
Problem
There is a way to create a circular dependency graph with an asset loader. I don't know how I managed to do it. I have tried to create a minimal example, but it has not exhibited the error yet. But I do have a means of exhibiting the error with my project Nano-9, reproduction details below.
Solution
This commit has two fixes: one at the insertion point (introducing self-reference), and one at the recursion point (following self-reference).
Insertion point
Issue warning when an asset wants to mark itself as a dependency and do not allow inserting itself as a dependent.
Recursion point
Check for self loops. Warn on self detection and do not loop.
It's likely that if you fix it at the insertion point, you don't need to worry about it at the recursion point. You will have stopped the cause of the issue. I left both in for transparency about where the issue lies so far as I could see.
Testing
I can reproduce this error with an example from my Nano-9 project. I wish it were a minimal example. It's not, but I have put it on a branch to isolate this issue. It uses my Bevy fork that is v0.16.1 plus a commit tagged v0.16.1b, which was required for it to build. This PR is a cherry pick of the fix commit against Bevy's main branch.
I can reproduce this error by doing the following:
Here is an excerpt of the crash report on macOS 15.6.1, M4 Max:
Exercising the fix
Alter Nano-9's Cargo file to use the fix branch:
Run the same command again, and you will see a warning:
2025-10-21T04:10:52.348726Z WARN bevy_asset::server::info: Asset 'BirdSprite.png' wants to treat itself as a dependency
Alternative Solution
This commit fixes the immediate stackoverflow issue; however, it would be even better if the user were prevented from making a circular dependency graph in the first place.