fix: handle stale XRSpace during VR session re-entry#495
Open
sawa-zen wants to merge 1 commit intopmndrs:mainfrom
Open
fix: handle stale XRSpace during VR session re-entry#495sawa-zen wants to merge 1 commit intopmndrs:mainfrom
sawa-zen wants to merge 1 commit intopmndrs:mainfrom
Conversation
Wrap `frame.getPose()` in `createGetXRSpaceMatrix` with try-catch to handle `InvalidStateError` when XRSpace from a previous session is still referenced during the first few frames after re-entering VR. This fixes pmndrs#473 where re-entering a VR session causes an uncaught `DOMException: XRSpace and XRFrame sessions do not match` on Pico 4, Pico Ultra, and Android smartphones. Quest devices are unaffected as they don't throw on session mismatch.
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.
Problem
Fixes #473
Re-entering a VR session (exit VR → enter VR again) causes an uncaught
InvalidStateError:This happens because
XRSpaceobjects from the previous session (attached toObject3D.xrSpace) remain referenced during the first few frames of the new session. WhencreateGetXRSpaceMatrixcallsframe.getPose(space, referenceSpace)with these stale spaces, the browser throws.Affected devices: Pico 4, Pico Ultra, Android smartphones (Chrome)
Unaffected: Quest 3 (does not throw on session mismatch)
Fix
Wrap the
frame.getPose()call increateGetXRSpaceMatrix(space.ts) with a try-catch. When the error is caught, the function returnsfalse(same as whenpose == null), allowing the frame loop to continue. The staleXRSpacereferences are naturally replaced within a few frames as the new session's input sources are registered.Note
A more thorough fix could proactively invalidate/clean up
XRSpacereferences onObject3Dnodes when a session ends, but this minimal change is the least invasive way to prevent the crash.