diff --git a/lib/child_process.js b/lib/child_process.js index 23465a706d1..48fc5de81cb 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -536,12 +536,24 @@ exports.execFile = function(file /* args, options, callback */) { } } + function errorhandler(e) { + err = e; + child.stdout.destroy(); + child.stderr.destroy(); + exithandler(); + } + function kill() { + child.stdout.destroy(); + child.stderr.destroy(); + killed = true; - child.kill(options.killSignal); - process.nextTick(function() { - exithandler(null, options.killSignal); - }); + try { + child.kill(options.killSignal); + } catch (e) { + err = e; + exithandler(); + } } if (options.timeout > 0) { @@ -571,6 +583,7 @@ exports.execFile = function(file /* args, options, callback */) { }); child.addListener('close', exithandler); + child.addListener('error', errorhandler); return child; }; @@ -822,25 +835,43 @@ function errnoException(errorno, syscall, errmsg) { ChildProcess.prototype.kill = function(sig) { + var signal; + if (!constants) { constants = process.binding('constants'); } - sig = sig || 'SIGTERM'; - var signal = constants[sig]; + if (sig === 0) { + signal = 0; + } else if (!sig) { + signal = constants['SIGTERM']; + } else { + signal = constants[sig]; + } - if (!signal) { + if (signal === undefined) { throw new Error('Unknown signal: ' + sig); } if (this._handle) { - this.killed = true; var r = this._handle.kill(signal); - if (r === -1) { + if (r == 0) { + /* Success. */ + this.killed = true; + return true; + } else if (errno == 'ESRCH') { + /* Already dead. */ + } else if (errno == 'EINVAL' || errno == 'ENOSYS') { + /* The underlying platform doesn't support this signal. */ + throw errnoException(errno, 'kill'); + } else { + /* Other error, almost certainly EPERM. */ this.emit('error', errnoException(errno, 'kill')); - return; } } + + /* Kill didn't succeed. */ + return false; }; diff --git a/src/node.js b/src/node.js index ba0786d6ea2..4571c7757db 100644 --- a/src/node.js +++ b/src/node.js @@ -430,6 +430,8 @@ if (r) { throw errnoException(errno, 'kill'); } + + return true; }; };