Skip to content

readline: cork output stream during line refresh - #67962

Open
dhaiirryyaa wants to merge 1 commit into
nodejs:mainfrom
dhaiirryyaa:fix/readline-cork-refresh-line
Open

dhaiirryyaa wants to merge 1 commit into
nodejs:mainfrom
dhaiirryyaa:fix/readline-cork-refresh-line

Conversation

@dhaiirryyaa

Copy link
Copy Markdown

Fixes: #67958

Problem

In Interface.prototype[kRefreshLine]() (lib/internal/readline/interface.js), each redraw issues multiple sequential writes to this.output (moveCursor, cursorTo(output, 0), clearScreenDown(output), this[kWriteToOutput](...), cursorTo(output, cursorPos.cols), moveCursor). On a tty.WriteStream, each write dispatches separately, allowing terminals to render an intermediate frame after moving to column 0 and clearing the line.

Fix

  • Wrap the output sequence in Interface.prototype[kRefreshLine]() with output.cork() / output.uncork() (inside try ... finally) when both methods are functions on this.output.
  • Streams implementing _writev (including tty.WriteStream via net.Socket) flush the entire redraw in a single writev batch without emitting extra non-standard ANSI sequences or altering chunk boundaries for Writable streams without _writev.
  • Added regression tests in test/parallel/test-readline-refresh-cork.js covering batched writev flushing (for readline and REPLServer syntax highlighting), uncork() cleanup if colorize throws, and custom streams lacking cork/uncork.

Verification

  • Tested on a macOS PTY (pty.openpty()) typing new into repl.start({ useColors: true }):
    • Before: 14 separate read() chunks across initial prompt and 3 keystrokes (with �[1G and �[0J arriving separately from content/repositioning).
    • After: 4 atomic read() chunks (1 per redraw).
  • Verified test/parallel/test-readline-refresh-cork.js and existing test/parallel/test-readline-* / test/parallel/test-repl-highlight*.js / test/parallel/test-repl-reverse-search.js tests against Node v26.13.0 with [kRefreshLine] patched.

AI assistance was used while investigating lib/internal/readline/interface.js and drafting the PTY reproduction and regression tests; I reviewed the diff and verified the behavior and tests locally.

Wrap writes in Interface.prototype[kRefreshLine] with output.cork() and
output.uncork() when available so streams with _writev (such as
tty.WriteStream) flush the cursor move, screen clear, redrawn line, and
cursor repositioning in a single writev batch instead of fragmented
writes that can cause terminal flicker.

Refs: nodejs#67958
Signed-off-by: dhaiirryyaa <dhairyakhandelwal880@gmail.com>
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. readline Issues and PRs related to the built-in readline module. labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. readline Issues and PRs related to the built-in readline module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

repl: cursor flickers and jumps to line start during syntax-highlighted redraws

2 participants