Skip to content

a-scene leaves window, document and navigator.xr listeners after it is removed #5867

Backlog

Description

@rkushyk

Description:

Removing an <a-scene> from the DOM leaves nine listeners behind on window, document and navigator.xr. Mount and unmount a scene N times and you end up with N copies of each. Most of the handlers close over the scene element, so the detached scene and its components stay reachable. In a single-page app that mounts a scene per route, this grows with every navigation.

Listeners added and never removed, with line numbers at v1.8.0 (master is the same):

  • src/core/scene/a-scene.js:166, attachedCallbackPostCamera: an anonymous window resize handler that calls self.resize(). disconnectedCallback removes sessionend, but not this one.
  • src/core/scene/a-scene.js:157: an anonymous navigator.xr sessiongranted handler that calls self.enterVR().
  • src/core/scene/a-scene.js:927-930, setupCanvas: fullscreenchange, mozfullscreenchange, webkitfullscreenchange and MSFullscreenChange on document, with onFullScreenChange. That function is declared inside setupCanvas and closes over sceneEl, so nothing outside can remove it.
  • src/components/scene/screenshot.js:108: play() adds a window keydown listener. The component has no pause() or remove().
  • src/components/scene/xr-mode-ui.js:48 and :56: init() adds an anonymous window message handler and a window orientationchange handler. remove() (line 127) leaves both.

The same lines are in 1.5.0, 1.6.0, 1.7.0 and 1.7.1. #3463 and #4899 describe memory that a removed scene does not release; this issue lists the listeners behind part of it.

To reproduce:

  1. Open a page that creates <a-scene> from script and removes it again, a few times over, for example document.body.appendChild(scene), wait for loaded, then scene.remove().
  2. In the Chrome DevTools console, run getEventListeners(window), getEventListeners(document) and getEventListeners(navigator.xr) after each removal.
  3. resize, keydown, message, orientationchange, sessiongranted and each of the four fullscreen events gain one entry per mount.

We measured this in a React app (aframe 1.5.0, Chrome) over three mount and unmount cycles: each listener above went 1, 2, 3.

Possible fix:

Keep a reference to each handler and remove it on teardown:

  • In a-scene.js, store the resize handler, the sessiongranted handler and onFullScreenChange on the scene (for example this.onWindowResize, set in setupCanvas as sceneEl.onFullScreenChange). Then remove them in disconnectedCallback next to the existing sessionend removal.

  • In screenshot.js, add pause() (or remove()) that removes this.onKeyDown.

  • In xr-mode-ui.js, store the message handler as a bound method and remove it and toggleOrientationModalIfNeeded when the scene is detached. update() also calls remove() when enabled is false, so the removal must not run there while the scene stays attached.

  • A-Frame Version: 1.5.0 (lines checked at 1.8.0 and master)

  • Platform / Device: Chrome, desktop

Activity

  1. vincentfretin commented on Nov 29, 2026

    @vincentfretin
    Contributor

    All this are indeed valid, can your agent do a PR that fixes this, with a unit test that assert the listeners are all removed? For screenshot, implement the removeEventListener in pause() only.
    Be aware of the pending #5868 it's funny it didn't report it, probably because window.addEventListener('load', undefined); doesn't register anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions