fix: ensure pool workers are properly initialized
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 16 Aug 2023 16:49:31 +0000 (18:49 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 16 Aug 2023 16:49:31 +0000 (18:49 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
src/pools/abstract-pool.ts
src/worker/cluster-worker.ts
src/worker/thread-worker.ts

index 6ac71e7c3013be7b954c20b9aab6e46a88f41cf6..5344323cbbfc0582ab193011724baa1833a4bf79 100644 (file)
@@ -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.
index 18d4fb8362dcfdd9de125e0e83ca51aa91b98ff3..6d71e87e34378abe5c75ad1c1806eacaad979a3b 100644 (file)
@@ -1125,6 +1125,9 @@ export abstract class AbstractPool<
   }
 
   private handleWorkerReadyResponse (message: MessageValue<Response>): 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
index b22c50fe4160a17ac83b1a525df8090ac1ae9538..e5bcb72781680fdcd9c0a3a505ae587947e35609 100644 (file)
@@ -43,10 +43,14 @@ export class ClusterWorker<
 
   /** @inheritDoc */
   protected handleReadyMessage (message: MessageValue<Data>): 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 })
+      }
     }
   }
 
index 69940f960600afe815cbe540d33f5547e242ed19..6f36664b8ae1a56cebb5058d7d4c56885b5ae88f 100644 (file)
@@ -54,13 +54,17 @@ export class ThreadWorker<
   protected handleReadyMessage (message: MessageValue<Data>): 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 })
+      }
     }
   }