Skip to content

Commit

Permalink
child_process: exit spawnSync with null on signal
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cjihrig committed Feb 14, 2017
1 parent b7ac0b2 commit 784eb2f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/spawn_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,17 @@ Local<Object> SyncProcessRunner::BuildResultObject() {
Integer::New(env()->isolate(), GetError()));
}

if (exit_status_ >= 0)
js_result->Set(env()->status_string(),
Number::New(env()->isolate(), static_cast<double>(exit_status_)));
else
if (exit_status_ >= 0) {
if (term_signal_ > 0) {
js_result->Set(env()->status_string(), Null(env()->isolate()));
} else {
js_result->Set(env()->status_string(),
Number::New(env()->isolate(), static_cast<double>(exit_status_)));
}
} else {
// If exit_status_ < 0 the process was never started because of some error.
js_result->Set(env()->status_string(), Null(env()->isolate()));
}

if (term_signal_ > 0)
js_result->Set(env()->signal_string(),
Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-child-process-spawnsync-kill-signal.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');
const cp = require('child_process');

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

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

assert.strictEqual(child.status, exitCode);
assert.strictEqual(child.status, null);
assert.strictEqual(child.error.code, 'ETIMEDOUT');
return child;
}
Expand Down

0 comments on commit 784eb2f

Please sign in to comment.