Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
child_process: refactor signal to use spawn function
  • Loading branch information
metcoder95 committed Jan 12, 2021
commit 9e3291e06a1053adffab5f738645f663bb7501fd
125 changes: 43 additions & 82 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ let debug = require('internal/util/debuglog').debuglog(
debug = fn;
}
);
let DOMException;
function lazyDOMException(message) {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return new DOMException(message);
}

const { Buffer } = require('buffer');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');

Expand Down Expand Up @@ -191,77 +186,28 @@ function exec(command, options, callback) {
}

const customPromiseExecFunction = (orig) => {
return (file, options = {}) => {
return (...file) => {
Comment thread
aduh95 marked this conversation as resolved.
Outdated
let child;

Comment thread
aduh95 marked this conversation as resolved.
Outdated
if (typeof options !== 'object' || options === null) {
return PromiseReject(
new ERR_INVALID_ARG_TYPE(
'options',
'Object',
options));
}

const { signal } = options;
const hasSignal = signal != null;

// Lets check if an AbortSignal is passed
if (hasSignal) {
const isNotValidSignal =
typeof signal !== 'object' || !('aborted' in signal);

if (isNotValidSignal) return PromiseReject(
new ERR_INVALID_ARG_TYPE(
'options.signal',
'AbortSignal',
signal));

// Already aborted
if (signal.aborted) return PromiseReject(lazyDOMException('AbortError'));
// Check if a callback is passed somehow
if (typeof file[file.length - 1] === 'function') {
file.pop();
}

const promise = new Promise((resolve, reject) => {
if (hasSignal) {
signal.addEventListener(
'abort',
rejectOnAbortSignal(reject),
{ once: true }
);
}

child = orig(file, options, (err, stdout, stderr) => {
if (hasSignal && signal.aborted) {
reject(err);
} else if (err !== null) {
child = orig(...file, (err, stdout, stderr) => {
if (err !== null) {
err.stdout = stdout;
err.stderr = stderr;
reject(err);
} else {
resolve({ stdout, stderr });
}

// Let's cleanup once exec is finished
if (hasSignal)
signal
.removeEventListener(
'abort',
rejectOnAbortSignal(reject),
{ once: true }
);
});
});

promise.child = child;

function rejectOnAbortSignal(reject) {
return () => {
if (promise && promise.child)
promise.child.kill(options.killSignal || 'SIGTERM');

reject(lazyDOMException('AbortError'));
};
}

return promise;
};
};
Expand Down Expand Up @@ -315,9 +261,6 @@ 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, {
Expand All @@ -327,7 +270,8 @@ function execFile(file /* , args, options, callback */) {
uid: options.uid,
shell: options.shell,
windowsHide: !!options.windowsHide,
windowsVerbatimArguments: !!options.windowsVerbatimArguments
windowsVerbatimArguments: !!options.windowsVerbatimArguments,
signal: options.signal
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you want spawn to work here you'd need to use spawnWithSignal and not spawn, but I don't think that'd work since kill below also deals with exec specifics.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, see the test failures below

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, basically it's getting called twice. I made a change on 8678797
Not totally sure if it's the right way to go, but fixes the double emitting of the event, please let me know your thoughts

Comment thread
aduh95 marked this conversation as resolved.
Outdated
});

let encoding;
Expand Down Expand Up @@ -429,28 +373,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 @@ -674,10 +602,43 @@ 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');

const onAbortListener = onAbortSignal(child);
Comment thread
aduh95 marked this conversation as resolved.
Outdated

// 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, { once: true }));
}

function onAbortSignal(childProcess) {
return function abortListener() {
if (childProcess.stdout) childProcess.stdout.destroy();

if (childProcess.stderr) childProcess.stderr.destroy();

process.nextTick(() => {
if (childProcess && childProcess.kill) {
childProcess.kill(options.killSignal);
}

childProcess.emit('error', new AbortError());
});
}
}
Comment thread
aduh95 marked this conversation as resolved.
}

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


return child;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ if (common.isWindows) {
const promise = execPromisifed(pwdcommand, { cwd: dir, signal });
assert.rejects(promise, /AbortError/);
Comment thread
aduh95 marked this conversation as resolved.
Outdated
ac.abort();
assert.strictEqual(promise.child.killed, true);
}

{
Expand Down Expand Up @@ -50,5 +49,4 @@ if (common.isWindows) {
const promise = execPromisifed(pwdcommand, { cwd: dir, signal });

assert.rejects(promise, /AbortError/);
assert.strictEqual(promise.child, undefined);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

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

const fixture = fixtures.path('exit.js');
const echoFixture = fixtures.path('echo.js');
const execOpts = { encoding: 'utf8', shell: true };
const execFilePromisified = promisify(execFile);

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

ac.abort();

assert.rejects(promise, /AbortError/);
}

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

const promise = execFilePromisified(process.execPath, [echoFixture, 0], { signal });

assert.rejects(promise, /AbortError/);
}

{
// Verify that if something different than Abortcontroller.signal
// is passed, ERR_INVALID_ARG_TYPE is thrown
const promise = execFilePromisified(process.execPath, [echoFixture, 0], { signal: {} });

assert.rejects(promise, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options.signal" property must be an ' +
'instance of AbortSignal. Received an instance of Object'
});

}

{
// Verify that if something different than Abortcontroller.signal
// is passed, ERR_INVALID_ARG_TYPE is thrown
const promise = execFilePromisified(process.execPath, [echoFixture, 0], { signal: 'world!' });

assert.rejects(promise, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options.signal" property must be an ' +
`instance of AbortSignal. Received type string ('world!')`
});

}
29 changes: 20 additions & 9 deletions test/parallel/test-child-process-execfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,32 @@ const execOpts = { encoding: 'utf8', shell: true };
const ac = new AbortController();
const { signal } = ac;

const test = () => {
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);
};
const callback = common.mustCall((err) => {
assert.strictEqual(err.code, 'ABORT_ERR');
assert.strictEqual(err.name, 'AbortError');
});

test();
execFile(process.execPath, [echoFixture, 0], { signal }, callback);
ac.abort();
// Verify that it still works the same way now that the signal is aborted.
test();
}

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

try {
execFile(process.execPath, [echoFixture, 0], { signal });
} catch (err) {
assert.strictEqual(err.code, 'ABORT_ERR');
assert.strictEqual(err.name, 'AbortError');
}

}

{
// Verify that if something different than Abortcontroller.signal
// is passed, ERR_INVALID_ARG_TYPE is thrown
Expand Down