summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBenjamin Gruenbaum <benjamingr@gmail.com>2020-12-07 19:02:06 +0200
committerNode.js GitHub Bot <github-bot@iojs.org>2020-12-12 16:46:24 +0000
commit738cd60418ee1fc6b696d6f10d7604d667f01a23 (patch)
tree182a155f910e58b89d5468a16de4c8f22094eccf /lib
parentd50b2ff58341d6fc786dcd5b4e68330d14f96174 (diff)
downloadios-node-v8-738cd60418ee1fc6b696d6f10d7604d667f01a23.tar.gz
ios-node-v8-738cd60418ee1fc6b696d6f10d7604d667f01a23.tar.bz2
ios-node-v8-738cd60418ee1fc6b696d6f10d7604d667f01a23.zip
child_process: add signal support to spawn
PR-URL: https://github.com/nodejs/node/pull/36432 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/child_process.js26
1 files changed, 25 insertions, 1 deletions
diff --git a/lib/child_process.js b/lib/child_process.js
index af49655ec7..ae5a5f6587 100644
--- a/lib/child_process.js
+++ b/lib/child_process.js
@@ -749,6 +749,30 @@ 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) {
+ const child = spawn(file, args, options);
+
+ if (options && options.signal) {
+ // Validate signal, if present
+ validateAbortSignal(options.signal, 'options.signal');
+ function kill() {
+ if (child._handle) {
+ child._handle.kill('SIGTERM');
+ child.emit('error', new AbortError());
+ }
+ }
+ if (options.signal.aborted) {
+ process.nextTick(kill);
+ } else {
+ options.signal.addEventListener('abort', kill);
+ const remove = () => options.signal.removeEventListener('abort', kill);
+ child.once('close', remove);
+ }
+ }
+ return child;
+}
module.exports = {
_forkChild,
ChildProcess,
@@ -757,6 +781,6 @@ module.exports = {
execFileSync,
execSync,
fork,
- spawn,
+ spawn: spawnWithSignal,
spawnSync
};