Fix a-assets image onload crash from loop-variable closure#5809
Open
vincentfretin wants to merge 1 commit intoaframevr:masterfrom
Open
Fix a-assets image onload crash from loop-variable closure#5809vincentfretin wants to merge 1 commit intoaframevr:masterfrom
vincentfretin wants to merge 1 commit intoaframevr:masterfrom
Conversation
The image onload handler closed over the shared outer loop variables, so by the time it fired imgEls[i] was undefined (or a stale element already removed by fixUpMediaElement), causing "Cannot read properties of undefined (reading 'getAttribute')". Bind the current img to a local inside the Promise executor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TypeError: Cannot read properties of undefined (reading 'getAttribute')ata-assets.js:68when an<img>in<a-assets>finishes loading asynchronously.THREE.Cache.addinto theimgEl.onloadhandler. Before that commit the cache call ran synchronously inside the Promise executor, so the current-iterationimgEl/imgEls[i]were still in scope and there was no crash — but the cache was populated before the image actually finished loading, which is what Set the image cache when img is loaded #5778 correctly set out to fix.onload, the callback closed over the shared outervar i/var imgEl. Becausevaris function-scoped, by the time the handler firedihad already advanced past the end of the images loop (typically tomediaEls.length), soimgEls[i]wasundefinedand.getAttribute('src')threw.imgEls[i]would also be semantically off whenfixUpMediaElementclones the element, sinceimgEls[i]is then the pre-clone element that has been detached from the DOM. That part alone wouldn't crash — detached elements still respond togetAttribute— and the src string happens to match the clone's, so the cache key would still be correct. The actual crash is purely the closure/indexing bug above.imgElto a localvar elinside the Promise executor and use it in both the.completeandonloadbranches.Test plan
caches image loaded asynchronously alongside media elementintests/core/a-assets.test.js. It uses a cache-busted src (soimgEl.completeis false and the onload path is taken) plus a sibling<audio>element, which reliably reproduces the crash before the fix.TypeErrorfrom the bug report, and passes with the fix applied.a-assetstest suite passes (29 tests).