From 54500a4281f97e434940dc4a27ce5352b6796641 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Thu, 17 Sep 2026 20:21:45 -0700 Subject: [PATCH] Release hang watchdog quit listener on shutdown (#20910) Co-authored-by: m4air Co-authored-by: m4air --- src/main/hang-watchdog/main-thread-hang-watchdog.test.ts | 6 +++++- src/main/hang-watchdog/main-thread-hang-watchdog.ts | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/hang-watchdog/main-thread-hang-watchdog.test.ts b/src/main/hang-watchdog/main-thread-hang-watchdog.test.ts index eaaea54225b..cd3811b19c4 100644 --- a/src/main/hang-watchdog/main-thread-hang-watchdog.test.ts +++ b/src/main/hang-watchdog/main-thread-hang-watchdog.test.ts @@ -10,7 +10,8 @@ const { workerState, appMock } = vi.hoisted(() => ({ appMock: { isPackaged: true, getAppPath: vi.fn(() => '/apps/orca/app.asar'), - on: vi.fn() + on: vi.fn(), + off: vi.fn() } })) @@ -58,6 +59,7 @@ describe('installMainThreadHangWatchdog', () => { workerState.instance = null workerState.error = null appMock.on.mockReset() + appMock.off.mockReset() appMock.isPackaged = true delete process.env.ORCA_HANG_WATCHDOG_FORCE delete process.env.ORCA_HANG_WATCHDOG_TIMEOUT_MS @@ -137,6 +139,7 @@ describe('installMainThreadHangWatchdog', () => { handle?.stop() expect(worker.postMessage.mock.calls.some(([m]) => m.type === 'shutdown')).toBe(true) + expect(appMock.off).toHaveBeenCalledWith('will-quit', expect.any(Function)) handle?.stop() const shutdowns = worker.postMessage.mock.calls.filter(([m]) => m.type === 'shutdown') @@ -173,6 +176,7 @@ describe('installMainThreadHangWatchdog', () => { const exitListener = worker.once.mock.calls.find(([event]) => event === 'exit')?.[1] expect(exitListener).toEqual(expect.any(Function)) exitListener() + expect(appMock.off).toHaveBeenCalledWith('will-quit', expect.any(Function)) vi.advanceTimersByTime(6_000) expect(worker.postMessage).not.toHaveBeenCalled() }) diff --git a/src/main/hang-watchdog/main-thread-hang-watchdog.ts b/src/main/hang-watchdog/main-thread-hang-watchdog.ts index 46454429fc7..5e6155b6be7 100644 --- a/src/main/hang-watchdog/main-thread-hang-watchdog.ts +++ b/src/main/hang-watchdog/main-thread-hang-watchdog.ts @@ -73,11 +73,15 @@ export function installMainThreadHangWatchdog(options: { return } stopped = true + // Drop the app-level callback as soon as this watchdog is retired so a + // closed worker cannot keep its closure (and worker handle) alive. + app.off('will-quit', stop) clearInterval(heartbeatTimer) postMessage({ type: 'shutdown' }) } worker.once('exit', () => { stopped = true + app.off('will-quit', stop) clearInterval(heartbeatTimer) }) worker.unref()