From e5fae631080dfa1f147c735cf10894e608ee4983 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 28 Dec 2020 18:18:46 -0800 Subject: [PATCH] child_process: reduce abort handler code duplication Move duplicate abort handler logic into a separate function. PR-URL: https://github.com/nodejs/node/pull/36644 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Daijiro Wachi --- lib/child_process.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 1482566ba893dc..42bdaa86448e83 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -354,6 +354,12 @@ function execFile(file /* , args, options, callback */) { } } + function abortHandler() { + if (!ex) + ex = new AbortError(); + process.nextTick(() => kill()); + } + if (options.timeout > 0) { timeoutId = setTimeout(function delayedKill() { kill(); @@ -362,16 +368,11 @@ function execFile(file /* , args, options, callback */) { } if (options.signal) { if (options.signal.aborted) { - if (!ex) - ex = new AbortError(); - process.nextTick(() => kill()); + process.nextTick(abortHandler); } else { const childController = new AbortController(); - options.signal.addEventListener('abort', () => { - if (!ex) - ex = new AbortError(); - kill(); - }, { signal: childController.signal }); + options.signal.addEventListener('abort', abortHandler, + { signal: childController.signal }); child.once('close', () => childController.abort()); } }