Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process: cleanup AbortSignal code duplication #37823

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 18 additions & 49 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,23 +605,6 @@ function spawn(file, args, options) {
const killSignal = sanitizeKillSignal(options.killSignal);
const child = new ChildProcess();

if (options.signal) {
const signal = options.signal;
if (signal.aborted) {
onAbortListener();
} else {
signal.addEventListener('abort', onAbortListener, { once: true });
child.once('exit',
() => signal.removeEventListener('abort', onAbortListener));
}

function onAbortListener() {
process.nextTick(() => {
abortChildProcess(child, killSignal);
});
}
}

debug('spawn', options);
child.spawn(options);

Expand All @@ -645,6 +628,23 @@ function spawn(file, args, options) {
});
}

if (options.signal) {
const signal = options.signal;
if (signal.aborted) {
process.nextTick(() => {
onAbortListener();
});
Linkgoron marked this conversation as resolved.
Show resolved Hide resolved
} else {
signal.addEventListener('abort', onAbortListener, { once: true });
child.once('exit',
() => signal.removeEventListener('abort', onAbortListener));
}

function onAbortListener() {
abortChildProcess(child, killSignal);
}
}

return child;
}

Expand Down Expand Up @@ -778,37 +778,6 @@ function sanitizeKillSignal(killSignal) {
}
}

// This level of indirection is here because the other child_process methods
// call spawn internally but should use different cancellation logic.
function spawnWithSignal(file, args, options) {
// Remove signal from options to spawn
// to avoid double emitting of AbortError
const opts = options && typeof options === 'object' && ('signal' in options) ?
{ ...options, signal: undefined } :
options;

if (options?.signal) {
// Validate signal, if present
validateAbortSignal(options.signal, 'options.signal');
}
const child = spawn(file, args, opts);

if (options?.signal) {
const killSignal = sanitizeKillSignal(options.killSignal);

function kill() {
abortChildProcess(child, killSignal);
}
if (options.signal.aborted) {
process.nextTick(kill);
} else {
options.signal.addEventListener('abort', kill, { once: true });
const remove = () => options.signal.removeEventListener('abort', kill);
child.once('exit', remove);
}
}
return child;
}
module.exports = {
_forkChild,
ChildProcess,
Expand All @@ -817,6 +786,6 @@ module.exports = {
execFileSync,
execSync,
fork,
spawn: spawnWithSignal,
spawn,
spawnSync
};