fix: properly fix race condition at worker node recreation
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 1 Apr 2024 20:42:37 +0000 (22:42 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 1 Apr 2024 20:42:37 +0000 (22:42 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts

index 5c153087640dc76e09b029deaafdd72c3fa1f47b..5eeb57911903587c0760dac8fee11e30407cb86a 100644 (file)
@@ -117,6 +117,10 @@ export abstract class AbstractPool<
    * Whether the pool is destroying or not.
    */
   private destroying: boolean
    * 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.
    */
   /**
    * 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.starting = false
     this.destroying = false
     this.readyEventEmitted = false
+    this.startingMinimumNumberOfWorkers = false
     if (this.opts.startWorkers === true) {
       this.start()
     }
     if (this.opts.startWorkers === true) {
       this.start()
     }
@@ -953,6 +958,7 @@ export abstract class AbstractPool<
    * Starts the minimum number of workers.
    */
   private startMinimumNumberOfWorkers (): void {
    * Starts the minimum number of workers.
    */
   private startMinimumNumberOfWorkers (): void {
+    this.startingMinimumNumberOfWorkers = true
     while (
       this.workerNodes.reduce(
         (accumulator, workerNode) =>
     while (
       this.workerNodes.reduce(
         (accumulator, workerNode) =>
@@ -962,6 +968,7 @@ export abstract class AbstractPool<
     ) {
       this.createAndSetupWorkerNode()
     }
     ) {
       this.createAndSetupWorkerNode()
     }
+    this.startingMinimumNumberOfWorkers = false
   }
 
   /** @inheritdoc */
   }
 
   /** @inheritdoc */
@@ -1251,7 +1258,7 @@ export abstract class AbstractPool<
       ) {
         if (workerNode.info.dynamic) {
           this.createAndSetupDynamicWorkerNode()
       ) {
         if (workerNode.info.dynamic) {
           this.createAndSetupDynamicWorkerNode()
-        } else {
+        } else if (!this.startingMinimumNumberOfWorkers) {
           this.startMinimumNumberOfWorkers()
         }
       }
           this.startMinimumNumberOfWorkers()
         }
       }
@@ -1273,7 +1280,11 @@ export abstract class AbstractPool<
     )
     workerNode.registerOnceWorkerEventHandler('exit', () => {
       this.removeWorkerNode(workerNode)
     )
     workerNode.registerOnceWorkerEventHandler('exit', () => {
       this.removeWorkerNode(workerNode)
-      if (this.started && !this.destroying) {
+      if (
+        this.started &&
+        !this.startingMinimumNumberOfWorkers &&
+        !this.destroying
+      ) {
         this.startMinimumNumberOfWorkers()
       }
     })
         this.startMinimumNumberOfWorkers()
       }
     })