Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process: allow promisified exec to be cancel #34249

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
eed3f17
child_process: allow promisified exec to be cancel
metcoder95 Jul 7, 2020
0ef841d
child_process: fix validation issue
metcoder95 Jul 13, 2020
70cb86c
child_process: use Primordials instead of plain Promise
metcoder95 Oct 23, 2020
a6ae623
child_process: refactor according to review
metcoder95 Oct 23, 2020
988ea3f
child_process: remove license from test
metcoder95 Oct 23, 2020
78e7521
child_process: improve testing
metcoder95 Oct 23, 2020
c8210f6
child_process: fix linter issues
metcoder95 Oct 23, 2020
3dca212
child_process: check if AbortSignal is aborted before rejecting/resol…
metcoder95 Oct 28, 2020
4163c44
child_process: check for signal before looking is aborted
metcoder95 Oct 28, 2020
9e3291e
child_process: refactor signal to use spawn function
metcoder95 Dec 22, 2020
3b14ebe
child_process: fix linter issues
metcoder95 Dec 22, 2020
087f924
child_process: revert check for extra callback
metcoder95 Dec 30, 2020
0fd0719
child_process: fix double AbortError emit
metcoder95 Dec 30, 2020
a41ac9a
child_process: fix linter issues
metcoder95 Dec 30, 2020
f9af3fd
child_process: fix fork issue with AbortController
metcoder95 Jan 12, 2021
2b19cd5
child_process: remove whitespace
metcoder95 Jan 12, 2021
f642015
child_process: re-order alphabetically the options
metcoder95 Jan 14, 2021
dade71b
child_process: refactor spawnWithSignal
metcoder95 Jan 18, 2021
5e7aa7d
child_process: remove wrapper function
metcoder95 Jan 18, 2021
2305734
child_process: revert refactor to spawn
metcoder95 Jan 18, 2021
a60b935
child_process: fix tests
metcoder95 Jan 18, 2021
fe077b0
child_process: refactor
metcoder95 Jan 21, 2021
72ff68a
child_process: refactor tests
metcoder95 Jan 21, 2021
026021a
child_process: refactor listeners args
metcoder95 Jan 21, 2021
c093035
child_process: inline listener
metcoder95 Jan 21, 2021
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
58 changes: 34 additions & 24 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function fork(modulePath /* , args, options */) {
options.execPath = options.execPath || process.execPath;
options.shell = false;

return spawnWithSignal(options.execPath, args, options);
return spawn(options.execPath, args, options);
}

