Skip to content

InvalidStateError: Cannot start XR session when another session is already active #220

Description

@XRDeltas

Bug Description

Many WebXR applications do not check whether an XR session is already active before attempting to create a new one. When the user triggers session entry multiple times (e.g., by clicking the "Enter VR" button rapidly), the application attempts to create multiple sessions, causing an InvalidStateError.

This is a defensive programming issue in the applications, not a browser or device bug.

Detected In

  • 29 instances across 28 traces and 17 applications
  • Identified through large-scale WebXR application study (WebXRBench)
  • Affected devices: Quest 3, Pico 4 native browsers

Steps to Reproduce

  1. Open a WebXR application
  2. Click the "Enter VR" button rapidly (multiple times)
  3. Check console for error

Expected Behavior

Application should check session state before creating a new session, and either:

  • Ignore subsequent clicks while a session is active, or
  • Exit the current session before starting a new one

Actual Behavior

InvalidStateError: Cannot start XR session: another session is already active

Environment

  • Applications: 17 (various frameworks)
  • Browsers: Quest 3, Pico 4 native browsers
  • Devices: True devices

Supporting Evidence

  • Pattern ID: D17
  • Divergence type: runtime_error_mismatch
  • App invariant evidence: available

Recommended Fix

Add a session state check before attempting to start a new XR session:

let xrSession = null;

function enterXR() {
  if (xrSession) {
    console.log("XR session already active, ignoring...");
    return; // or optionally exit current session first
  }
  
  navigator.xr.requestSession("immersive-vr").then((session) => {
    xrSession = session;
    
    session.addEventListener("end", () => {
      xrSession = null;
    });
    
    // start rendering...
  }).catch((error) => {
    console.error("Failed to start XR session:", error);
  });
}

Additional Context

This issue was identified through WebXRBench, a large-scale benchmark and testing framework for WebXR applications. The study found 29 instances of this InvalidStateError across 17 different WebXR applications, indicating that many developers are not handling rapid user interactions defensively.

Note: This is an app-level issue. Each affected application should apply the fix independently. A pull request with the fix can be submitted to each application's repository.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions