Skip to content

Commit 743a009

Browse files
sam-githubtrevnorris
authored andcommitted
process: pid can be a string in process.kill()
Not allowing string was a change from v0.10 behaviour, commented on in nodejs/node-v0.x-archive#7991. Allow them again, but still check that argument is numberish. Also, simplify the fragile and non-portable test code introduced in 832ec1c that required fixups 2a41535, and ef3c4ed. PR-URL: nodejs/node-v0.x-archive#8531 Reviewed-by: Trevor Norris <trev.norris@gmail.com>
1 parent f6556b6 commit 743a009

2 files changed

Lines changed: 39 additions & 51 deletions

File tree

src/node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@
641641
process.kill = function(pid, sig) {
642642
var err;
643643

644-
if (typeof pid !== 'number' || !isFinite(pid)) {
645-
throw new TypeError('pid must be a number');
644+
if (pid != (pid | 0)) {
645+
throw new TypeError('invalid pid');
646646
}
647647

648648
// preserve null signal

test/simple/test-process-kill-pid.js

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@
2323
var common = require('../common');
2424
var assert = require('assert');
2525

26-
var pass;
27-
var wait = setInterval(function(){}, 1000);
28-
29-
process.on('exit', function(code) {
30-
if (code === 0) {
31-
assert.ok(pass);
32-
}
33-
});
34-
3526
// test variants of pid
3627
//
3728
// null: TypeError
@@ -43,58 +34,55 @@ process.on('exit', function(code) {
4334
//
4435
// Nan, Infinity, -Infinity: TypeError
4536
//
46-
// 0: our group process
37+
// 0, String(0): our group process
4738
//
48-
// process.pid: ourself
39+
// process.pid, String(process.pid): ourself
4940

5041
assert.throws(function() { process.kill('SIGTERM'); }, TypeError);
51-
assert.throws(function() { process.kill(String(process.pid)); }, TypeError);
5242
assert.throws(function() { process.kill(null); }, TypeError);
5343
assert.throws(function() { process.kill(undefined); }, TypeError);
5444
assert.throws(function() { process.kill(+'not a number'); }, TypeError);
5545
assert.throws(function() { process.kill(1/0); }, TypeError);
5646
assert.throws(function() { process.kill(-1/0); }, TypeError);
5747

58-
/* Sending SIGHUP is not supported on Windows */
59-
if (process.platform === 'win32') {
60-
pass = true;
61-
clearInterval(wait);
62-
return;
63-
}
48+
// Test kill argument processing in valid cases.
49+
//
50+
// Monkey patch _kill so that we don't actually send any signals, particularly
51+
// that we don't kill our process group, or try to actually send ANY signals on
52+
// windows, which doesn't support them.
53+
function kill(tryPid, trySig, expectPid, expectSig) {
54+
var getPid;
55+
var getSig;
56+
var origKill = process._kill;
57+
process._kill = function(pid, sig) {
58+
getPid = pid;
59+
getSig = sig;
6460

65-
process.once('SIGHUP', function() {
66-
process.once('SIGHUP', function() {
67-
pass = true;
68-
clearInterval(wait);
69-
});
70-
process.kill(process.pid, 'SIGHUP');
71-
});
61+
// un-monkey patch process._kill
62+
process._kill = origKill;
63+
};
7264

65+
process.kill(tryPid, trySig);
7366

74-
/*
75-
* Monkey patch _kill so that, in the specific case
76-
* when we want to test sending a signal to pid 0,
77-
* we don't actually send the signal to the process group.
78-
* Otherwise, it could cause a lot of trouble, like terminating
79-
* the test runner, or any other process that belongs to the
80-
* same process group as this test.
81-
*/
82-
var origKill = process._kill;
83-
process._kill = function(pid, sig) {
84-
/*
85-
* make sure we patch _kill only when
86-
* we want to test sending a signal
87-
* to the process group.
88-
*/
89-
assert.strictEqual(pid, 0);
67+
assert.equal(getPid, expectPid);
68+
assert.equal(getSig, expectSig);
69+
}
9070

91-
// make sure that _kill is passed the correct args types
92-
assert(typeof pid === 'number');
93-
assert(typeof sig === 'number');
71+
// Note that SIGHUP and SIGTERM map to 1 and 15 respectively, even on Windows
72+
// (for Windows, libuv maps 1 and 15 to the correct behaviour).
9473

95-
// un-monkey patch process._kill
96-
process._kill = origKill;
97-
process._kill(process.pid, sig);
98-
}
74+
kill(0, 'SIGHUP', 0, 1);
75+
kill(0, undefined, 0, 15);
76+
kill('0', 'SIGHUP', 0, 1);
77+
kill('0', undefined, 0, 15);
78+
79+
// negative numbers are meaningful on unix
80+
kill(-1, 'SIGHUP', -1, 1);
81+
kill(-1, undefined, -1, 15);
82+
kill('-1', 'SIGHUP', -1, 1);
83+
kill('-1', undefined, -1, 15);
9984

100-
process.kill(0, 'SIGHUP');
85+
kill(process.pid, 'SIGHUP', process.pid, 1);
86+
kill(process.pid, undefined, process.pid, 15);
87+
kill(String(process.pid), 'SIGHUP', process.pid, 1);
88+
kill(String(process.pid), undefined, process.pid, 15);

0 commit comments

Comments
 (0)