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

WriteBuffer.flushSync (called by resize) re-parses already-parsed chunks and stops at an empty-string chunk

Open dansolombrino opened this issue 1 month ago • 1 comments

Details

  • xterm.js version: 6.1.0-beta.304 (@xterm/xterm and @xterm/headless); same code in 6.1.0-beta.315
  • Browser/runtime: Electron 43 (Chromium) and Node (headless)
  • OS: macOS, Linux

CoreTerminal.resize() calls this._writeBuffer.flushSync():

while (chunk = this._writeBuffer.shift()) {
  this._action(chunk);
  const cb = this._callbacks.shift();
  if (cb) cb();
}
this._pendingData = 0;
this._bufferOffset = 0x7FFFFFFF;
this._writeBuffer.length = 0;
this._callbacks.length = 0;

Two problems:

  1. Chunks that were already parsed are parsed again. _innerWrite advances _bufferOffset but leaves parsed chunks in _writeBuffer until the batch completes. When a time slice yields (WRITE_TIMEOUT_MS) with chunks left, and resize() runs before the next slice, flushSync starts at index 0. It re-applies chunks [0, _bufferOffset) and invokes their callbacks a second time. Output on screen is duplicated, and TUIs that redraw relative to the cursor stay corrupted until a full redraw.
  2. An empty-string chunk ends the loop. '' is falsy, so the loop stops at it. The reset that follows then discards every later chunk and its callback. A write('', cb) "parse barrier" followed by more writes and a resize() loses that output, and cb never fires.

Steps to reproduce

  1. const term = new Terminal({ cols: 80, rows: 24 });
    term.write('a\r\n'); term.write('b\r\n'); /* enough data that _innerWrite yields after 'b' */ term.write('c\r\n');
    // after the first slice has run (setTimeout), before the next:
    term.resize(80, 25);
    
    The screen shows a b a b c instead of a b c.
  2. term.write('x'); term.write('', cb1); term.write('y', cb2); term.resize(81, 24); leaves y never rendered, and cb1/cb2 never called.

Suggested fix: iterate from _bufferOffset (for (let i = this._bufferOffset; i < this._writeBuffer.length; i++)), check for undefined instead of falsiness, call the matching callbacks, then reset.

dansolombrino avatar Oct 20 '26 09:10 dansolombrino

#6201 fixed this: flushSync now drains from _bufferOffset, so chunks that were already parsed aren't replayed, and an empty-string chunk no longer ends the flush. It's on master and will ship in the next release. The separate failure when an async parser handler is pending during a resize is tracked in #6154.

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