RFC: Release WebGL contexts when a Deck is finalized

Open
#11,220 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
15/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Read the issue’s implementation references #11221 and #11183, then inspect Deck.finalize() in core and the React DeckGL unmount paths. Compare the proposed option B behavior against the listed canvas reuse scenarios and the headless project results. Done means contexts created by Deck are released, React owned canvases opt in, passed devices remain untouched, and the referenced headless coverage passes without test setup patches.

Written by the indexing model from the issue text.

Description

Problem

Deck.finalize() doesn't free the WebGL context of the device Deck created. Deck creates it with _reuseDevices: true, so WebGLDevice.destroy() does nothing, and even without that flag luma.gl leaves the context to garbage collection, since WebGL has no API to destroy one. Browsers cap the number of live contexts per page (about 16 in Chromium) and drop the oldest one when a new one goes over.

  • In apps, every finalized Deck keeps its context until it is collected. A page that mounts and unmounts <DeckGL> often (route changes, dashboards, StrictMode remounts) can push a live map's context out. This follows from the code path; I haven't reproduced it in an app.
  • In our tests, the headless project runs every file in one page. On master, 14 contexts are left open after their files finish, just under the limit, which is why test-coverage and test-webgpu sometimes fail with a "WebGL context is lost" cascade. The React renderer's browser specs (#11183) push it over.
Leaked on master Contexts Canvas
mapbox/mapbox-overlay.spec 4 created by Deck
core/lib/pick-layers.spec 2 created by Deck
react/deckgl.spec 6 rendered by the React component
react/widgets.spec 1 rendered by the React component
core/lib/deck.spec 1 passed by the test

A test setup file that released each file's contexts (#11196) worked, but it patched getContext for every browser test and hid leaks rather than fixing them, so it was closed in favor of a fix in deck.gl.

Options considered

Both use luma.gl's public device.loseDevice(), and neither ever touches a device or gl the app passed.

A. Deck releases any context it created, once its canvas leaves the document

After finalize(), Deck waits one task. If no other Deck has started on the canvas and the canvas is no longer in the document, it loses the device. This covers vanilla and React apps with one rule in core, and no wrapper changes.

The catch: a canvas only ever has one WebGL context. If an app finalizes a Deck, leaves its canvas out of the document, and later starts a new Deck on the same canvas element, the new Deck gets the lost context, and luma.gl's device reuse returns the old lost device. It renders nothing, silently.

A variant fixes that, and I verified the browser side: if the webglcontextlost event is preventDefault()-ed, the context can be restored with restoreContext(), and a lost-but-restorable context no longer counts against the limit (40 of them didn't push out an older live context; 40 live ones did). But a new device on the restored context needs luma.gl to forget the old one, which today means clearing its internal gl.luma record from deck.gl. That belongs in luma.gl.

B. Deck releases contexts on canvases it created; components that render their own canvas opt in (proposed)
  • finalize() releases the device when Deck created the canvas too. Deck removes that canvas, so nothing can render to it again.
  • For a canvas the app passed, finalize() does nothing new. A component that renders its own canvas and knows it won't reuse it calls _releaseCanvasContext(deck) from @deck.gl/core after finalize(). That applies A's rule (one task later, out of the document, no other Deck on it) to that canvas only.
  • Both React DeckGL components call it on unmount. React never reuses an unmounted canvas, and StrictMode and <Activity> keep the canvas in the document.

Real-world scenarios

Scenario A B
Hiding the map: display: none, visibility: hidden, React <Activity mode="hidden"> kept (canvas stays in the document) kept
React StrictMode remount kept (canvas stays, new Deck reuses it) kept
React <DeckGL> unmounts released released
Moving the element in one step (appendChild to a new parent) kept (reconnected before the check) kept
A web component that creates its <canvas> once, starts Deck in connectedCallback and finalizes it in disconnectedCallback, then is re-added later: tab UIs that keep panel elements, docking layouts that detach hidden panels (dockview's "only when visible"), a Vue <KeepAlive> wrapper that finalizes on deactivate second Deck renders nothing unaffected: leaks until GC, as today
A vanilla app that passes its own canvas and finalizes released if the canvas left the document not released, as today
deck.gl's integrations: Mapbox, MapLibre and Google Maps overlays let each Deck create its own canvas; ArcGIS passes gl released / untouched released / untouched

The web-component row is the deciding one. Setting up in connectedCallback and cleaning up in disconnectedCallback is the standard custom-element pattern, and both callbacks can fire many times on one element.

Why B

  • No behavior change for any app. Core only releases canvases it created and removed; the React components opt in for canvases they know are never reused.
  • Same result in tests. With no test patches, #11183's full headless project passes with no lost contexts under both: 310/310 files with B, 309/309 with A.
  • A's failure is silent and lands on a legitimate pattern.
  • A stays possible later. If luma.gl can create a fresh device on a restored context, core could release passed canvases too and restore them on reuse, and _releaseCanvasContext becomes unnecessary.

What B leaves: a vanilla app that passes its own canvas still keeps the context until GC, as today, and core/lib/deck.spec keeps its one leaked context.

Open questions

  1. luma.gl: should loseDevice() detach the device from the context, so a restored context gets a fresh device? That would make A safe.
  2. Should _releaseCanvasContext become public for other wrappers (Vue, web components), or stay internal until (1)?

Implementation

  • #11221: option B in core and @deck.gl/react.
  • #11183 uses _releaseCanvasContext in the React renderer and drops the test setup patch.

Generated by Claude Code

Dominant language
TypeScript
Stars
14.8k
Forks
2.3k
Avg merge
1d 9h
Merged PRs (30d)
230

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from visgl/deck.gl

All issues in visgl/deck.gl

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.