-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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: fix spawn and fork AbortSignal behavior #37325
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -585,6 +585,18 @@ function normalizeSpawnArguments(file, args, options) { | |
}; | ||
} | ||
|
||
function abortChildProcess(child, killSignal) { | ||
if (!child) | ||
return; | ||
try { | ||
if (child.kill(killSignal)) { | ||
child.emit('error', new AbortError()); | ||
} | ||
} catch (err) { | ||
child.emit('error', err); | ||
} | ||
} | ||
|
||
|
||
function spawn(file, args, options) { | ||
const child = new ChildProcess(); | ||
|
@@ -594,21 +606,19 @@ function spawn(file, args, options) { | |
const signal = options.signal; | ||
// Validate signal, if present | ||
validateAbortSignal(signal, 'options.signal'); | ||
|
||
const killSignal = sanitizeKillSignal(options.killSignal); | ||
// Do nothing and throw if already aborted | ||
if (signal.aborted) { | ||
onAbortListener(); | ||
} else { | ||
signal.addEventListener('abort', onAbortListener, { once: true }); | ||
child.once('close', | ||
child.once('exit', | ||
() => signal.removeEventListener('abort', onAbortListener)); | ||
} | ||
|
||
function onAbortListener() { | ||
process.nextTick(() => { | ||
child?.kill?.(options.killSignal); | ||
|
||
child.emit('error', new AbortError()); | ||
abortChildProcess(child, killSignal); | ||
}); | ||
} | ||
} | ||
|
@@ -764,19 +774,18 @@ function spawnWithSignal(file, args, options) { | |
} | ||
const child = spawn(file, args, opts); | ||
|
||
if (options && options.signal) { | ||
if (options?.signal) { | ||
const killSignal = sanitizeKillSignal(options.killSignal); | ||
|
||
function kill() { | ||
if (child._handle) { | ||
child._handle.kill(options.killSignal || 'SIGTERM'); | ||
child.emit('error', new AbortError()); | ||
} | ||
abortChildProcess(child, killSignal); | ||
} | ||
if (options.signal.aborted) { | ||
process.nextTick(kill); | ||
} else { | ||
options.signal.addEventListener('abort', kill); | ||
options.signal.addEventListener('abort', kill, { once: true }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a fan of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just added it for consistency as one of the calls had it, in |
||
const remove = () => options.signal.removeEventListener('abort', kill); | ||
child.once('close', remove); | ||
child.once('exit', remove); | ||
} | ||
} | ||
return child; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just missing from the docs right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, although I did add
sanitizeKillSignal
for it (which doesn't do anything forundefined
, so technically it shouldn't affect anyone)