WebglAddon.dispose() does not release the underlying WebGL2 context
Details
- Browser and browser version: Chromium (reproduced on Electron 30+/Chromium 124+ and stock Chrome); driver-dependent
- OS version: Windows 11 (also seen on macOS)
- xterm.js version: 5.5 / current
master,@xterm/addon-webgl
Steps to reproduce
-
Create a
Terminal,open()it, and load aWebglAddon. -
Dispose the addon (or the terminal), then repeat: create + dispose many WebGL-backed terminals over the app's lifetime (tab/split churn, session restore, etc.).
-
Once the number of contexts created this way passes Chromium's per-document live-context cap (~16, driver/build dependent), the console logs:
WARNING: Too many active WebGL contexts. Oldest context will be lost.and a still-live terminal loses its renderer (
webglcontextlost) and renders blank / as an "X box" / garbled.
A self-contained repro is attached (repro.html): it keeps one live terminal and then creates and disposes 30 more WebGL terminals.
Root cause
WebglRenderer's dispose (registered in the constructor) tears down the render layers and removes the <canvas> from the DOM, but never releases the WebGL2 context:
https://github.com/xtermjs/xterm.js/blob/master/addons/addon-webgl/src/WebglRenderer.ts#L160-L168
this._register(toDisposable(() => {
clearTimeout(this._contextRestorationTimeout);
this._contextRestorationTimeout = undefined;
for (const l of this._renderLayers) {
l.dispose();
}
this._canvas.parentElement?.removeChild(this._canvas);
removeTerminalFromCache(this._terminal);
}));
Detaching the canvas does not free the GPU-side context — it lingers until the canvas is garbage collected, which is non-deterministic and can be arbitrarily delayed. Every disposed addon leaves a "zombie" context that still counts toward Chromium's cap. When the cap is exceeded the browser force-evicts the oldest context, which may belong to a terminal that is still on screen, so a live terminal goes blank rather than the disposed one being cleaned up.
Impact
Any embedder that creates and disposes many terminals in one document hits this — VS Code-style multi-terminal UIs, terminal multiplexers, IDEs with per-tab terminals. The failure is confusing because the terminal that breaks is not the one that was disposed, and it only appears after enough churn to cross the (driver-dependent) cap.
We currently work around it downstream by reaching into private addon internals (addon._renderer._gl) after dispose() and calling WEBGL_lose_context.loseContext() ourselves — which is exactly the kind of thing that should live in the addon.
Suggested fix
Call this._gl.getExtension('WEBGL_lose_context')?.loseContext() as part of the renderer's dispose so the context is released immediately and the live count stays bounded. PR to follow.
Confirming this in the wild, and that the suggested workaround fixes it.
Environment: Obsidian 1.12.3 (Electron/Chromium), @xterm/xterm 6.0.0, @xterm/addon-webgl 0.19.0, Linux/Wayland, AMD RADV.
Setup: a terminal plugin with many terminal tabs, where the WebGL addon is loaded per visible pane and disposed when a pane is hidden. Normal tab switching therefore does one dispose() + one create per switch.
Symptom: exactly as described — the terminal that breaks is not the one that was disposed. After roughly 16 tab switches, a still-visible pane started rendering garbled glyphs (correct cell geometry, wrong glyph contents), with WARNING: Too many active WebGL contexts. Oldest context will be lost. in the console. Before crossing the cap everything renders correctly, which makes it look like a renderer bug rather than a lifecycle one.
Worth underlining for anyone else landing here: this is reachable without any leak in the embedder. Disposing on hide is the correct thing to do, and the total number of simultaneously visible panes never came close to the cap — only the cumulative count of created-and-disposed contexts did. Any UI where panes come and go (tabs, splits, session restore) will hit it eventually.
Workaround applied — the one recommended in the issue body:
addon.dispose();
const gl = (addon as any)._renderer?._gl;
gl?.getExtension('WEBGL_lose_context')?.loseContext();
Result: garbling gone, and the warning no longer appears regardless of how much tab churn I generate. So the diagnosis and fix in #6069 both hold up in a real embedder.
Would be good to see #6069 land — reaching into _renderer._gl from outside the addon is exactly the kind of private-internals dependency that breaks silently on an addon upgrade, and every embedder that hits this has to write the same hack. Happy to test the PR branch against this setup if that's useful.
Follow-up with something that may be blocking this: #6068 is easy to mistake for the shared-atlas family, and the atlas fixes do not resolve it.
Both present identically — correct cell geometry, wrong glyph contents, on a terminal nobody touched:
- atlas family: #6014 / #6038, fixed by #6042 (merged 2026-07-13) and #6055 (merged 2026-07-27)
- this issue: GL context eviction
#6014 and #6018 were both closed on 2026-07-27 pointing at #6055, so "didn't the atlas work already cover this?" is a reasonable assumption to make about #6068. It doesn't.
Direct evidence that they are independent
Running @xterm/addon-webgl 0.19.0 in an Electron embedder with WebGL loaded per visible pane and disposed on hide, I hit both at once and each needed its own separate fix:
- Calls to
clearTextureAtlas()on activation/load garbled still-visible sibling panes (shared atlas,CharAtlasCacheownedBy: Terminal[]). Removing those calls fixed that — and the other garbling remained. - Context eviction after cumulative dispose churn persisted until I added
WEBGL_lose_context.loseContext()afterdispose()as suggested above. That fix does nothing for the atlas symptom, and the atlas fix did nothing for this one.
So this is a genuinely separate root cause, not a stale duplicate.
Triage discriminators
Might save maintainer time on incoming reports, since the visible symptom is nearly identical:
| Signal | Shared atlas (#6014/#6055) | Context eviction (#6068) |
|---|---|---|
| Console | (quiet) | Too many active WebGL contexts. Oldest context will be lost. |
| Correlates with | atlas clear / page merge | cumulative create+dispose count crossing ~16 (not the simultaneously-visible count) |
| A newly opened terminal | also garbled — re-acquires the cached atlas via configEquals (per @heudev) |
renders fine — it gets a fresh context; the oldest live one is the casualty |
| Selecting the text | makes it legible (forces redraw) | no effect |
| Closing all terminals | fixes it (last owner drops, atlas disposed) | fixes it (contexts finally GC'd) |
Rows 1–2 and 5 are what I observed directly on 0.19.0. Row 3's eviction cell and row 4's are what the documented oldest-context-first eviction predicts rather than something I isolated — flagging that rather than dressing it up.
Why stable consumers see both simultaneously
Worth noting for report triage: latest on npm is still 0.19.0, published 2025-12-22, while #6042 and #6055 merged in July 2026 and are only in 0.20.0-beta.*. So anyone on stable has the atlas bugs and this one at the same time, with no way to isolate them by upgrading — which is exactly the condition that produces conflated reports.
Happy to test #6069 against this setup; I can reproduce the eviction on demand and I have a known-good baseline for the atlas half.