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 <benjamingr@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
This commit is contained in:
Rich Trott authored and Michaël Zasso committed 2021-06-11 09:29:53 +02:00
1 parent 598d2bdf09
commit e5fae63108
1 file changed
+9 -8
+9 -8
View File
@@ -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());
}
}