From: Jérôme Benoit Date: Mon, 16 Oct 2023 09:35:58 +0000 (+0200) Subject: fix: workaround possible race condition at work nodes array element removal and querying X-Git-Tag: v3.0.1~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=dbfa7948a27653450e1eb30f4fd2127c16058101;p=poolifier.git fix: workaround possible race condition at work nodes array element removal and querying See https://github.com/poolifier/poolifier/issues/1468 Signed-off-by: Jérôme Benoit --- 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]