Skip to content

Commit 784eb2f

Browse files
committed
child_process: exit spawnSync with null on signal
This commit sets the spawnSync() exit code to null when the child is killed via signal. This brings the behavior more in sync with spawn(). Fixes: #11284 PR-URL: #11288 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
1 parent b7ac0b2 commit 784eb2f

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/spawn_sync.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,12 +650,17 @@ Local<Object> SyncProcessRunner::BuildResultObject() {
650650
Integer::New(env()->isolate(), GetError()));
651651
}
652652

653-
if (exit_status_ >= 0)
654-
js_result->Set(env()->status_string(),
655-
Number::New(env()->isolate(), static_cast<double>(exit_status_)));
656-
else
653+
if (exit_status_ >= 0) {
654+
if (term_signal_ > 0) {
655+
js_result->Set(env()->status_string(), Null(env()->isolate()));
656+
} else {
657+
js_result->Set(env()->status_string(),
658+
Number::New(env()->isolate(), static_cast<double>(exit_status_)));
659+
}
660+
} else {
657661
// If exit_status_ < 0 the process was never started because of some error.
658662
js_result->Set(env()->status_string(), Null(env()->isolate()));
663+
}
659664

660665
if (term_signal_ > 0)
661666
js_result->Set(env()->signal_string(),

test/parallel/test-child-process-spawnsync-kill-signal.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
33
const assert = require('assert');
44
const cp = require('child_process');
55

66
if (process.argv[2] === 'child') {
77
setInterval(() => {}, 1000);
88
} else {
9-
const exitCode = common.isWindows ? 1 : 0;
109
const { SIGKILL } = process.binding('constants').os.signals;
1110

1211
function spawn(killSignal) {
1312
const child = cp.spawnSync(process.execPath,
1413
[__filename, 'child'],
1514
{killSignal, timeout: 100});
1615

17-
assert.strictEqual(child.status, exitCode);
16+
assert.strictEqual(child.status, null);
1817
assert.strictEqual(child.error.code, 'ETIMEDOUT');
1918
return child;
2019
}

0 commit comments

Comments
 (0)