RFC: Release WebGL contexts when a Deck is finalized
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
- Domain
- frontend, performance
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
headlessproject runs every file in one page. On master, 14 contexts are left open after their files finish, just under the limit, which is whytest-coverageandtest-webgpusometimes 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/coreafterfinalize(). That applies A's rule (one task later, out of the document, no other Deck on it) to that canvas only. - Both React
DeckGLcomponents 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
headlessproject 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
_releaseCanvasContextbecomes 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
- luma.gl: should
loseDevice()detach the device from the context, so a restored context gets a fresh device? That would make A safe. - Should
_releaseCanvasContextbecome public for other wrappers (Vue, web components), or stay internal until (1)?
Implementation
- #11221: option B in core and
@deck.gl/react. - #11183 uses
_releaseCanvasContextin 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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from visgl/deck.gl
-
[Bug] Tile3DLayer selects no tiles until the camera moves when tileset.json loads right after a layer updatePossibly taken @lee3Q claimed this 3 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
[Bug] SplitterWidget calls onDragStart on drag end; Deck._getViews writes into the app's views arrayPossibly taken @04jennet-coder claimed this 20 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
Maintainers usually reply within 1 day
-
[Bug] WMSLayer positions EPSG:4326 images as CARTESIANPossibly taken @athomct claimed this 3 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
[Bug] MapLibreOverlay under React StrictMode: 'WebGL context already attached to device' because the attach path omits _reuseDevicesPossibly taken @jsve claimed this 84 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
visgl/deck.gl#10681 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
Similar issues
-
accessibility area:ci ci-test level:low priority:high type:test
Difficulty 2/5 1-3 hours Newbie friendliness 71/100
skytuhua-org/Gen-V1#3132 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
drmrd/vernacular#1238 ·
Maintainers usually reply within 1 day
-
question
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
enhancement migration-prioritized
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Hudson-Pufferfish/local-party-console#136 ·
Maintainers usually reply within 1 day
-
priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day