Skip to content

Commit 947d07b

Browse files
cjihrigMylesBorins
authored andcommitted
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 <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent ce01372 commit 947d07b

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
@@ -645,12 +645,17 @@ Local<Object> SyncProcessRunner::BuildResultObject() {
645645
Integer::New(env()->isolate(), GetError()));
646646
}
647647

648-
if (exit_status_ >= 0)
649-
js_result->Set(env()->status_string(),
650-
Number::New(env()->isolate(), static_cast<double>(exit_status_)));
651-
else
648+
if (exit_status_ >= 0) {
649+
if (term_signal_ > 0) {
650+
js_result->Set(env()->status_string(), Null(env()->isolate()));
651+
} else {
652+
js_result->Set(env()->status_string(),
653+
Number::New(env()->isolate(), static_cast<double>(exit_status_)));
654+
}
655+
} else {
652656
// If exit_status_ < 0 the process was never started because of some error.
653657
js_result->Set(env()->status_string(), Null(env()->isolate()));
658+
}
654659

655660
if (term_signal_ > 0)
656661
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)