Skip to content

Commit

Permalink
child_process: reduce abort handler code duplication
Browse files Browse the repository at this point in the history
Move duplicate abort handler logic into a separate function.

PR-URL: #36644
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
  • Loading branch information
Trott authored and targos committed Jun 11, 2021
1 parent 598d2bd commit e5fae63
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/child_process.js
Expand Up @@ -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();
Expand All @@ -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());
}
}
Expand Down

0 comments on commit e5fae63

Please sign in to comment.