fix: workaround possible race condition at work nodes array element removal and querying
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 16 Oct 2023 09:35:58 +0000 (11:35 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 16 Oct 2023 09:35:58 +0000 (11:35 +0200)
See https://github.com/poolifier/poolifier/issues/1468

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
CHANGELOG.md
src/pools/abstract-pool.ts
src/pools/cluster/fixed.ts
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/thread/fixed.ts

index 7c0ec1e072a76183e51892fa183f7225109df23e..c00452f502bd9b6bb01c1933a97559ac2afcf8db 100644 (file)
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Fixed
+
+- Workaround possible race condition at work nodes array element removal and querying. See [issue #1468](https://github.com/poolifier/poolifier/issues/1468).
+
 ### Changed
 
 - Switch the worker node eventing code to `EventTarget` API .
index bec4cd29056f4bf531e5f267e767795d4a1753b0..8e0a259af8ea3e6f0edad24f29dbb9b4e81821fe 100644 (file)
@@ -1601,7 +1601,7 @@ export abstract class AbstractPool<
    * @returns The worker information.
    */
   protected getWorkerInfo (workerNodeKey: number): WorkerInfo {
-    return this.workerNodes[workerNodeKey].info
+    return this.workerNodes[workerNodeKey]?.info
   }
 
   /**
index d457b37790d0537c80e17635fcbb76c4b4599b6d..68ae50050ba24d38ac1cd2b6119af626d248e9c6 100644 (file)
@@ -85,7 +85,7 @@ export class FixedClusterPool<
   ): void {
     this.workerNodes[workerNodeKey].worker.send({
       ...message,
-      workerId: this.workerNodes[workerNodeKey].info.id as number
+      workerId: this.getWorkerInfo(workerNodeKey).id as number
     })
   }
 
index 630a2258be49a7ed725f12b1ac555d510dd0e257..68e09238b204c35e4f6bb87392021f314e3e2923 100644 (file)
@@ -123,7 +123,7 @@ export abstract class AbstractWorkerChoiceStrategy<
    * @returns Whether the worker node is ready or not.
    */
   private isWorkerNodeReady (workerNodeKey: number): boolean {
-    return this.pool.workerNodes[workerNodeKey].info.ready
+    return this.pool.workerNodes[workerNodeKey]?.info.ready
   }
 
   /**
index db278485a097fcb40a1846902ee8a7b36b548c3e..7d47e81ae937abe695a87841babed4011873fc7a 100644 (file)
@@ -82,7 +82,7 @@ export class FixedThreadPool<
     (
       this.workerNodes[workerNodeKey].messageChannel as MessageChannel
     ).port1.postMessage(
-      { ...message, workerId: this.workerNodes[workerNodeKey].info.id },
+      { ...message, workerId: this.getWorkerInfo(workerNodeKey).id },
       transferList
     )
   }
@@ -95,7 +95,7 @@ export class FixedThreadPool<
     workerNode.worker.postMessage(
       {
         ready: false,
-        workerId: workerNode.info.id,
+        workerId: this.getWorkerInfo(workerNodeKey).id,
         port: port2
       },
       [port2]