From 6ad629895d5491036d1362c77516abc9c6bebd14 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Mon, 17 Jan 2011 22:59:25 +0100 Subject: [PATCH] Make child_process.kill always work on windows --- TODO.win32 | 3 --- lib/child_process.js | 5 ++--- src/node_child_process.cc | 9 +++++---- src/node_child_process_win32.cc | 6 +++--- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/TODO.win32 b/TODO.win32 index ff2daa1cad..7e8528123a 100644 --- a/TODO.win32 +++ b/TODO.win32 @@ -23,9 +23,6 @@ block; on windows a libeio thread is used to call CreateProcess. So this can't really be fixed, but it could be worked around by adding a 'spawn' or 'pid' event. - * kill() doesn't work when the pid is not available yet. All the plumbing - is there to make it work, but lib/child_process.js just doesn't call - ChildProcess::Kill() as long as the pid is not known. * passing socket custom_fds is not supported * child_process.exec() only works on systems with msys installed. It's because it relies on the 'sh' shell. The default windows shell diff --git a/lib/child_process.js b/lib/child_process.js index 520961fd1a..bf899da791 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -172,12 +172,11 @@ util.inherits(ChildProcess, EventEmitter); ChildProcess.prototype.kill = function(sig) { - if (this._internal.pid) { - this.killed = true; + if (!this.killed && !this.exited) { if (!constants) constants = process.binding('constants'); sig = sig || 'SIGTERM'; if (!constants[sig]) throw new Error('Unknown signal: ' + sig); - return this._internal.kill(constants[sig]); + this.killed = this._internal.kill(constants[sig]); } }; diff --git a/src/node_child_process.cc b/src/node_child_process.cc index 6c5c1eb075..3cfceaaf09 100644 --- a/src/node_child_process.cc +++ b/src/node_child_process.cc @@ -240,7 +240,8 @@ Handle ChildProcess::Kill(const Arguments& args) { assert(child); if (child->pid_ < 1) { - return ThrowException(Exception::Error(String::New("No such process"))); + // nothing to do + return False(); } int sig = SIGTERM; @@ -249,15 +250,15 @@ Handle ChildProcess::Kill(const Arguments& args) { if (args[0]->IsNumber()) { sig = args[0]->Int32Value(); } else { - return ThrowException(Exception::Error(String::New("Bad argument."))); + return ThrowException(Exception::TypeError(String::New("Bad argument."))); } } if (child->Kill(sig) != 0) { - return ThrowException(Exception::Error(String::New(strerror(errno)))); + return ThrowException(ErrnoException(errno, "Kill")); } - return Undefined(); + return True(); } diff --git a/src/node_child_process_win32.cc b/src/node_child_process_win32.cc index f3d49bd181..fd47d8c55c 100644 --- a/src/node_child_process_win32.cc +++ b/src/node_child_process_win32.cc @@ -810,15 +810,15 @@ Handle ChildProcess::Kill(const Arguments& args) { if (args[0]->IsNumber()) { sig = args[0]->Int32Value(); } else { - return ThrowException(Exception::Error(String::New("Bad argument."))); + return ThrowException(Exception::TypeError(String::New("Bad argument."))); } } if (do_kill(child, sig) != 0) { - return ThrowException(Exception::Error(String::New(strerror(errno)))); + return ThrowException(ErrnoException(GetLastError())); } - return Undefined(); + return True(); }