fix: unref() message port at worker exit
[poolifier.git] / src / worker / abstract-worker.ts
index 67b4b5ff9e4575ead2d824537fb106d6d0b5ba11..0f1d442a6f242ef2a25068f8e212f2fbff686a37 100644 (file)
@@ -311,12 +311,21 @@ export abstract class AbstractWorker<
         this.run(message)
       } else if (message.kill === true) {
         // Kill message received
-        !this.isMain && this.stopCheckActive()
-        this.emitDestroy()
+        this.handleKillMessage(message)
       }
     }
   }
 
+  /**
+   * Handles a kill message sent by the main worker.
+   *
+   * @param message - The kill message.
+   */
+  protected handleKillMessage (message: MessageValue<Data>): void {
+    !this.isMain && this.stopCheckActive()
+    this.emitDestroy()
+  }
+
   /**
    * Starts the worker check active interval.
    */
@@ -325,7 +334,8 @@ export abstract class AbstractWorker<
     this.activeInterval = setInterval(
       this.checkActive.bind(this),
       (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
-    ).unref()
+    )
+    this.activeInterval.unref()
   }
 
   /**