Follow-up to the salvaged #111918. That commit fixed the WebGL context
pile-up by removing the WebGL renderer altogether. With @xterm/xterm 6 the
fallback is the DOM renderer, so wide layouts would have lost the crisp,
fast rendering the renderer split in 63975aa deliberately gave them, and
the salvaged comment ("default canvas renderer") described a renderer that
no longer exists.
The leak itself is one missing call: @xterm/addon-webgl 0.19 removes its
canvas on dispose but never calls WEBGL_lose_context.loseContext(), so
every PTY reconnect (which rebuilds the Terminal) leaves a live GL context
until GC. Browsers cap live contexts at ~16 and force-lose the oldest, so a
reconnect storm eventually blanks the terminal the user is looking at.
`loseWebglContexts(host)` runs in the terminal effect's cleanup before
`term.dispose()` and releases every WebGL context under the host; WebGL
stays on for wide layouts exactly as before.
The keepalive is no longer gated on tab visibility / chat activity: an open
PTY socket on a hidden or backgrounded tab still owns its PTY and is the one
most likely to sit idle through a proxy timeout, and the frame is ~20 bytes.
That removes the `shouldSendPtyKeepalive` helper and its two tests; the
socket-open check already lives in `sendTerminalResize`. The ChatPage test
drops its "no WebglAddon constructed" assertion, which passed on base too
(jsdom hosts are 0px wide, so the wide-layout WebGL branch never ran).
28 lines
1.2 KiB
TypeScript
28 lines
1.2 KiB
TypeScript
/**
|
|
* Release the WebGL contexts an xterm WebglAddon left behind.
|
|
*
|
|
* `@xterm/addon-webgl` (0.19) removes its canvas on dispose but never calls
|
|
* `WEBGL_lose_context.loseContext()`, so the GL context stays alive until the
|
|
* browser garbage-collects the canvas. Browsers cap live contexts (~16) and
|
|
* force-lose the oldest when the cap is hit; ChatPage rebuilds the terminal on
|
|
* every PTY reconnect, so a reconnect storm walks straight into that cap and
|
|
* eventually loses the context of the terminal the user is looking at.
|
|
*
|
|
* Call it on the terminal host BEFORE `term.dispose()` removes the canvases.
|
|
* Returns the number of contexts released.
|
|
*/
|
|
export function loseWebglContexts(host: ParentNode): number {
|
|
let released = 0
|
|
for (const canvas of Array.from(host.querySelectorAll('canvas'))) {
|
|
// getContext() with the type a canvas already holds returns that context;
|
|
// for a 2D canvas both WebGL lookups return null without creating one.
|
|
const gl = canvas.getContext('webgl2') ?? canvas.getContext('webgl')
|
|
const lose = gl?.getExtension('WEBGL_lose_context')
|
|
if (lose) {
|
|
lose.loseContext()
|
|
released += 1
|
|
}
|
|
}
|
|
return released
|
|
}
|