From 78d4d91e54d3d8f3734fbda7bbd04e278dcdba56 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 28 Dec 2020 16:52:51 -0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/36644 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Daijiro Wachi --- lib/child_process.js | 2 ++ test/parallel/test-child-process-execfile.js | 28 ++++++++------------ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index b67ea1c7f05eca..54c3649f29f3b9 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -376,6 +376,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 1788b733934add..49a4bdbda29957 100644 --- a/test/parallel/test-child-process-execfile.js +++ b/test/parallel/test-child-process-execfile.js @@ -53,25 +53,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(); } {