From: Jérôme Benoit Date: Wed, 16 Aug 2023 16:49:31 +0000 (+0200) Subject: fix: ensure pool workers are properly initialized X-Git-Tag: v2.6.28~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f05ed93ccebb3ea4212d015788bcd6ca9e0b38d2;p=poolifier.git fix: ensure pool workers are properly initialized Signed-off-by: Jérôme Benoit --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ac71e7c..5344323c 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 + +- Ensure pool workers are properly initialized. + ### Added - HTTP server pool examples: express-cluster, express-hybrid. diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 18d4fb83..6d71e87e 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1125,6 +1125,9 @@ export abstract class AbstractPool< } private handleWorkerReadyResponse (message: MessageValue): void { + if (message.ready === false) { + throw new Error(`Worker ${message.workerId} failed to initialize`) + } this.getWorkerInfo( this.getWorkerNodeKeyByWorkerId(message.workerId) ).ready = message.ready as boolean diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index b22c50fe..e5bcb727 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -43,10 +43,14 @@ export class ClusterWorker< /** @inheritDoc */ protected handleReadyMessage (message: MessageValue): void { - if (message.workerId === this.id && message.ready != null) { - this.getMainWorker()?.on('message', this.messageListener.bind(this)) - this.sendTaskFunctionsListToMainWorker() - this.sendToMainWorker({ ready: true, workerId: this.id }) + if (message.workerId === this.id && message.ready === false) { + try { + this.getMainWorker()?.on('message', this.messageListener.bind(this)) + this.sendTaskFunctionsListToMainWorker() + this.sendToMainWorker({ ready: true, workerId: this.id }) + } catch { + this.sendToMainWorker({ ready: false, workerId: this.id }) + } } } diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 69940f96..6f36664b 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -54,13 +54,17 @@ export class ThreadWorker< protected handleReadyMessage (message: MessageValue): void { if ( message.workerId === this.id && - message.ready != null && + message.ready === false && message.port != null ) { - this.port = message.port - this.port.on('message', this.messageListener.bind(this)) - this.sendTaskFunctionsListToMainWorker() - this.sendToMainWorker({ ready: true, workerId: this.id }) + try { + this.port = message.port + this.port.on('message', this.messageListener.bind(this)) + this.sendTaskFunctionsListToMainWorker() + this.sendToMainWorker({ ready: true, workerId: this.id }) + } catch { + this.sendToMainWorker({ ready: false, workerId: this.id }) + } } }