Skip to content

Commit cdfc1c8

Browse files
Linkgoronruyadorno
authored andcommitted
child_process: cleanup AbortSignal duplication
cleanup AbortSignal child_process code duplication PR-URL: #37823 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9defe10 commit cdfc1c8

File tree

1 file changed

+16
-49
lines changed

1 file changed

+16
-49
lines changed

lib/child_process.js

+16-49
Original file line numberDiff line numberDiff line change
@@ -605,23 +605,6 @@ function spawn(file, args, options) {
605605
const killSignal = sanitizeKillSignal(options.killSignal);
606606
const child = new ChildProcess();
607607

608-
if (options.signal) {
609-
const signal = options.signal;
610-
if (signal.aborted) {
611-
onAbortListener();
612-
} else {
613-
signal.addEventListener('abort', onAbortListener, { once: true });
614-
child.once('exit',
615-
() => signal.removeEventListener('abort', onAbortListener));
616-
}
617-
618-
function onAbortListener() {
619-
process.nextTick(() => {
620-
abortChildProcess(child, killSignal);
621-
});
622-
}
623-
}
624-
625608
debug('spawn', options);
626609
child.spawn(options);
627610

@@ -645,6 +628,21 @@ function spawn(file, args, options) {
645628
});
646629
}
647630

631+
if (options.signal) {
632+
const signal = options.signal;
633+
if (signal.aborted) {
634+
process.nextTick(onAbortListener);
635+
} else {
636+
signal.addEventListener('abort', onAbortListener, { once: true });
637+
child.once('exit',
638+
() => signal.removeEventListener('abort', onAbortListener));
639+
}
640+
641+
function onAbortListener() {
642+
abortChildProcess(child, killSignal);
643+
}
644+
}
645+
648646
return child;
649647
}
650648

@@ -778,37 +776,6 @@ function sanitizeKillSignal(killSignal) {
778776
}
779777
}
780778

781-
// This level of indirection is here because the other child_process methods
782-
// call spawn internally but should use different cancellation logic.
783-
function spawnWithSignal(file, args, options) {
784-
// Remove signal from options to spawn
785-
// to avoid double emitting of AbortError
786-
const opts = options && typeof options === 'object' && ('signal' in options) ?
787-
{ ...options, signal: undefined } :
788-
options;
789-
790-
if (options?.signal) {
791-
// Validate signal, if present
792-
validateAbortSignal(options.signal, 'options.signal');
793-
}
794-
const child = spawn(file, args, opts);
795-
796-
if (options?.signal) {
797-
const killSignal = sanitizeKillSignal(options.killSignal);
798-
799-
function kill() {
800-
abortChildProcess(child, killSignal);
801-
}
802-
if (options.signal.aborted) {
803-
process.nextTick(kill);
804-
} else {
805-
options.signal.addEventListener('abort', kill, { once: true });
806-
const remove = () => options.signal.removeEventListener('abort', kill);
807-
child.once('exit', remove);
808-
}
809-
}
810-
return child;
811-
}
812779
module.exports = {
813780
_forkChild,
814781
ChildProcess,
@@ -817,6 +784,6 @@ module.exports = {
817784
execFileSync,
818785
execSync,
819786
fork,
820-
spawn: spawnWithSignal,
787+
spawn,
821788
spawnSync
822789
};

0 commit comments

Comments
 (0)