Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Fix child_process.kill oddities
Browse files Browse the repository at this point in the history
* Failure should be reported by throwing, like process.kill().
* `exec`-ed processes should not blow up when, upon reaching time /
  buffer limit, an attempt is made to kill them, and this kill fails
  because the process already exited.
* Document this behaviour.

Fixes: #3409
  • Loading branch information
piscisaureus committed Jun 12, 2012
1 parent a55faea commit 486a5b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
10 changes: 8 additions & 2 deletions doc/api/child_process.markdown
Expand Up @@ -119,8 +119,14 @@ be sent `'SIGTERM'`. See `signal(7)` for a list of available signals.
// send SIGHUP to process
grep.kill('SIGHUP');

Note that while the function is called `kill`, the signal delivered to the child
process may not actually kill it. `kill` really just sends a signal to a process.
Although the function is called `kill`, the signal delivered to the child
process may not actually kill it. `kill` really just sends a signal to a
process.

Also note that if the child process already exited when `kill` is called,
an exception will be thrown. This can even happen when the `child_process`
object did not emit `exit yet. Therefore it is wise to wrap the `kill` call in

This comment has been minimized.

Copy link
@isaacs

isaacs Jun 12, 2012

s/exit/exit/` (missing the closing tick.)

a try..catch block.

See `kill(2)`

Expand Down
13 changes: 7 additions & 6 deletions lib/child_process.js
Expand Up @@ -538,10 +538,12 @@ exports.execFile = function(file /* args, options, callback */) {

function kill() {
killed = true;
child.kill(options.killSignal);
process.nextTick(function() {
exithandler(null, options.killSignal);
});
try {
child.kill(options.killSignal);
} catch (e) {
}
child.stdout.destroy();
child.stderr.destroy();
}

if (options.timeout > 0) {
Expand Down Expand Up @@ -837,8 +839,7 @@ ChildProcess.prototype.kill = function(sig) {
this.killed = true;
var r = this._handle.kill(signal);
if (r === -1) {
this.emit('error', errnoException(errno, 'kill'));
return;
throw errnoException(errno, 'kill');
}
}
};
Expand Down

0 comments on commit 486a5b3

Please sign in to comment.