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,