From c0199c43cf99aba17a6fbb0ffa48ab2ddc19dbf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A4=A7=E7=8C=AB?= <16399091+binaricat@users.noreply.github.com> Date: Thu, 12 Feb 2026 22:16:42 +0800 Subject: [PATCH] fix: prevent zombie processes and improve window recovery on restart (#201) - Destroy trayPanelWindow and clear refresh timer during cleanup, preventing hidden BrowserWindows from keeping the Electron process alive - Add SIGTERM/SIGINT handlers for graceful shutdown - Detect crashed webContents in focusMainWindow() and recreate the window instead of silently failing on second-instance activation Closes the issue where restarting the app shows "Failed to load the UI" and leaves multiple zombie processes in the task manager. Co-authored-by: Claude Opus 4.6 --- electron/bridges/globalShortcutBridge.cjs | 14 +++++++++++++ electron/main.cjs | 25 ++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/electron/bridges/globalShortcutBridge.cjs b/electron/bridges/globalShortcutBridge.cjs index ec735e804..c3a47e676 100644 --- a/electron/bridges/globalShortcutBridge.cjs +++ b/electron/bridges/globalShortcutBridge.cjs @@ -707,6 +707,20 @@ function registerHandlers(ipcMain) { function cleanup() { unregisterGlobalHotkey(); destroyTray(); + + if (trayPanelRefreshTimer) { + clearInterval(trayPanelRefreshTimer); + trayPanelRefreshTimer = null; + } + + if (trayPanelWindow && !trayPanelWindow.isDestroyed()) { + try { + trayPanelWindow.destroy(); + } catch { + // ignore + } + trayPanelWindow = null; + } } module.exports = { diff --git a/electron/main.cjs b/electron/main.cjs index 269ac556f..f6f5cb4d4 100644 --- a/electron/main.cjs +++ b/electron/main.cjs @@ -241,6 +241,15 @@ function focusMainWindow() { const win = wins && wins.length ? wins[0] : null; if (!win) return false; + // Check if the webContents has crashed or been destroyed + try { + if (win.webContents?.isCrashed?.()) { + console.warn('[Main] Main window webContents has crashed, destroying window'); + win.destroy(); + return false; + } + } catch {} + try { if (win.isMinimized && win.isMinimized()) win.restore(); } catch {} @@ -693,7 +702,13 @@ if (!gotLock) { app.quit(); } else { app.on("second-instance", () => { - focusMainWindow(); + if (!focusMainWindow()) { + // Window is missing or crashed — try to recreate it + void createWindow().catch((err) => { + console.error("[Main] Failed to recreate window on second-instance:", err); + showStartupError(err); + }); + } }); // Application lifecycle @@ -780,6 +795,14 @@ if (!gotLock) { }); } +// Graceful shutdown on SIGTERM/SIGINT to prevent zombie processes +for (const sig of ['SIGTERM', 'SIGINT']) { + process.on(sig, () => { + console.log(`[Main] Received ${sig}, quitting…`); + app.quit(); + }); +} + // Export for testing module.exports = { sessions,