Skip to content

Remove ugly throttling mechanism when pasting to the terminal #283056

Closed

Description

@Tyriar

Jarred Sumner (@Jarred-Sumner) pointed out in https://x.com/jarredsumner/status/1999317065237512224 this throttling mechanism on paste/sendText (my response at https://x.com/Tyriar/status/1999487637674147925):

input(data: string, isBinary: boolean = false): void {
if (this._store.isDisposed || !this._ptyProcess) {
return;
}
this._writeQueue.push(...chunkInput(data).map(e => {
return { isBinary, data: e };
}));
this._startWrite();
}

private _startWrite(): void {
// Don't write if it's already queued of is there is nothing to write
if (this._writeTimeout !== undefined || this._writeQueue.length === 0) {
return;
}
this._doWrite();
// Don't queue more writes if the queue is empty
if (this._writeQueue.length === 0) {
this._writeTimeout = undefined;
return;
}
// Queue the next write
this._writeTimeout = setTimeout(() => {
this._writeTimeout = undefined;
this._startWrite();
}, Constants.WriteInterval);
}

const enum Constants {
/**
* Writing large amounts of data can be corrupted for some reason, after looking into this is
* appears to be a race condition around writing to the FD which may be based on how powerful
* the hardware is. The workaround for this is to space out when large amounts of data is being
* written to the terminal. See https://github.com/microsoft/vscode/issues/38137
*/
WriteMaxChunkSize = 50,
}
/**
* Splits incoming pty data into chunks to try prevent data corruption that could occur when pasting
* large amounts of data.
*/
export function chunkInput(data: string): string[] {
const chunks: string[] = [];
let nextChunkStartIndex = 0;
for (let i = 0; i < data.length - 1; i++) {
if (
// If the max chunk size is reached
i - nextChunkStartIndex + 1 >= Constants.WriteMaxChunkSize ||
// If the next character is ESC, send the pending data to avoid splitting the escape
// sequence.
data[i + 1] === '\x1b'
) {
chunks.push(data.substring(nextChunkStartIndex, i + 1));
nextChunkStartIndex = i + 1;
// Skip the next character as the chunk would be a single character
i++;
}
}
// Push final chunk
if (nextChunkStartIndex !== data.length) {
chunks.push(data.substring(nextChunkStartIndex));
}
return chunks;
}

This was added as one of the attempts to try fix an issue where sending large amounts of data to the terminal would drop chunks of it #38137

This comment is the most relevant explanation as to why I thought this was happening at the time. I believe I was also never able to reproduce the problem on my machine:

const enum Constants {
/**
* Writing large amounts of data can be corrupted for some reason, after looking into this is
* appears to be a race condition around writing to the FD which may be based on how powerful
* the hardware is. The workaround for this is to space out when large amounts of data is being
* written to the terminal. See https://github.com/microsoft/vscode/issues/38137
*/
WriteMaxChunkSize = 50,
}

The justification for the original fix is that correctness is much more important than performance here as pasting large amounts of data like this is an edge case, at least in a terminal within an editor. It sucks though so we should fix it.

Activity

  1. added
    bug
    important
    terminal-process
    on Dec 12, 2025
  2. ovr commented on Dec 12, 2025

    @ovr

    I didn't review the whole code, but I assume that TTY is running in non blocking mode (O_NONBLOCK) as everything in Node.js, same as STDOUT/STDERR (but libuv handles it internally).

    Whenever FD is running in non-blocking mode, when a write is done, it may return a special status "would block" (EAGAIN/EWOULDBLOCK) and returns the number of the position that the system wrote to. It writes the string partially if it's larger than the OS buffers.

    Something around ~2/4kb, depending on the Unix family os. That's why in the original issue, there is a comment:

    The only pattern I can see is that the text gets cropped after 1018 characters

    That's why the messages were corrupted. (Partially writted)

    Technically, it requires the usage of libc's pool to unblock whenever it is ready instead of using a 5ms pause. So, I've figured this thing, Whenever I was working on a Rust module in Node.js, I had a reference code that probably can help:

    https://github.com/ovr/log_nonblock/blob/master/src/worker.rs#L68

    I hope my idea is correct, and it should help to fix the issue. Thanks!

  3. Tyriar commented on Dec 12, 2025

    @Tyriar
    ContributorAuthor

    Here's likely the file with the problem: https://github.com/microsoft/node-pty/blob/main/src/unix/pty.cc

    Dmitry Patsura (@ovr) that's very similar to what someone from the Windows team reached out to me saying was the likely culprit 👍

  4. pablodz commented on Dec 12, 2025

    @pablodz

    Just to remember that a mistake doesn’t define you, your response does, some people laugh, other keeps building

  5. AlphaFoxz commented on Dec 12, 2025

    @AlphaFoxz

    Daniel Imms (@Tyriar) It doesn't handle the returned boolean value. The code snippet from node-pty:
    Image

  6. API-Beast commented on Dec 12, 2025

    @API-Beast

    If it's truly a workaround for a race condition in foreign code it should be investigated if you can get away with a much lower delay, like 0.01ms or 0.1ms.

  7. 5 remaining items

  8. Tyriar commented on Dec 14, 2025

    @Tyriar
    ContributorAuthor

    Porker Rowen (@l3afyb0y) thanks for the effort, I did a big write up about it in the fix PR: microsoft/node-pty#831. Also thanks for pointing out that issue!

  9. added a commit that references this issue on Dec 15, 2025
    ce39cdb
  10. added
    unreleased
    insiders-released
    and removed
    unreleased
    on Dec 15, 2025
  11. added a commit that references this issue on Dec 19, 2025
  12. added
    feature-request
    and removed
    bug
    on Dec 22, 2025
  13. Tyriar commented on Dec 22, 2025

    @Tyriar
    ContributorAuthor

    Marked feature so it deserves a more thorough test and a call out on release notes.

  14. added
    verification-needed
    on Jan 5, 2026
  15. Tyriar commented on Jan 5, 2026

    @Tyriar
    ContributorAuthor

    To verify:

    1. macOS
    2. Get Copilot to suggest running a large Python script
    3. Run it, it should work fine

    Also

    1. macOS
    2. Copy a large string, performance should be comparable to native terminals
  16. added
    on-release-notes
    on Jan 8, 2026
  17. locked and limited conversation to collaborators on Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-requestimportantinsiders-releasedon-release-notesterminal-processverification-neededverified

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions