From b362a92937bfb85e32f7a69030ff3a35a54151be Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 1 Apr 2024 22:42:37 +0200 Subject: [PATCH] fix: properly fix race condition at worker node recreation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 5c153087..5eeb5791 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -117,6 +117,10 @@ export abstract class AbstractPool< * Whether the pool is destroying or not. */ private destroying: boolean + /** + * Whether the minimum number of workers is starting or not. + */ + private startingMinimumNumberOfWorkers: boolean /** * Whether the pool ready event has been emitted or not. */ @@ -175,6 +179,7 @@ export abstract class AbstractPool< this.starting = false this.destroying = false this.readyEventEmitted = false + this.startingMinimumNumberOfWorkers = false if (this.opts.startWorkers === true) { this.start() } @@ -953,6 +958,7 @@ export abstract class AbstractPool< * Starts the minimum number of workers. */ private startMinimumNumberOfWorkers (): void { + this.startingMinimumNumberOfWorkers = true while ( this.workerNodes.reduce( (accumulator, workerNode) => @@ -962,6 +968,7 @@ export abstract class AbstractPool< ) { this.createAndSetupWorkerNode() } + this.startingMinimumNumberOfWorkers = false } /** @inheritdoc */ @@ -1251,7 +1258,7 @@ export abstract class AbstractPool< ) { if (workerNode.info.dynamic) { this.createAndSetupDynamicWorkerNode() - } else { + } else if (!this.startingMinimumNumberOfWorkers) { this.startMinimumNumberOfWorkers() } } @@ -1273,7 +1280,11 @@ export abstract class AbstractPool< ) workerNode.registerOnceWorkerEventHandler('exit', () => { this.removeWorkerNode(workerNode) - if (this.started && !this.destroying) { + if ( + this.started && + !this.startingMinimumNumberOfWorkers && + !this.destroying + ) { this.startMinimumNumberOfWorkers() } }) -- 2.34.1