From: Jérôme Benoit Date: Mon, 1 Apr 2024 20:42:37 +0000 (+0200) Subject: fix: properly fix race condition at worker node recreation X-Git-Tag: v3.1.29~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b362a92937bfb85e32f7a69030ff3a35a54151be;p=poolifier.git fix: properly fix race condition at worker node recreation Signed-off-by: Jérôme Benoit --- 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() } })