#12237·pixijs

Destroying AccessibilitySystem before attaching its canvas leaves a live MutationObserver that crashes later

Author: mf3493297-pngCreated Sep 27, 2026Updated Sep 27, 2026

Current behavior

When accessibility is activated while the renderer canvas is detached, AccessibilitySystem installs a MutationObserver that waits for the canvas to enter the DOM.

If the system is destroyed before that happens, the observer remains active. Appending the retained canvas afterward executes the stale callback and throws:

TypeError: can't access property "ensureAttached", this._canvasObserver is null

The failure reproduces on the current dev branch at commit 27071aa0096d8749bfdb189857c58178c30cd153 in a headed Firefox runtime.

Expected behavior

Destroying the accessibility system should disconnect every lifecycle-owned observer. Appending a previously detached canvas after destruction should not execute callbacks against nulled system state.

Reproduction

  1. Create a renderer whose canvas has no parent node.
  2. Initialize AccessibilitySystem with enabledByDefault: true.
  3. Confirm the system is active.
  4. Destroy the system before inserting the canvas into the DOM.
  5. Append the retained canvas to document.body.
  6. Observe the asynchronous uncaught TypeError from the stale observer callback.

Observed result in headed Firefox:

json
{
  "errors": [
    "TypeError: can't access property \"ensureAttached\", this._canvasObserver is null"
  ],
  "reproduced": true
}

Controls:

  • Activating the system and attaching the live canvas without destroying it produces no error.
  • Starting with an attached canvas, then destroying the system, produces no error.

Root cause

Affected code: src/accessibility/AccessibilitySystem.ts.

_activate() creates a function-local MutationObserver when canvas.parentNode is absent:

typescript
const observer = new MutationObserver(() =>
{
    if (canvas.parentNode)
    {
        observer.disconnect();
        this._canvasObserver.ensureAttached();
        this._initAccessibilitySetup();
    }
});

The observer is not retained on the system instance. Consequently, destroy() cannot disconnect it before clearing:

typescript
this._canvasObserver = null;
this._renderer = null;

A later DOM insertion satisfies the callback condition and dereferences cleared lifecycle state.

The stale callback also retains the destroyed system and canvas until it fires, or indefinitely if the canvas is never attached.

Environment

  • PixiJS branch: dev
  • Commit: 27071aa0096d8749bfdb189857c58178c30cd153
  • Runtime: headed Firefox via Camoufox
  • Accessibility: explicitly enabled; this is an opt-in subsystem
  • Node.js: v22.22.2

Duplicate review

I searched open and closed issues and pull requests for:

  • AccessibilitySystem + destroy
  • accessibility + detached canvas
  • MutationObserver + accessibility
  • ensureAttached + crash

PR #11700 fixes bound event-listener cleanup. PR #12220 fixes fresh-page Tab activation. Neither retains or disconnects the detached-canvas MutationObserver.

Suggested fix direction

Store the pending DOM-attachment observer on the AccessibilitySystem instance, disconnect and clear it during destroy(), and clear it after its callback runs. A destroyed-state guard in the callback can provide defense in depth, but lifecycle ownership should be resolved by disconnecting the observer.

Please consider assigning a bounty if this issue meets the program's impact criteria. I can provide a focused regression test and patch after triage.

Public runnable reproduction

Run npm ci && npm start, then open http://127.0.0.1:4173/accessibility/ in Firefox. The page prints REPRODUCED: when the bug occurs. The repository includes the exact binary fixtures where applicable and was verified in headed Camoufox.