From 2286d70758042614efa6d68b548a6d622abc6198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Thu, 16 Apr 2026 23:08:37 -0400 Subject: [PATCH] fix(worker-service): cancel kill-timer on worker exit; log shutdown failure --- src/main/worker-service.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/worker-service.ts b/src/main/worker-service.ts index e54a2aeb..ef6d2fb2 100644 --- a/src/main/worker-service.ts +++ b/src/main/worker-service.ts @@ -100,13 +100,20 @@ export function createWorkerService({ // because the worker died before it could forward SIGTERM to its children. try { worker.postMessage({ type: "shutdown" }); - } catch { /* worker may already be dead */ } + } catch (error) { + logger.debug( + `[worker-service] shutdown postMessage failed: ${error instanceof Error ? error.message : String(error)}`, + ); + } - // Safety net: force-kill worker after 2s if shutdown didn't complete + // Safety net: force-kill worker after 2s if shutdown didn't complete. + // Track the timer so we can cancel it the moment the worker cleanly + // exits — otherwise the pending timer keeps the event loop alive. const ref = worker; - setTimeout(() => { + const killTimer = setTimeout(() => { try { ref.kill(); } catch { /* already dead */ } }, 2000); + ref.once("exit", () => clearTimeout(killTimer)); worker = null; }