Skip to content
Browse files

Destroy stdout/stderr when child exits

Windows will not close the pipes until grandchildren exit (even if child exited).
Added a test to verify this.
  • Loading branch information...
1 parent 9f7f86b commit e8e460469538104cd8c75f931c0594961b1f34eb Elad Ben-Israel committed
View
8 lib/child_process.js
@@ -390,6 +390,14 @@ function ChildProcess() {
self.stdin.destroy();
}
+ if (self.stdout) {
+ self.stdout.destroy();
+ }
+
+ if (self.stderr) {
+ self.stderr.destroy();
+ }
+
self._internal.close();
self._internal = null;
View
9 test/fixtures/process-tree/child.js
@@ -0,0 +1,9 @@
+var spawn = require('child_process').spawn;
+var path = require('path');
+
+spawn(process.execPath, [ path.join(__dirname, 'grandchild') ]);
+
+// die quickly
+setTimeout(function() {
+ process.exit(1);
+}, 100);
View
2 test/fixtures/process-tree/grandchild.js
@@ -0,0 +1,2 @@
+// die after 10 seconds
+setTimeout(null, 10000);
View
37 test/simple/test-child-process-tree.js
@@ -0,0 +1,37 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var spawn = require('child_process').spawn;
+var assert = require('assert');
+var path = require('path');
+
+// spawn a child and raise `exitEvent` flag when it exits.
+var child = spawn(process.execPath, [ path.join(common.fixturesDir, 'process-tree', 'child') ]);
+child.on('exit', function(code) {
+ process.exit(0); // test passed
+});
+
+// child.js exits after 0.5 second. set a timeout for 1sec and verify that
+// indeed we received an `exit` event from the child. if not, fail the test.
+setTimeout(function() {
+ assert.ok(false, 'Expecting `exit` event to be emitted since child exited, dah!');
+}, 2000);

0 comments on commit e8e4604

Please sign in to comment.
Something went wrong with that request. Please try again.