Harden Electron main: re-entrancy guard, tray destroy, loadURL error log

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-26 09:49:20 -07:00
co-authored by Claude Sonnet 4.6
parent 7ea303a2ef
commit affc71bd0f
+14 -8
View File
@@ -10,6 +10,7 @@ const YT_MUSIC_URL = 'https://music.youtube.com';
let serverHandle = null; let serverHandle = null;
let mainWindow = null; let mainWindow = null;
let tray = null; let tray = null;
let isQuitting = false;
async function createWindow() { async function createWindow() {
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
@@ -29,7 +30,11 @@ async function createWindow() {
mainWindow = null; mainWindow = null;
}); });
await mainWindow.loadURL(YT_MUSIC_URL); try {
await mainWindow.loadURL(YT_MUSIC_URL);
} catch (err) {
console.error('[wtm] Failed to load YT Music:', err.message);
}
} }
app.whenReady().then(async () => { app.whenReady().then(async () => {
@@ -47,7 +52,6 @@ app.whenReady().then(async () => {
onShow: () => { onShow: () => {
if (!mainWindow) return; if (!mainWindow) return;
if (mainWindow.isMinimized()) mainWindow.restore(); if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.show();
mainWindow.focus(); mainWindow.focus();
}, },
onQuit: () => app.quit(), onQuit: () => app.quit(),
@@ -59,10 +63,12 @@ app.on('window-all-closed', () => {
}); });
app.on('before-quit', async (e) => { app.on('before-quit', async (e) => {
if (serverHandle) { if (isQuitting) return;
e.preventDefault?.(); if (!serverHandle) return;
try { await serverHandle.stop(); } catch (_) {} e.preventDefault();
serverHandle = null; isQuitting = true;
app.exit(0); if (tray) { tray.destroy(); tray = null; }
} try { await serverHandle.stop(); } catch (_) {}
serverHandle = null;
app.exit(0);
}); });