diff --git a/lib/child_process.js b/lib/child_process.js index ad5bce3db044d9..1482566ba893dc 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -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(); diff --git a/test/parallel/test-child-process-execfile.js b/test/parallel/test-child-process-execfile.js index bf1dfda8bfe982..28bb498ea4b51d 100644 --- a/test/parallel/test-child-process-execfile.js +++ b/test/parallel/test-child-process-execfile.js @@ -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(); } {