From fe1a1f142e8ae5ea075c0822f2a810a437e3b16d Mon Sep 17 00:00:00 2001 From: upendrasingh Date: Sun, 22 Mar 2026 22:49:20 +0530 Subject: [PATCH] fix: add retry limit and signal handlers to prevent Electron hang Add maxRetries parameter to waitForServer (default 60 attempts = 30s) to prevent infinite hang when Nitro server fails to start. The app now gracefully quits with an error message instead of hanging forever. Also add SIGTERM and SIGINT handlers for clean shutdown, ensuring the server process is properly killed when receiving termination signals. Fixes #312 Co-Authored-By: Claude Sonnet 4.5 --- electron/main.cjs | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/electron/main.cjs b/electron/main.cjs index 6fd09bb..1927a6a 100644 --- a/electron/main.cjs +++ b/electron/main.cjs @@ -28,12 +28,20 @@ if (!gotLock) { } // Wait until server is ready -function waitForServer(url) { - return new Promise((resolve) => { +function waitForServer(url, maxRetries = 60) { + return new Promise((resolve, reject) => { + let attempts = 0; const check = () => { + attempts++; http .get(url, () => resolve()) - .on('error', () => setTimeout(check, 500)); + .on('error', () => { + if (attempts >= maxRetries) { + reject(new Error(`Server failed to start after ${maxRetries} attempts (${maxRetries * 0.5}s)`)); + } else { + setTimeout(check, 500); + } + }); }; check(); }); @@ -41,7 +49,7 @@ function waitForServer(url) { // Start Nitro server (production) function startServer() { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const serverPath = path.join( process.resourcesPath, 'app.asar.unpacked', @@ -62,7 +70,9 @@ function startServer() { }, }); - waitForServer(`http://localhost:${serverPort}`).then(resolve); + waitForServer(`http://localhost:${serverPort}`) + .then(resolve) + .catch(reject); }); } @@ -91,12 +101,30 @@ function createWindow() { // App start app.whenReady().then(async () => { - await startServer(); - createWindow(); + try { + await startServer(); + createWindow(); + } catch (error) { + console.error('Failed to start server:', error.message); + app.quit(); + } }); // Cleanup app.on('window-all-closed', () => { if (serverProcess) serverProcess.kill(); if (process.platform !== 'darwin') app.quit(); +}); + +// Handle process termination signals for clean shutdown +process.on('SIGTERM', () => { + console.log('Received SIGTERM, shutting down gracefully'); + if (serverProcess) serverProcess.kill(); + app.quit(); +}); + +process.on('SIGINT', () => { + console.log('Received SIGINT, shutting down gracefully'); + if (serverProcess) serverProcess.kill(); + app.quit(); }); \ No newline at end of file