Skip to content

Commit 78d4d91

Browse files
Trottdanielleadams
authored andcommitted
child_process: treat already-aborted controller as aborting
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 <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]>
1 parent d5d56ec commit 78d4d91

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

lib/child_process.js

+2
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ function execFile(file /* , args, options, callback */) {
376376
}
377377
if (options.signal) {
378378
if (options.signal.aborted) {
379+
if (!ex)
380+
ex = new AbortError();
379381
process.nextTick(() => kill());
380382
} else {
381383
const childController = new AbortController();

test/parallel/test-child-process-execfile.js

+11-17
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,19 @@ const execOpts = { encoding: 'utf8', shell: true };
5353
const ac = new AbortController();
5454
const { signal } = ac;
5555

56-
const firstCheck = common.mustCall((err) => {
57-
assert.strictEqual(err.code, 'ABORT_ERR');
58-
assert.strictEqual(err.name, 'AbortError');
59-
assert.strictEqual(err.signal, undefined);
60-
});
61-
62-
const secondCheck = common.mustCall((err) => {
63-
assert.strictEqual(err.code, null);
64-
assert.strictEqual(err.name, 'Error');
65-
assert.strictEqual(err.signal, 'SIGTERM');
66-
});
67-
68-
execFile(process.execPath, [echoFixture, 0], { signal }, (err) => {
69-
firstCheck(err);
70-
// Test that re-using the aborted signal results in immediate SIGTERM.
71-
execFile(process.execPath, [echoFixture, 0], { signal }, secondCheck);
72-
});
56+
const test = () => {
57+
const check = common.mustCall((err) => {
58+
assert.strictEqual(err.code, 'ABORT_ERR');
59+
assert.strictEqual(err.name, 'AbortError');
60+
assert.strictEqual(err.signal, undefined);
61+
});
62+
execFile(process.execPath, [echoFixture, 0], { signal }, check);
63+
};
7364

65+
test();
7466
ac.abort();
67+
// Verify that it still works the same way now that the signal is aborted.
68+
test();
7569
}
7670

7771
{

0 commit comments

Comments
 (0)