xterm.js icon indicating copy to clipboard operation
xterm.js copied to clipboard

Document-level mouseup/mousemove listeners from _bindMouse survive dispose() during an active drag-select, throwing on later disposed-RenderService access

Open josiahbryan opened this issue 4 months ago • 3 comments

Description

Terminal._bindMouse attaches mousedown-triggered mouseup/mousemove listeners directly to document via bare addEventListener, rather than through xterm's own Disposable lifecycle (e.g. this.register(addDisposableDomListener(...))). If terminal.dispose() is called while a mouse-drag is in progress (before the paired mouseup self-removes the listener), the listener is never removed and lives on document indefinitely, closing over the disposed RenderService.

RenderService.dimensions uses a non-null assertion internally, so any subsequent mousemove/mouseup anywhere on the page (not just in the original terminal) re-invokes the stale handler and throws.

This looks like the same underlying defect class as #5011, at a different call site (_bindMouse's document-level listeners rather than the one already reported there).

Reproduction

  1. Create a Terminal, call .open(), attach the standard mouse-drag-select handlers.
  2. Start a mouse-drag selection inside the terminal (mousedown fires, installing the document-level mouseup/mousemove listeners).
  3. While still mid-drag (before mouseup), call terminal.dispose().
  4. Move the mouse or release the mouse button anywhere on the page.

Expected: no error; disposing a terminal mid-interaction should tear down all listeners it installed.

Actual: an exception is thrown from the stale mousemove/mouseup handler, because it still references the disposed RenderService, whose dimensions getter uses a non-null assertion.

Measured impact (our environment)

  • 0 leaked listeners / 0 exceptions on ordinary navigation away from a terminal view (no drag in progress).
  • +1 leaked listener pair (mouseup + mousemove) per terminal disposed while a drag-select was active, and each subsequent mouse move/up anywhere on the page throws once more per leaked pair until the page is reloaded — confirmed empirically linear with repeated dispose-during-drag cycles.

Suggested fix direction

Route the document-level mousedown-installed mouseup/mousemove listeners through the terminal's own Disposable registration (the same pattern used elsewhere in the class), so dispose() unconditionally tears them down regardless of drag state, instead of relying on the listener's own self-removal on mouseup.

We did not attempt a workaround on our end that injects a synthetic mouseup before dispose, since that would send an unwanted mouse-release escape sequence down any PTY with active mouse-reporting enabled — a tradeoff we didn't want to make in application code for what looks like a lifecycle bug in the library itself.

Happy to provide a minimal standalone repro repo if useful.

josiahbryan avatar Aug 03 '26 00:08 josiahbryan

Checked current master: this specific listener leak is already fixed. _bindMouse's document-level mouseup/mousemove listeners now live in MouseService.ts as MutableDisposables registered through the terminal's own disposable chain (from #6019), so dispose() tears them down regardless of drag state — the code sample in this issue no longer matches what's on master.

The RenderService.dimensions non-null-assertion crash you'd hit downstream of a leaked listener is still real for anything else that can reach it though — opened #6111 to guard it directly (same defect class flagged in #6070's second half).

arsyadal avatar Aug 17 '26 10:08 arsyadal

This looks to already be fixed on master — commit 1701dcef ("use disposables for transient mouse listeners") reworked MouseService.bindMouse to hold the document-level mouseup/mousemove listeners in MutableDisposables that are registered through the register() callback passed in from Terminal/CoreBrowserTerminal (which wires to this._register(...)). So terminal.dispose() now clears those listeners unconditionally via the normal Disposable lifecycle, regardless of whether a drag is still in progress — not relying on the listener's own self-removal on mouseup anymore.

Confirmed by reading current src/browser/services/MouseService.ts: _handleMouseDown sets ctx.mouseupListener.value / ctx.mousedragListener.value, and both are register()-ed MutableDisposables tied to the terminal's disposal.

Might be safe to close, unless there's a remaining gap I'm missing.

arsyadal avatar Aug 17 '26 11:08 arsyadal

Duplicate of #6070

The leaked document-level mouseup/mousemove listeners are fixed on master by #6019 (1701dcef): they're now MutableDisposables registered on the terminal, so dispose() removes them even in the middle of a drag. The fix isn't in a stable release yet; 6.0.0 is still the latest. The remaining RenderService.dimensions throw after dispose is tracked in #6070, with a fix open in #6111.

xtermjs-bot avatar Oct 29 '26 13:10 xtermjs-bot