Repository navigation
Closed
Labels
invalidIssues and PRs that are invalid.
Description
- 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.
Metadata
Metadata
Assignees
Labels
invalidIssues and PRs that are invalid.
Activity
bnoordhuis commentedon May 12, 2018
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.
gajus commentedon May 12, 2018
Is there another event to listen for? Anything to ensure that stderr gets written at the end of stdout.
bnoordhuis commentedon May 12, 2018
There is not.