From 984dc9c8adc66d4ed982e76391db751dd685f0b2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 20 Jul 2023 17:47:52 +0200 Subject: [PATCH] fix: unref() message port at worker exit MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/worker-node.ts | 2 ++ src/worker/abstract-worker.ts | 16 +++++++++++++--- src/worker/thread-worker.ts | 7 +++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index fcc09137..3b69d385 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -77,6 +77,8 @@ implements IWorkerNode { /** @inheritdoc */ public closeChannel (): void { if (this.info.messageChannel != null) { + this.info.messageChannel?.port1.unref() + this.info.messageChannel?.port2.unref() this.info.messageChannel?.port1.close() this.info.messageChannel?.port2.close() delete this.info.messageChannel diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 67b4b5ff..0f1d442a 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -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): 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() } /** diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 98d58b5d..4d4c1c34 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -66,6 +66,13 @@ export class ThreadWorker< } } + /** @inheritDoc */ + protected handleKillMessage (message: MessageValue): void { + super.handleKillMessage(message) + this.port?.unref() + this.port?.close() + } + /** @inheritDoc */ protected get id (): number { return threadId -- 2.34.1