Memory leak: every closed tab leaks its whole terminal (xterm WebGL cursor-blink timer never disposed) + unterminated OSC freezes the app — root causes, measurements & fixes
Version: 1.0.236 Platform: macOS arm64 26.5.1 Plugins: none Frontend: xterm-webgl
Describe the problem:
The longer Tabby is open, the more memory it uses and the slower it gets, until the whole app can become unresponsive. Two bugs cause this, and both are still on master:
- Every closed terminal tab leaks its whole terminal. The xterm.js WebGL addon never disposes its cursor-blink timer, so a closed tab's terminal (full scrollback), WebGL renderer,
Sessionand PTY proxy stay in memory. Measured: about 42 MB of renderer RSS per closed tab (20,000 lines of scrollback). - A single unterminated
ESC ]freezes the whole app.OSCProcessorholds all later output of that session and re-copies the growing backlog on every chunk. Measured: main-thread stalls went from 0 to 2.4 s within 40 s, at under 1 MB/s of plain output.
Fixes are in #11719, #11720, #11721 and #11722; details below.
To Reproduce: Leak (defaults: WebGL frontend, cursor blink on):
- Open DevTools → Console.
- Open ~10 tabs, run
seq 1 20000in each, then close them withexit. - Memory tab → collect garbage, then run
queryObjects(WebGL2RenderingContext). The count grows by one per closed tab and never goes down, and the renderer's RSS grows by tens of MB per closed tab.
Freeze:
- Run the script below in a tab. It prints one
ESC ]and then plain lines with no BEL orESC \. - The tab shows no further output, and the whole app becomes increasingly unresponsive (multi-second stalls within a minute).
import sys, time
out = sys.stdout.buffer
out.write(b'\x1b]'); out.flush()
line = b'plain log line with no bell and no string terminator\n' * 130
t0 = time.time()
while time.time() - t0 < 45:
out.write(line); out.flush(); time.sleep(0.01)Additional context
1. Closed tabs are never freed: CursorBlinkStateManager in @xterm/addon-webgl is never disposed
Root cause
In @xterm/addon-webgl ≤ 0.19.0, WebglRenderer creates _cursorBlinkStateManager as a MutableDisposable but, unlike its sibling fields, never registers it:
// @xterm/addon-webgl 0.17.0, lib/addon-webgl.js (minified)
this._cursorBlinkStateManager=new c.MutableDisposable, // <- not registered
this._charAtlasDisposable=this.register(new c.MutableDisposable),When Tabby disposes the frontend (XTermFrontend.destroy() → webGLAddon.dispose() / xterm.dispose()), the renderer is marked disposed, but the blink manager's 600 ms setInterval keeps running. Its callback closes over the renderer, and from there it holds the whole terminal and, through xterm's event emitters, Tabby's frontend, tab, Session and ElectronPTYProxy. This happens whenever the tab has focus when it's closed, which is almost always.
This was fixed upstream in xterm.js by xtermjs/xterm.js#5817 (issue xtermjs/xterm.js#5818), but only on the 0.20 / xterm 6 line. The open Dependabot bump #11454 (0.17.0 → 0.19.0) does not fix it, because 0.19.0 still has the unregistered field.
Evidence
A heap snapshot after closing 20 tabs and forcing GC shows every retained WebGL2RenderingContext with the same retainer path:
(Global handles) → global.clearInterval → zone.js patchDelegate → tasksByHandleId
→ ZoneTask.callback → CursorBlinkStateManager._renderCallback → WebglRenderer._gl- All 20 leaked
WebglRenderers have_isDisposed = true, and all 20XTermWebGLFrontends havedisposed = true, so Tabby's own cleanup did run. - Each leaked renderer's
CursorBlinkStateManager._blinkIntervalis still set. Session,ElectronPTYProxy,SessionMiddlewareStack,OSCProcessor,BufferService: 21 instances each (20 closed + 1 live).
A/B test in two identical clean profiles (no third-party plugins): open a tab, run seq 1 20000, exit, repeated 20 times, then force GC.
| after N closed tabs | stock 1.0.236: renderer RSS / WebGL2 contexts / canvases | with fix: RSS / WebGL2 / canvases |
|---|---|---|
| 0 | 265 MB / 1 / 5 | 267 MB / 1 / 5 |
| 5 | 482 MB / 7 / 24 | 382 MB / 2 / 9 |
| 10 | 687 MB / 12 / 39 | 389 MB / 2 / 9 |
| 20 | 1105 MB / 22 / 69 | 390 MB / 2 / 9 |
JS heap: 49 → 136 MB (stock) vs 50 → 53 MB (fixed). Most of the leaked memory is the scrollback ArrayBuffers, which sit outside the JS heap. That's why the process's RSS grows much faster than the DevTools heap numbers suggest.
Simple repro
- Open DevTools (Ctrl/Cmd+Shift+I) → Console.
- Open ~10 tabs, run
seq 1 20000in each, then close them withexit. - Memory tab → collect garbage, then in the console run
queryObjects(WebGL2RenderingContext). The count grows by one per closed tab and never goes down.
Fix
#11720 disposes the blink manager explicitly in XTermFrontend, both on destroy and on WebGL context loss, until Tabby uses addon-webgl ≥ 0.20. A patch-package patch with upstream's one-token change (this._cursorBlinkStateManager=this.register(new c.MutableDisposable) in lib/addon-webgl.js) would also work, but because that file is a single minified line the patch came out at 200 KB and was unreviewable.
Upgrading to the xterm 6 line would also fix it. Note that xtermjs/xterm.js#5817 fixes a second unregistered disposable, RenderService._pausedResizeTask, which is also present in Tabby's bundle. I did not see it contribute in my test and haven't patched it.
Workaround until a fix ships (verified): turn off Settings → Terminal → Cursor blink, which means no blink manager is created. On stock 1.0.236 with cursorBlink: false, the same churn test stays flat: 265 → 379 MB and 2 WebGL contexts after 10 closed tabs, the same as the patched build. Restart Tabby to release anything that already leaked.
2. An unterminated ESC ] makes OSCProcessor swallow the stream, with O(n²) cost
Root cause
tabby-terminal/src/middleware/oscProcessing.ts:
if (!foundSuffix) {
// No suffix found - buffer the rest and wait for next chunk
this.buffer = data.subarray(prefixIndex)
break
}Only BEL and ESC \ are accepted as terminators, and the buffer has no limit. After a stray or aborted ESC ]:
- all later output of that session is held and never reaches xterm;
- every new chunk does
Buffer.concat([this.buffer, data])plus threeindexOfscans over the whole backlog.
The cost grows quadratically. Because every tab shares one renderer thread, the whole app becomes unresponsive, not just that tab. xterm's own parser doesn't behave like this: it aborts an OSC on CAN/SUB or on an ESC that isn't followed by \.
Evidence
One tab, printf '\e]', then plain text lines (no BEL, no ESC) at a target of ~1 MB/s for 45 s:
| main-thread latency (CDP eval round-trip) | renderer RSS | output that got through | |
|---|---|---|---|
| stock | 0 → 226 → 1082 → 1798 → 2390 ms | 615 → 1284 MB | 18 MB (producer throttled by the stalls) |
| with fix | 0–4 ms throughout | ~405 MB, flat | 34 MB |
Repro script:
import sys, time
out = sys.stdout.buffer
out.write(b'\x1b]'); out.flush()
line = b'plain log line with no bell and no string terminator\n' * 130
t0 = time.time()
while time.time() - t0 < 45:
out.write(line); out.flush(); time.sleep(0.01)Fix (verified)
Mirror xterm's abort rules and cap the hold. A split, legitimate OSC (e.g. ESC ]0;SPLIT … 1 s later … TITLE_OK BEL) still works; I checked this.
if (!foundSuffix) {
// Only keep waiting while this can still be a live OSC: xterm aborts an OSC on
// CAN/SUB or on ESC not followed by '\' (an ESC\ here would already have matched
// as the suffix). Also cap the hold so a stray "ESC ]" can't swallow the stream.
const pending = data.subarray(prefixIndex)
const esc = pending.indexOf(0x1b, OSCPrefix.length)
const aborted = pending.length > 1024 * 1024
|| esc !== -1 && esc + 1 < pending.length
|| pending.indexOf(0x18, OSCPrefix.length) !== -1
|| pending.indexOf(0x1a, OSCPrefix.length) !== -1
if (aborted) {
processedData.push(pending) // pass through untouched; xterm handles it
break
}
this.buffer = pending
break
}(In my build I also pass the session logger into OSCProcessor and log a one-time warning when this triggers, so it shows up in log.txt.)
Related open issues
Probably the same root cause as #1 (memory grows with tabs and isn't freed on close):
- #8851 Memory problems with tabs: "If you open and close tabs it doesn't release RAM" (xterm-webgl)
- #4244 memory leak: "Memory grows when TAB is added, but memory is not freed when TAB is closed"
- #11199 High CPU Tabby Helper Renderer after closing Tabby (~10 sessions, "new sessions open and closed regularly")
- #5464 High Memory Usage on M1 Mac (grows to ~2 GB after a few hours)
- #7892 Memory Leek on M1 MacBook Pro (5–8 GB)
- #10383 Tabby Helper (Renderer) occupy large memory
- #10777 Insane memory usage (27 GB, "even after I close all tabs"; the reporter uses SSH port forwarding, see also #11639)
- #4072, #7151, #7324, #4714 (general high renderer memory)
Possibly related to #2 or to the growing memory (progressive sluggishness until the app is unresponsive):
- #11552 macOS: multiple long-running TUI sessions eventually make xterm-WebGL nearly unresponsive
- #10747 UI freezes with high CPU and continuously increasing memory
- #9415 Freeze when too many outputs come up fast
- #8974 The software often freezes
Context: #11454 (Dependabot addon-webgl → 0.19.0, which doesn't contain the fix), #11667 (xterm 5.4.0 pin).
Probably not related (different subsystems): the SFTP leaks #11572 / #10716, the ZMODEM OOMs #11590 / #11269, and the SSH port-forwarding leak #11639.
Minor, noticed along the way (not measured, happy to file separately):
BaseSessionalso accumulatesinitialDataBufferwithBuffer.concatper chunk until the frontend attaches, and the frontend only attaches when the tab is first focused. A tab that's never focused and produces output (e.g. restored background tabs) grows without limit, with the same O(n²) pattern.- Tabby's renderer exports its plugin search paths via
process.env.NODE_PATH, and every spawned shell inherits it.
Source: Eugeny/tabby