Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class ProcessWrap : public HandleWrap {
object,
reinterpret_cast<uv_handle_t*>(&process_),
AsyncWrap::PROVIDER_PROCESSWRAP) {
process_.pid = 0;
Comment thread
avivkeller marked this conversation as resolved.
MarkAsUninitialized();
}

Expand Down Expand Up @@ -355,7 +356,10 @@ class ProcessWrap : public HandleWrap {
signal = SIGKILL;
}
#endif
int err = uv_process_kill(&wrap->process_, signal);
// uv_spawn() only assigns a pid when it succeeds, and kill(0, signal)
// signals every process in our own process group.
int err = wrap->process_.pid > 0 ? uv_process_kill(&wrap->process_, signal)
: UV_ESRCH;
args.GetReturnValue().Set(err);
}

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/child-process-kill-spawn-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { spawn } = require('child_process');
const child = spawn('foo123');
child.on('error', () => {});
if (child.kill() !== false || child.killed !== false) process.exit(1);
16 changes: 16 additions & 0 deletions test/parallel/test-child-process-kill-spawn-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const { spawn } = require('child_process');

// Killing a child process that never spawned must not signal the process
// group of the caller. The check runs in a detached child so that a
// regression cannot take the test runner down with it.
const childPath = fixtures.path('child-process-kill-spawn-error.js');
const child = spawn(process.execPath, [childPath], { detached: true });

child.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(signal, null);
assert.strictEqual(code, 0);
}));