From: Ryan Dahl Date: Sun, 9 May 2010 05:11:55 +0000 (-0700) Subject: Fix error reporting in child_process callbacks X-Git-Tag: v0.1.95~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=264e540d008a251a7e37f367611677414c2591d1;p=platform%2Fupstream%2Fnodejs.git Fix error reporting in child_process callbacks Issue 120, test case by Nathan Ostgard --- diff --git a/lib/child_process.js b/lib/child_process.js index 1a44505..b2ad2b6 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -86,7 +86,7 @@ exports.execFile = function (file, args /*, options, callback */) { function ChildProcess () { - process.EventEmitter.call(this); + EventEmitter.call(this); var self = this; @@ -99,11 +99,14 @@ function ChildProcess () { var stdout = this.stdout = new Stream(); var stderr = this.stderr = new Stream(); - stderr.onend = stdout.onend = function () { + function onClose () { if (gotCHLD && !stdout.readable && !stderr.readable) { self.emit('exit', exitCode, termSignal); } - }; + } + + stderr.addListener('close', onClose); + stdout.addListener('close', onClose); internal.onexit = function (code, signal) { gotCHLD = true; diff --git a/test/simple/test-child-process-exit-code.js b/test/simple/test-child-process-exit-code.js index 3a33dd4..5b14cfd 100644 --- a/test/simple/test-child-process-exit-code.js +++ b/test/simple/test-child-process-exit-code.js @@ -1,11 +1,30 @@ require("../common"); -var spawn = require('child_process').spawn - , path = require('path') - , sub = path.join(fixturesDir, 'exit.js') - , child = spawn(process.argv[0], [sub, 23]) - ; +spawn = require('child_process').spawn, +path = require('path'); -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(signal, null); -}); \ No newline at end of file + + 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); +});