Skip to content

Conversation

andriyDev
Copy link
Contributor

Objective

  • Inserting into a dropped AssetId would panic. There is no API to check if the asset ID is still valid, so you can't avoid this case.

For a concrete example, imagine a user uses Assets::reserve_handle to get a handle, and then passes its AssetId into an async task. While the async task is running, the user drops the handle, which removes the entry from Assets. Later when the async task returns, it tries to insert the asset into that AssetId. This previously caused a panic. Based on a true story.

Solution

  • Make Assets::insert return an error when inserting into a dropped AssetId.

Testing

  • Added a test specifically for this case.

@andriyDev andriyDev requested a review from janhohenheim August 6, 2025 06:35
@andriyDev andriyDev added D-Trivial Nice and easy! A great choice to get started with Bevy A-Assets Load files from disk to use for things like images, models, and sounds S-Needs-Review Needs reviewer attention (from anyone!) to move forward C-Bug An unexpected or incorrect behavior labels Aug 6, 2025
@andriyDev andriyDev force-pushed the fix-invalid-handles branch from aa8345e to 891caa0 Compare August 6, 2025 06:37
@janhohenheim janhohenheim added this to the 0.17 milestone Aug 6, 2025
Copy link
Member

@janhohenheim janhohenheim left a comment

Choose a reason for hiding this comment

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

Looks clean, thanks for the fix!
Putting this in the milestone because it fixes Bevy reaching unreachable! code in sensible user code

@andriyDev andriyDev 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 Aug 6, 2025
@janhohenheim janhohenheim added the P-Crash A sudden unexpected crash label Aug 6, 2025
@james7132 james7132 added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Aug 6, 2025
Copy link
Contributor

@greeble-dev greeble-dev left a comment

Choose a reason for hiding this comment

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

Would have been nice to avoid unwraps when the handle is guaranteed to be a UUID. But I can't see a way to do that without adding a AssetUuid type and changing a bunch of APIs, so seems out of scope for this PR.

Co-authored-by: Greeble <166992735+greeble-dev@users.noreply.github.com>
@james7132 james7132 added this pull request to the merge queue Aug 10, 2025
Merged via the queue into bevyengine:main with commit 95f2a82 Aug 10, 2025
32 checks passed
gwafotapa pushed a commit to gwafotapa/bevy that referenced this pull request Aug 12, 2025
…ne#20439)

# Objective

- Inserting into a dropped AssetId would panic. There is no API to check
if the asset ID is still valid, so you can't avoid this case.

For a concrete example, imagine a user uses `Assets::reserve_handle` to
get a handle, and then passes its AssetId into an async task. While the
async task is running, the user drops the handle, which removes the
entry from `Assets`. Later when the async task returns, it tries to
insert the asset into that `AssetId`. This previously caused a panic.
[Based on a true
story](https://discord.com/channels/691052431525675048/749332104487108618/1402511976898498590).

## Solution

- Make `Assets::insert` return an error when inserting into a dropped
`AssetId`.

## Testing

- Added a test specifically for this case.

---------

Co-authored-by: Greeble <166992735+greeble-dev@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior D-Trivial Nice and easy! A great choice to get started with Bevy M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide P-Crash A sudden unexpected crash 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.

4 participants