Skip to content

writable.write callback is called before the buffer is flushed #20687

Closed

Description

@gajus
  • Version:

v10.1.0

  • Platform:

Darwin curiosity.local 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64

Here is the code to replicate the issue:

let lastException;
let stdoutActiveWriteCount = 0;
let stderrActiveWriteCount = 0;

process.on('uncaughtException', (exception) => {
  if (stdoutActiveWriteCount || stderrActiveWriteCount) {
    lastException = exception;
  } else {
    throw exception;
  }
});

const writeStdout = process.stdout.write.bind(process.stdout);
const writeStderr = process.stderr.write.bind(process.stderr);

process.stdout.write = (chunk, encoding, callback) => {
  stdoutActiveWriteCount++;

  process.stdout.once('drain', () => {
    process.stderr.write('END0');
  });

  return writeStdout(chunk, encoding, () => {
    stdoutActiveWriteCount--;

    if (!stdoutActiveWriteCount && lastException) {
      process.stderr.write('END1');

      return;
    }

    if (callback) {
      callback();
    }
  });
};

process.stderr.write = (chunk, encoding, callback) => {
  stderrActiveWriteCount++;

  return writeStderr(chunk, encoding, () => {
    stderrActiveWriteCount--;

    if (!stderrActiveWriteCount && lastException) {
      process.exit();
    }

    if (callback) {
      callback();
    }
  });
};

let index = 0;
let output = '';

while (index < 50000) {
  output += ' ' + (index++);
}

process.stdout.write(output);

throw new Error('test');

Executing this code in the CLI provides expected result:

$ node --eval "/* eslint-disable */ let lastException; let stdoutActiveWriteCount = 0; let stderrActiveWriteCount = 0; process.on('uncaughtException', (exception) => { if (stdoutActiveWriteCount || stderrActiveWriteCount) { lastException = exception; } else { throw exception; } }); const writeStdout = process.stdout.write.bind(process.stdout); const writeStderr = process.stderr.write.bind(process.stderr); process.stdout.write = (chunk, encoding, callback) => { stdoutActiveWriteCount++; process.stdout.once('drain', () => { process.stderr.write('END0'); }); return writeStdout(chunk, encoding, () => { stdoutActiveWriteCount--; if (\!stdoutActiveWriteCount && lastException) { process.stderr.write('END1'); return; } if (callback) { callback(); } }); }; process.stderr.write = (chunk, encoding, callback) => { stderrActiveWriteCount++; return writeStderr(chunk, encoding, () => { stderrActiveWriteCount--; if (\!stderrActiveWriteCount && lastException) { process.exit(); } if (callback) { callback(); } }); }; let index = 0; let output = ''; while (index < 50000) { output += ' ' + (index++); } process.stdout.write(output); throw new Error('test');"
[..] 49996 49997 49998 49999END0END1

Notice that the output ends with END0END1.

However, if you attempt to pipe the output, END0 and END1 appear middle of the resulting string, e.g.

$ node --eval "/* eslint-disable */ let lastException; let stdoutActiveWriteCount = 0; let stderrActiveWriteCount = 0; process.on('uncaughtException', (exception) => { if (stdoutActiveWriteCount || stderrActiveWriteCount) { lastException = exception; } else { throw exception; } }); const writeStdout = process.stdout.write.bind(process.stdout); const writeStderr = process.stderr.write.bind(process.stderr); process.stdout.write = (chunk, encoding, callback) => { stdoutActiveWriteCount++; process.stdout.once('drain', () => { process.stderr.write('END0'); }); return writeStdout(chunk, encoding, () => { stdoutActiveWriteCount--; if (\!stdoutActiveWriteCount && lastException) { process.stderr.write('END1'); return; } if (callback) { callback(); } }); }; process.stderr.write = (chunk, encoding, callback) => { stderrActiveWriteCount++; return writeStderr(chunk, encoding, () => { stderrActiveWriteCount--; if (\!stderrActiveWriteCount && lastException) { process.exit(); } if (callback) { callback(); } }); }; let index = 0; let output = ''; while (index < 50000) { output += ' ' + (index++); } process.stdout.write(output); throw new Error('test');" | cat
[..] 43150 43151 4END13152 43153 43154 [..] 39053 39054 39055 39056END0 39057 39058 39059 [..] 49996 49997 49998 49999

END1 appears considerably earlier than END0 too.

The expected output is the same as it is without piping the output.

Activity

  1. bnoordhuis commented on May 12, 2018

    @bnoordhuis
    Member

    The expected output is the same as it is without piping the output.

    That's a wrong expectation. 'drain' and write callbacks are invoked when the data is handed off to the operating system, they don't say anything about whether the other end received the data.

    I'll close this out, not a bug.

  2. added
    invalid
    on May 12, 2018
  3. gajus commented on May 12, 2018

    @gajus
    Author

    That's a wrong expectation. 'drain' and write callbacks are invoked when the data is handed off to the operating system, they don't say anything about whether the other end received the data.

    Is there another event to listen for? Anything to ensure that stderr gets written at the end of stdout.

  4. bnoordhuis commented on May 12, 2018

    @bnoordhuis
    Member

    There is not.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions