Skip to content

Commit

Permalink
child_process: treat already-aborted controller as aborting
Browse files Browse the repository at this point in the history
If an AbortController passed to execfile() is already aborted, use the
same behavior as if the controller was aborted after calling execfile().
This mimics the behavior of fetch in the browser.

PR-URL: #36644
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
  • Loading branch information
Trott authored and targos committed Jun 11, 2021
1 parent c3b1167 commit 598d2bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/child_process.js
Expand Up @@ -362,6 +362,8 @@ function execFile(file /* , args, options, callback */) {
}
if (options.signal) {
if (options.signal.aborted) {
if (!ex)
ex = new AbortError();
process.nextTick(() => kill());
} else {
const childController = new AbortController();
Expand Down
28 changes: 11 additions & 17 deletions test/parallel/test-child-process-execfile.js
Expand Up @@ -54,25 +54,19 @@ const execOpts = { encoding: 'utf8', shell: true };
const ac = new AbortController();
const { signal } = ac;

const firstCheck = common.mustCall((err) => {
assert.strictEqual(err.code, 'ABORT_ERR');
assert.strictEqual(err.name, 'AbortError');
assert.strictEqual(err.signal, undefined);
});

const secondCheck = common.mustCall((err) => {
assert.strictEqual(err.code, null);
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.signal, 'SIGTERM');
});

execFile(process.execPath, [echoFixture, 0], { signal }, (err) => {
firstCheck(err);
// Test that re-using the aborted signal results in immediate SIGTERM.
execFile(process.execPath, [echoFixture, 0], { signal }, secondCheck);
});
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);
};

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

{
Expand Down

0 comments on commit 598d2bd

Please sign in to comment.