Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
net: make stdout & stderr block on all platforms
Previously, if a write to stdout (or stderr) was long enough that it
would have to be broken into chunks for writing, and the process was
terminated before all of those chunks were written, they would be lost.

This prevents chunks from being lost or delayed by making writes to
std{out|err} synchronous (and thus blocking).

Fixes: #784
  • Loading branch information
Fishrock123 committed Jul 6, 2015
commit a99699665323061bfdd835842d3d202e502e48d5
5 changes: 1 addition & 4 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ function Socket(options) {
} else if (options.fd !== undefined) {
this._handle = createHandle(options.fd);
this._handle.open(options.fd);
if ((options.fd == 1 || options.fd == 2) &&
(this._handle instanceof Pipe) &&
process.platform === 'win32') {
// Make stdout and stderr blocking on Windows
if ((options.fd == 1 || options.fd == 2) && this._handle instanceof Pipe) {
var err = this._handle.setBlocking(true);
if (err)
throw errnoException(err, 'setBlocking');
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/stdout-producer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

// Produce a very long string, so that stdout will have to break it into chunks.
var str = 'test\n';
for (var i = 0; i < 17; i++, str += str);

// Add something so that we can identify the end.
str += 'hey\n';

process.stdout.write(str);

// Close the process, attempting to close before
// all chunked stdout writes are done.
//
// This is required to achieve the regression described in
// https://github.com/nodejs/io.js/issues/784.
process.exit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.exit() unconditionally terminates the process. Even when stdio is synchronous, there is no guarantee that the other end will have received it all.

EDIT: It's probably a bad idea in general to call process.exit() in tests, it tends to hide bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, however, I'm not able to produce the regression without it, what do you suggest?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, can you add a comment explaining that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, though I'm not sure of the specifics. I could take a dive into it if need be.

26 changes: 26 additions & 0 deletions test/parallel/test-child-process-chunked-stdout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const fork = require('child_process').fork;
const stream = require('stream');
const path = require('path');

const producer = fork(path.join(common.fixturesDir, 'stdout-producer.js'),
{ silent: true });

var output = '';
const writable = new stream.Writable({
write(chunk, _, next) {
output += chunk.toString();
next();
}
});

producer.stdout.pipe(writable);
producer.stdout.on('close', function() {
assert(output.endsWith('hey\n'),
'Not all chunked writes were completed before process termination.');
});

producer.stderr.pipe(process.stderr);