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

Commit 9bf2975

Browse files
committed
Make sure Error object on exec() gets killed member
Also default to SIGTERM for destruction when exceeding timeout or buffer on exec()
1 parent d9a5edb commit 9bf2975

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

doc/api.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -1068,14 +1068,14 @@ There is a second optional argument to specify several options. The default opti
10681068
{ encoding: 'utf8'
10691069
, timeout: 0
10701070
, maxBuffer: 200*1024
1071-
, killSignal: 'SIGKILL'
1071+
, killSignal: 'SIGTERM'
10721072
, cwd: null
10731073
, env: null
10741074
}
10751075

10761076
If `timeout` is greater than 0, then it will kill the child process
10771077
if it runs longer than `timeout` milliseconds. The child process is killed with
1078-
`killSignal` (default: `'SIGKILL'`). `maxBuffer` specifies the largest
1078+
`killSignal` (default: `'SIGTERM'`). `maxBuffer` specifies the largest
10791079
amount of data allowed on stdout or stderr - if this value is exceeded then
10801080
the child process is killed.
10811081

lib/child_process.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ exports.execFile = function (file /* args, options, callback */) {
2828
var options = { encoding: 'utf8'
2929
, timeout: 0
3030
, maxBuffer: 200*1024
31-
, killSignal: 'SIGKILL'
31+
, killSignal: 'SIGTERM'
3232
, cwd: null
3333
, env: null
3434
};
@@ -62,33 +62,38 @@ exports.execFile = function (file /* args, options, callback */) {
6262
var stderr = "";
6363
var killed = false;
6464
var exited = false;
65+
var timeoutId;
6566

6667
function exithandler (code, signal) {
67-
if (timeoutId) clearTimeout(timeoutId);
6868
if (exited) return;
6969
exited = true;
70+
71+
if (timeoutId) {
72+
clearTimeout(timeoutId);
73+
timeoutId = null;
74+
}
75+
7076
if (!callback) return;
7177

7278
if (code === 0 && signal === null) {
7379
callback(null, stdout, stderr);
7480
} else {
7581
var e = new Error("Command failed: " + stderr);
76-
e.killed = killed;
82+
e.killed = child.killed || killed;
7783
e.code = code;
7884
e.signal = signal;
7985
callback(e, stdout, stderr);
8086
}
8187
}
8288

8389
function kill () {
84-
var c = child.kill(options.killSignal);
8590
killed = true;
91+
child.kill(options.killSignal);
8692
process.nextTick(function () {
8793
exithandler(null, options.killSignal)
8894
});
8995
}
9096

91-
var timeoutId;
9297
if (options.timeout > 0) {
9398
timeoutId = setTimeout(function () {
9499
kill();
@@ -113,8 +118,6 @@ exports.execFile = function (file /* args, options, callback */) {
113118
}
114119
});
115120

116-
var pid = child.pid;
117-
118121
child.addListener("exit", exithandler);
119122

120123
return child;
@@ -126,6 +129,8 @@ function ChildProcess () {
126129

127130
var self = this;
128131

132+
this.killed = false;
133+
129134
var gotCHLD = false;
130135
var exitCode;
131136
var termSignal;
@@ -172,6 +177,7 @@ util.inherits(ChildProcess, EventEmitter);
172177

173178
ChildProcess.prototype.kill = function (sig) {
174179
if (this._internal.pid) {
180+
this.killed = true;
175181
if (!constants) constants = process.binding("constants");
176182
sig = sig || 'SIGTERM';
177183
if (!constants[sig]) throw new Error("Unknown signal: " + sig);

test/simple/test-exec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ exec("sleep 3", { timeout: 50 }, function (err, stdout, stderr) {
4444
assert.ok(diff < 500);
4545
assert.ok(err);
4646
assert.ok(err.killed);
47-
assert.equal(err.signal, 'SIGKILL');
47+
assert.equal(err.signal, 'SIGTERM');
4848
});
4949

5050

@@ -69,7 +69,7 @@ function killMeTwiceCallback(err, stdout, stderr) {
6969
// parameters.
7070
assert.ok(err);
7171
assert.ok(err.killed);
72-
assert.equal(err.signal, 'SIGKILL');
72+
assert.equal(err.signal, 'SIGTERM');
7373

7474
// the timeout should still be in effect
7575
console.log("'sleep 3' was already killed. Took %d ms", diff);
@@ -81,7 +81,7 @@ function killMeTwiceCallback(err, stdout, stderr) {
8181
exec('python -c "print 200000*\'C\'"', { maxBuffer: 1000 }, function (err, stdout, stderr) {
8282
assert.ok(err);
8383
assert.ok(err.killed);
84-
assert.equal(err.signal, 'SIGKILL');
84+
assert.equal(err.signal, 'SIGTERM');
8585
});
8686

8787
process.addListener("exit", function () {

0 commit comments

Comments
 (0)