From dbfa7948a27653450e1eb30f4fd2127c16058101 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 16 Oct 2023 11:35:58 +0200 Subject: [PATCH] fix: workaround possible race condition at work nodes array element removal and querying MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit See https://github.com/poolifier/poolifier/issues/1468 Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 4 ++++ src/pools/abstract-pool.ts | 2 +- src/pools/cluster/fixed.ts | 2 +- .../selection-strategies/abstract-worker-choice-strategy.ts | 2 +- src/pools/thread/fixed.ts | 4 ++-- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c0ec1e0..c00452f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 . diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index bec4cd29..8e0a259a 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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 } /** diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index d457b377..68ae5005 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -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 }) } diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 630a2258..68e09238 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -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 } /** diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index db278485..7d47e81a 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -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] -- 2.34.1