From e33ab50c2ab3ddb5bd9032c0c39cb02008d5d901 Mon Sep 17 00:00:00 2001 From: rohitneharabrowserstack Date: Fri, 20 Feb 2026 17:44:19 +0530 Subject: [PATCH 1/2] Added logic for keeping background process runing on closing of debug console --- src/main/actions/cleanup.js | 23 ++++++++++- src/main/actions/startBackgroundProcess.js | 44 ++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/main/actions/cleanup.js b/src/main/actions/cleanup.js index a178d088..a91a48db 100644 --- a/src/main/actions/cleanup.js +++ b/src/main/actions/cleanup.js @@ -19,11 +19,30 @@ export const getReadyToQuitApp = async () => { cleanup(); if (global.backgroundWindow) { + // Set flag to allow background window destruction + global.allowBackgroundWindowDestruction = true; + ipcMain.once("shutdown-success", () => { - console.log("shudown sucess"); - global.backgroundWindow?.close(); + // When app is actually quitting, use the original destroy + if (global.backgroundWindow._originalDestroy) { + global.backgroundWindow._originalDestroy(); + } else { + global.backgroundWindow.destroy(); + } resolve() }); + + // Timeout fallback in case shutdown-success never comes + setTimeout(() => { + if (global.backgroundWindow && !global.backgroundWindow.isDestroyed()) { + if (global.backgroundWindow._originalDestroy) { + global.backgroundWindow._originalDestroy(); + } else { + global.backgroundWindow.destroy(); + } + } + resolve(); + }, 2000); } else { resolve(); } diff --git a/src/main/actions/startBackgroundProcess.js b/src/main/actions/startBackgroundProcess.js index 8b04d734..26662f46 100644 --- a/src/main/actions/startBackgroundProcess.js +++ b/src/main/actions/startBackgroundProcess.js @@ -69,11 +69,55 @@ const startBackgroundProcess = async () => { }) } + // Prevent closing - hide instead of destroying the window + const closeHandler = (event) => { + event.preventDefault(); + event.returnValue = false; + if (!backgroundWindow.isDestroyed()) { + backgroundWindow.hide(); + } + return false; + }; + + backgroundWindow.on("close", closeHandler); + + // Store the close handler so it can't be easily removed + backgroundWindow._preventCloseHandler = closeHandler; + + // Override the destroy method to prevent accidental destruction + const originalDestroy = backgroundWindow.destroy.bind(backgroundWindow); + backgroundWindow.destroy = () => { + // Allow destruction if we're in quit mode + if (global.allowBackgroundWindowDestruction) { + originalDestroy(); + return; + } + if (!backgroundWindow.isDestroyed()) { + backgroundWindow.hide(); + } + }; + + // Store reference to restore destroy if really needed during app quit + backgroundWindow._originalDestroy = originalDestroy; + + // Override removeAllListeners to prevent removal of close handler + const originalRemoveAllListeners = backgroundWindow.removeAllListeners.bind(backgroundWindow); + backgroundWindow.removeAllListeners = (eventName) => { + if (eventName === 'close' || eventName === undefined) { + // Re-attach the close handler after removal + originalRemoveAllListeners.call(backgroundWindow, eventName); + backgroundWindow.on("close", closeHandler); + return backgroundWindow; + } + return originalRemoveAllListeners.call(backgroundWindow, eventName); + }; + // Setup IPC forwarding setupIPCForwardingToBackground(backgroundWindow); // Set state global.isBackgroundProcessActive = true; + global.backgroundProcessStarted = true; backgroundWindow.webContents.on("did-finish-load", () => { resolve(true); From 7ff5611fbba68f7f53d1df510335b1a11cc4dd87 Mon Sep 17 00:00:00 2001 From: rohitneharabrowserstack Date: Fri, 20 Feb 2026 18:18:32 +0530 Subject: [PATCH 2/2] Addressed code rabbit comments --- src/main/actions/cleanup.js | 24 ++++++++++++++-------- src/main/actions/startBackgroundProcess.js | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/actions/cleanup.js b/src/main/actions/cleanup.js index a91a48db..c0a164bf 100644 --- a/src/main/actions/cleanup.js +++ b/src/main/actions/cleanup.js @@ -21,19 +21,27 @@ export const getReadyToQuitApp = async () => { if (global.backgroundWindow) { // Set flag to allow background window destruction global.allowBackgroundWindowDestruction = true; - + + let timeoutHandle; + ipcMain.once("shutdown-success", () => { - // When app is actually quitting, use the original destroy - if (global.backgroundWindow._originalDestroy) { - global.backgroundWindow._originalDestroy(); - } else { - global.backgroundWindow.destroy(); + clearTimeout(timeoutHandle); + if ( + global.backgroundWindow && + !global.backgroundWindow.isDestroyed() + ) { + // When app is actually quitting, use the original destroy + if (global.backgroundWindow._originalDestroy) { + global.backgroundWindow._originalDestroy(); + } else { + global.backgroundWindow.destroy(); + } } - resolve() + resolve(); }); // Timeout fallback in case shutdown-success never comes - setTimeout(() => { + timeoutHandle = setTimeout(() => { if (global.backgroundWindow && !global.backgroundWindow.isDestroyed()) { if (global.backgroundWindow._originalDestroy) { global.backgroundWindow._originalDestroy(); diff --git a/src/main/actions/startBackgroundProcess.js b/src/main/actions/startBackgroundProcess.js index 26662f46..9ca03f43 100644 --- a/src/main/actions/startBackgroundProcess.js +++ b/src/main/actions/startBackgroundProcess.js @@ -88,7 +88,7 @@ const startBackgroundProcess = async () => { const originalDestroy = backgroundWindow.destroy.bind(backgroundWindow); backgroundWindow.destroy = () => { // Allow destruction if we're in quit mode - if (global.allowBackgroundWindowDestruction) { + if (global.allowBackgroundWindowDestruction && !backgroundWindow.isDestroyed()) { originalDestroy(); return; }