Skip to content

fix: handle stale XRSpace during VR session re-entry#495

Open
sawa-zen wants to merge 1 commit intopmndrs:mainfrom
sawa-zen:fix/vr-reentry-getpose-session-mismatch
Open

fix: handle stale XRSpace during VR session re-entry#495
sawa-zen wants to merge 1 commit intopmndrs:mainfrom
sawa-zen:fix/vr-reentry-getpose-session-mismatch

Conversation

@sawa-zen
Copy link
Copy Markdown
Contributor

Problem

Fixes #473

Re-entering a VR session (exit VR → enter VR again) causes an uncaught InvalidStateError:

DOMException: Failed to execute 'getPose' on 'XRFrame': XRSpace and XRFrame sessions do not match.

This happens because XRSpace objects from the previous session (attached to Object3D.xrSpace) remain referenced during the first few frames of the new session. When createGetXRSpaceMatrix calls frame.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 in createGetXRSpaceMatrix (space.ts) with a try-catch. When the error is caught, the function returns false (same as when pose == null), allowing the frame loop to continue. The stale XRSpace references 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 XRSpace references on Object3D nodes when a session ends, but this minimal change is the least invasive way to prevent the crash.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"XRSpace and XRFrame sessions do not match" exception thrown when a session ends and then restarts

1 participant