xterm.js
xterm.js copied to clipboard
WriteBuffer.flushSync (called by resize) re-parses already-parsed chunks and stops at an empty-string chunk
Details
- xterm.js version: 6.1.0-beta.304 (
@xterm/xtermand@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:
- Chunks that were already parsed are parsed again.
_innerWriteadvances_bufferOffsetbut leaves parsed chunks in_writeBufferuntil the batch completes. When a time slice yields (WRITE_TIMEOUT_MS) with chunks left, andresize()runs before the next slice,flushSyncstarts 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. - 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. Awrite('', cb)"parse barrier" followed by more writes and aresize()loses that output, andcbnever fires.
Steps to reproduce
-
The screen showsconst 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);a b a b cinstead ofa b c. term.write('x'); term.write('', cb1); term.write('y', cb2); term.resize(81, 24);leavesynever rendered, andcb1/cb2never 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.
#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.