From 5dbea327c1fa30deccebb486bc2f2c56a05377c3 Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Mon, 7 Oct 2019 12:56:39 -0700 Subject: [PATCH] fix(child-process): Use Set to manage book-keeping instead of mutable integer --- core/child-process/index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core/child-process/index.js b/core/child-process/index.js index fb64edb3ad..2edf97c69c 100644 --- a/core/child-process/index.js +++ b/core/child-process/index.js @@ -6,9 +6,9 @@ const execa = require("execa"); const logTransformer = require("strong-log-transformer"); // bookkeeping for spawned processes -let children = 0; +const children = new Set(); -// when streaming children are spawned, use this color for prefix +// when streaming processes are spawned, use this color for prefix const colorWheel = ["cyan", "magenta", "blue", "yellow", "green", "red"]; const NUM_COLORS = colorWheel.length; @@ -54,9 +54,9 @@ function spawnStreaming(command, args, opts, prefix) { } // Avoid "Possible EventEmitter memory leak detected" warning due to piped stdio - if (children > process.stdout.listenerCount("close")) { - process.stdout.setMaxListeners(children); - process.stderr.setMaxListeners(children); + if (children.size > process.stdout.listenerCount("close")) { + process.stdout.setMaxListeners(children.size); + process.stderr.setMaxListeners(children.size); } spawned.stdout.pipe(logTransformer(stdoutOpts)).pipe(process.stdout); @@ -66,7 +66,7 @@ function spawnStreaming(command, args, opts, prefix) { } function getChildProcessCount() { - return children; + return children.size; } function getExitCode(result) { @@ -86,11 +86,9 @@ function getExitCode(result) { } function spawnProcess(command, args, opts) { - children += 1; - const child = execa(command, args, opts); const drain = (code, signal) => { - children -= 1; + children.delete(child); // don't run repeatedly if this is the error event if (signal === undefined) { @@ -105,6 +103,8 @@ function spawnProcess(command, args, opts) { child.pkg = opts.pkg; } + children.add(child); + return child; }