]> Piment Noir Git Repositories - poolifier.git/commitdiff
refactor: make pool destroy() more robust
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 22 Aug 2025 16:42:38 +0000 (18:42 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 22 Aug 2025 16:42:38 +0000 (18:42 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts

index d074b72edf358ee96a8b96c1e11f30f9ca3aeef4..cfd919ccf10ca079226d5b452c2a326d76bb27f4 100644 (file)
@@ -617,20 +617,32 @@ export abstract class AbstractPool<
       throw new Error('Cannot destroy an already destroying pool')
     }
     this.destroying = true
-    await Promise.all(
-      this.workerNodes.map(async (_, workerNodeKey) => {
-        await this.destroyWorkerNode(workerNodeKey)
-      })
-    )
-    if (this.emitter != null) {
-      this.emitter.listenerCount(PoolEvents.destroy) > 0 &&
-        this.emitter.emit(PoolEvents.destroy, this.info)
-      this.emitter.emitDestroy()
-      this.readyEventEmitted = false
+    try {
+      await Promise.allSettled(
+        this.workerNodes.map(async (_, workerNodeKey) => {
+          try {
+            await this.destroyWorkerNode(workerNodeKey)
+          } catch (error) {
+            if (
+              this.emitter != null &&
+              this.emitter.listenerCount(PoolEvents.error) > 0
+            ) {
+              this.emitter.emit(PoolEvents.error, error)
+            }
+          }
+        })
+      )
+    } finally {
+      if (this.emitter != null) {
+        this.emitter.listenerCount(PoolEvents.destroy) > 0 &&
+          this.emitter.emit(PoolEvents.destroy, this.info)
+        this.emitter.emitDestroy()
+        this.readyEventEmitted = false
+      }
+      delete this.startTimestamp
+      this.destroying = false
+      this.started = false
     }
-    delete this.startTimestamp
-    this.destroying = false
-    this.started = false
   }
 
   /** @inheritDoc */