Skip to content

Commit 598d2bd

Browse files
Trotttargos
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 <benjamingr@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
1 parent c3b1167 commit 598d2bd

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
@@ -362,6 +362,8 @@ function execFile(file /* , args, options, callback */) {
362362
}
363363
if (options.signal) {
364364
if (options.signal.aborted) {
365+
if (!ex)
366+
ex = new AbortError();
365367
process.nextTick(() => kill());
366368
} else {
367369
const childController = new AbortController();

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

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

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

66+
test();
7567
ac.abort();
68+
// Verify that it still works the same way now that the signal is aborted.
69+
test();
7670
}
7771

7872
{

0 commit comments

Comments
 (0)