function _forkChild(fd, serializationMode) {
Expand Down Expand Up @@ -254,17 +254,15 @@ function execFile(file /* , args, options, callback */) {
// Validate maxBuffer, if present.
validateMaxBuffer(options.maxBuffer);

// Validate signal, if present
validateAbortSignal(options.signal, 'options.signal');

options.killSignal = sanitizeKillSignal(options.killSignal);

const child = spawn(file, args, {
cwd: options.cwd,
env: options.env,
gid: options.gid,
uid: options.uid,
shell: options.shell,
signal: options.signal,
uid: options.uid,
windowsHide: !!options.windowsHide,
windowsVerbatimArguments: !!options.windowsVerbatimArguments
});
Expand Down Expand Up @@ -368,28 +366,12 @@ function execFile(file /* , args, options, callback */) {
}
}

function abortHandler() {
if (!ex)
ex = new AbortError();
process.nextTick(() => kill());
}

if (options.timeout > 0) {
timeoutId = setTimeout(function delayedKill() {
kill();
timeoutId = null;
}, options.timeout);
}
if (options.signal) {
if (options.signal.aborted) {
process.nextTick(abortHandler);
} else {
const childController = new AbortController();
options.signal.addEventListener('abort', abortHandler,
{ signal: childController.signal });
child.once('close', () => childController.abort());
}
}

if (child.stdout) {
if (encoding)
Expand Down Expand Up @@ -613,8 +595,31 @@ function normalizeSpawnArguments(file, args, options) {

function spawn(file, args, options) {
const child = new ChildProcess();

options = normalizeSpawnArguments(file, args, options);

if (options.signal) {
const signal = options.signal;
// Validate signal, if present
validateAbortSignal(signal, 'options.signal');

// Do nothing and throw if already aborted
if (signal.aborted) {
onAbortListener();
} else {
signal.addEventListener('abort', onAbortListener, { once: true });
child.once('close',
() => signal.removeEventListener('abort', onAbortListener));
}

function onAbortListener() {
process.nextTick(() => {
child?.kill?.(options.killSignal);

child.emit('error', new AbortError());
});
}
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
}

debug('spawn', options);
child.spawn(options);

Expand Down Expand Up @@ -754,14 +759,19 @@ function sanitizeKillSignal(killSignal) {
// This level of indirection is here because the other child_process methods
// call spawn internally but should use different cancellation logic.
function spawnWithSignal(file, args, options) {
const child = spawn(file, args, options);
// Remove signal from options to spawn
// to avoid double emitting of AbortError
const opts = options && typeof options === 'object' && ('signal' in options) ?
{ ...options, signal: undefined } :
options;
const child = spawn(file, args, opts);

if (options && options.signal) {
// Validate signal, if present
validateAbortSignal(options.signal, 'options.signal');
function kill() {
if (child._handle) {
child.kill('SIGTERM');
child._handle.kill(options.killSignal || 'SIGTERM');
child.emit('error', new AbortError());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const exec = require('child_process').exec;
const { promisify } = require('util');

let pwdcommand, dir;
const execPromisifed = promisify(exec);
const invalidArgTypeError = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
};


if (common.isWindows) {
pwdcommand = 'echo %cd%';
dir = 'c:\\windows';
} else {
pwdcommand = 'pwd';
dir = '/dev';
}


{
const ac = new AbortController();
const signal = ac.signal;
const promise = execPromisifed(pwdcommand, { cwd: dir, signal });
assert.rejects(promise, /AbortError/).then(common.mustCall());
ac.abort();
}

{
assert.throws(() => {
execPromisifed(pwdcommand, { cwd: dir, signal: {} });
}, invalidArgTypeError);
}

{
function signal() {}
assert.throws(() => {
execPromisifed(pwdcommand, { cwd: dir, signal });
}, invalidArgTypeError);
}

{
const ac = new AbortController();
const signal = (ac.abort(), ac.signal);
const promise = execPromisifed(pwdcommand, { cwd: dir, signal });

assert.rejects(promise, /AbortError/).then(common.mustCall());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { promisify } = require('util');
const execFile = require('child_process').execFile;
const fixtures = require('../common/fixtures');

const echoFixture = fixtures.path('echo.js');
const promisified = promisify(execFile);
const invalidArgTypeError = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
};

{
// Verify that the signal option works properly
const ac = new AbortController();
const signal = ac.signal;
const promise = promisified(process.execPath, [echoFixture, 0], { signal });

ac.abort();

assert.rejects(
promise,
{ name: 'AbortError' }
).then(common.mustCall());
}

{
// Verify that the signal option works properly when already aborted
const ac = new AbortController();
const { signal } = ac;
ac.abort();

assert.rejects(
promisified(process.execPath, [echoFixture, 0], { signal }),
{ name: 'AbortError' }
).then(common.mustCall());
}

{
// Verify that if something different than Abortcontroller.signal
// is passed, ERR_INVALID_ARG_TYPE is thrown
const signal = {};
assert.throws(() => {
promisified(process.execPath, [echoFixture, 0], { signal });
}, invalidArgTypeError);
}

{
// Verify that if something different than Abortcontroller.signal
// is passed, ERR_INVALID_ARG_TYPE is thrown
const signal = 'world!';
assert.throws(() => {
promisified(process.execPath, [echoFixture, 0], { signal });
}, invalidArgTypeError);
}
17 changes: 15 additions & 2 deletions test/parallel/test-child-process-execfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,23 @@ const execOpts = { encoding: 'utf8', shell: true };
execFile(process.execPath, [echoFixture, 0], { signal }, check);
};

test();
ac.abort();
// Verify that it still works the same way now that the signal is aborted.
test();
ac.abort();
}

{
// Verify that does not spawn a child if already aborted
const ac = new AbortController();
const { signal } = ac;
ac.abort();

const check = common.mustCall((err) => {
assert.strictEqual(err.code, 'ABORT_ERR');
assert.strictEqual(err.name, 'AbortError');
assert.strictEqual(err.signal, undefined);
});
execFile(process.execPath, [echoFixture, 0], { signal }, check);
}

{
Expand Down