From fa548cda5120ac0708d82f37cd0ce1260d7f96c1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 13 Dec 2023 11:27:30 +0100 Subject: [PATCH] fix: fix possible null exception with worker_threads pools MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 4 ++++ src/pools/thread/fixed.ts | 25 +++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddfd15e0..3ba761e3 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 + +- Fix possible null exception with worker_threads pools. + ## [3.0.13] - 2023-12-12 ### Fixed diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 73740baa..28f8fceb 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -81,9 +81,7 @@ export class FixedThreadPool< message: MessageValue, transferList?: TransferListItem[] ): void { - ( - this.workerNodes[workerNodeKey]?.messageChannel as MessageChannel - )?.port1?.postMessage( + this.workerNodes[workerNodeKey].messageChannel?.port1?.postMessage( { ...message, workerId: this.getWorkerInfo(workerNodeKey).id }, transferList ) @@ -109,9 +107,10 @@ export class FixedThreadPool< workerNodeKey: number, listener: (message: MessageValue) => void ): void { - ( - this.workerNodes[workerNodeKey]?.messageChannel as MessageChannel - ).port1.on('message', listener) + this.workerNodes[workerNodeKey].messageChannel?.port1?.on( + 'message', + listener + ) } /** @inheritDoc */ @@ -119,9 +118,10 @@ export class FixedThreadPool< workerNodeKey: number, listener: (message: MessageValue) => void ): void { - ( - this.workerNodes[workerNodeKey]?.messageChannel as MessageChannel - ).port1.once('message', listener) + this.workerNodes[workerNodeKey].messageChannel?.port1?.once( + 'message', + listener + ) } /** @inheritDoc */ @@ -129,9 +129,10 @@ export class FixedThreadPool< workerNodeKey: number, listener: (message: MessageValue) => void ): void { - ( - this.workerNodes[workerNodeKey]?.messageChannel as MessageChannel - ).port1.off('message', listener) + this.workerNodes[workerNodeKey].messageChannel?.port1?.off( + 'message', + listener + ) } /** @inheritDoc */ -- 2.34.1