Browse Source

Fix error reporting in child_process callbacks

Issue 120, test case by Nathan Ostgard
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
264e540d00
  1. 9
      lib/child_process.js
  2. 31
      test/simple/test-child-process-exit-code.js

9
lib/child_process.js

@ -86,7 +86,7 @@ exports.execFile = function (file, args /*, options, callback */) {
function ChildProcess () { function ChildProcess () {
process.EventEmitter.call(this); EventEmitter.call(this);
var self = this; var self = this;
@ -99,11 +99,14 @@ function ChildProcess () {
var stdout = this.stdout = new Stream(); var stdout = this.stdout = new Stream();
var stderr = this.stderr = new Stream(); var stderr = this.stderr = new Stream();
stderr.onend = stdout.onend = function () { function onClose () {
if (gotCHLD && !stdout.readable && !stderr.readable) { if (gotCHLD && !stdout.readable && !stderr.readable) {
self.emit('exit', exitCode, termSignal); self.emit('exit', exitCode, termSignal);
} }
}; }
stderr.addListener('close', onClose);
stdout.addListener('close', onClose);
internal.onexit = function (code, signal) { internal.onexit = function (code, signal) {
gotCHLD = true; gotCHLD = true;

31
test/simple/test-child-process-exit-code.js

@ -1,11 +1,30 @@
require("../common"); require("../common");
var spawn = require('child_process').spawn spawn = require('child_process').spawn,
, path = require('path') path = require('path');
, sub = path.join(fixturesDir, 'exit.js')
, child = spawn(process.argv[0], [sub, 23])
;
child.addListener('exit', function(code, signal) { exits = 0;
exitScript = path.join(fixturesDir, 'exit.js')
exitChild = spawn(process.argv[0], [exitScript, 23]);
exitChild.addListener('exit', function(code, signal) {
assert.strictEqual(code, 23); assert.strictEqual(code, 23);
assert.strictEqual(signal, null); assert.strictEqual(signal, null);
exits++;
});
errorScript = path.join(fixturesDir, 'child_process_should_emit_error.js')
errorChild = spawn(process.argv[0], [errorScript]);
errorChild.addListener('exit', function(code, signal) {
assert.ok(code !== 0);
assert.strictEqual(signal, null);
exits++;
});
process.addListener('exit', function () {
assert.equal(2, exits);
}); });
Loading…
Cancel
Save