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 <noreply@anthropic.com>
This commit is contained in:
陈大猫andClaude Opus 4.6 authored and GitHub committed 2026-02-12 22:16:42 +08:00
1 parent 7940b9a0a7
commit c0199c43cf
2 files changed
+38 -1

No files matched your search

+14
View File
@@ -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 = {
+24 -1
View File
@@ -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,