Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: make stdout & stderr block on all platforms #1771

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ecmaFeatures:
generators: true
forOf: true
objectLiteralShorthandProperties: true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

used here: https://github.com/nodejs/io.js/pull/1771/files#diff-7718507eb7a71106a68401cae76c5268R14

I don't see a good reason not to enable these other than backporting, maybe.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, we had to enable one of these in #1760 already.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I only needed one here, but might as well enable both.

objectLiteralShorthandMethods: true

rules:
# Possible Errors
Expand Down
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);