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

child_process: fix data loss with readable event #5036

Closed
wants to merge 1 commit into from

Conversation

mscdex
Copy link
Contributor

@mscdex mscdex commented Feb 2, 2016

This commit prevents child process stdio streams from being automatically flushed on child process exit/close if a 'readable' event handler has been attached at the time of exit.

Without this, child process stdio data can be lost if the process exits quickly and a read() (e.g. from a 'readable' handler) hasn't had the chance to get called yet.

Fixes: #5034

@mscdex mscdex added child_process Issues and PRs related to the child_process subsystem. lts-watch-v4.x labels Feb 2, 2016
@mscdex
Copy link
Contributor Author

mscdex commented Feb 2, 2016

I'm tagging this for LTS since the problem is also present in v4.x.

It's also a problem with v0.12.9, but the fix may be different -- I haven't looked yet.

}));

p.stdout.read();

setTimeout(function() {
p.kill();
}, 100);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the timeout here because I'm quite wary of such things as I've seen tests with timeouts like this become flaky.

Copy link
Contributor

Choose a reason for hiding this comment

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

you could choose to use common.platformTimeout

setTimeout(function() {
  p.kill();
}, common.platformTimeout(100);

That should solve the falkyness

@mscdex
Copy link
Contributor Author

mscdex commented Feb 2, 2016

@mscdex
Copy link
Contributor Author

mscdex commented Feb 2, 2016

CI is green except infrastructure-related CI failures.

@mscdex
Copy link
Contributor Author

mscdex commented Feb 2, 2016

This same fix should apply fine as-is to v4. I have a separate PR for v0.12 in #5037.

@evanlucas
Copy link
Contributor

LGTM if the CI is happy

@cjihrig
Copy link
Contributor

cjihrig commented Feb 2, 2016

LGTM. Might want to do another CI run.

p.on('close', common.mustCall(function(code, signal) {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
assert.strictEqual(Buffer.concat(buffer).toString(), '123\n');
Copy link
Member

Choose a reason for hiding this comment

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

Maybe .trim() the result? I'd expect this to fail when the line terminator is \r\n.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought so too, but CI passed on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went ahead and added trim() anyway.

@bnoordhuis
Copy link
Member

LGTM with a suggestion.

This commit prevents child process stdio streams from being
automatically flushed on child process exit/close if a 'readable'
event handler has been attached at the time of exit.

Without this, child process stdio data can be lost if the process
exits quickly and a `read()` (e.g. from a 'readable' handler)
hasn't had the chance to get called yet.

Fixes: nodejs#5034
@mscdex mscdex force-pushed the fix-child-process-flush-stdio branch from 1f8c83c to e528bdc Compare February 4, 2016 02:20
@mscdex
Copy link
Contributor Author

mscdex commented Feb 4, 2016

Adjusted test as suggested.

CI again: https://ci.nodejs.org/job/node-test-pull-request/1536/

@jasnell
Copy link
Member

jasnell commented Feb 4, 2016

Previous CI run had a build bot failure... just to be safe: https://ci.nodejs.org/job/node-test-pull-request/1545/

@jasnell
Copy link
Member

jasnell commented Feb 4, 2016

LGTM if CI is green

@mscdex
Copy link
Contributor Author

mscdex commented Feb 4, 2016

CI seems to be green except for some flaky tests.

mscdex added a commit that referenced this pull request Feb 11, 2016
This commit prevents child process stdio streams from being
automatically flushed on child process exit/close if a 'readable'
event handler has been attached at the time of exit.

Without this, child process stdio data can be lost if the process
exits quickly and a `read()` (e.g. from a 'readable' handler)
hasn't had the chance to get called yet.

Fixes: #5034
PR-URL: #5036
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@mscdex
Copy link
Contributor Author

mscdex commented Feb 11, 2016

Landed in 12274a5.

@mscdex mscdex closed this Feb 11, 2016
@mscdex mscdex deleted the fix-child-process-flush-stdio branch February 11, 2016 15:51
rvagg pushed a commit that referenced this pull request Feb 15, 2016
This commit prevents child process stdio streams from being
automatically flushed on child process exit/close if a 'readable'
event handler has been attached at the time of exit.

Without this, child process stdio data can be lost if the process
exits quickly and a `read()` (e.g. from a 'readable' handler)
hasn't had the chance to get called yet.

Fixes: #5034
PR-URL: #5036
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
stefanmb pushed a commit to stefanmb/node that referenced this pull request Feb 23, 2016
This commit prevents child process stdio streams from being
automatically flushed on child process exit/close if a 'readable'
event handler has been attached at the time of exit.

Without this, child process stdio data can be lost if the process
exits quickly and a `read()` (e.g. from a 'readable' handler)
hasn't had the chance to get called yet.

Fixes: nodejs#5034
PR-URL: nodejs#5036
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this pull request Mar 1, 2016
This commit prevents child process stdio streams from being
automatically flushed on child process exit/close if a 'readable'
event handler has been attached at the time of exit.

Without this, child process stdio data can be lost if the process
exits quickly and a `read()` (e.g. from a 'readable' handler)
hasn't had the chance to get called yet.

Fixes: #5034
PR-URL: #5036
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this pull request Mar 1, 2016
This commit prevents child process stdio streams from being
automatically flushed on child process exit/close if a 'readable'
event handler has been attached at the time of exit.

Without this, child process stdio data can be lost if the process
exits quickly and a `read()` (e.g. from a 'readable' handler)
hasn't had the chance to get called yet.

Fixes: #5034
PR-URL: #5036
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this pull request Mar 2, 2016
This commit prevents child process stdio streams from being
automatically flushed on child process exit/close if a 'readable'
event handler has been attached at the time of exit.

Without this, child process stdio data can be lost if the process
exits quickly and a `read()` (e.g. from a 'readable' handler)
hasn't had the chance to get called yet.

Fixes: #5034
PR-URL: #5036
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lost input from child processes due to initially-flowing net.Socket.
6 participants