Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/actions/setupIPCForwarding.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { ipcMain } from "electron";
// Sentry for main process
const Sentry = require("@sentry/electron/main");

export const setupIPCForwardingToBackground = (backgroundWindow) => {
ipcMain.handle(
"forward-event-from-webapp-to-background-and-await-reply",
async (event, incomingData) => {
return new Promise((resolve) => {
// Check if backgroundWindow is available and not destroyed (check on each IPC call)
if (!backgroundWindow || backgroundWindow.isDestroyed()) {
const errorMessage = "Background process unavailable during IPC communication";
console.error(errorMessage);
Sentry.captureException(new Error(errorMessage));
}

const { actualPayload, eventName } = incomingData;
ipcMain.once(`reply-${eventName}`, (responseEvent, responsePayload) => {
resolve(responsePayload);
Expand Down
48 changes: 30 additions & 18 deletions src/main/actions/startBackgroundProcess.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable */
import { enable as enableWebContents } from "@electron/remote/main";
import { captureException } from "@sentry/browser";
/** Babel */
require("core-js/stable");
require("regenerator-runtime/runtime");
Expand Down Expand Up @@ -27,28 +28,34 @@ const startBackgroundProcess = async () => {
return new Promise(async (resolve) => {
let backgroundWindow = await getState("backgroundWindow");
if (!backgroundWindow) {
backgroundWindow = await setState(null, {
stateName: "backgroundWindow",
// Background Process Window
newValue: new BrowserWindow({
width: 800,
height: 600,
show:
process.env.NODE_ENV === "development" ||
process.env.DEBUG_PROD === "true",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
},
}),
});
enableWebContents(backgroundWindow.webContents);
try {
backgroundWindow = await setState(null, {
stateName: "backgroundWindow",
// Background Process Window
newValue: new BrowserWindow({
width: 800,
height: 600,
show:
process.env.NODE_ENV === "development" ||
process.env.DEBUG_PROD === "true",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
},
}),
});
enableWebContents(backgroundWindow.webContents);
} catch (error) {
console.error(error);
captureException(error);
resolve(false);
return;
}
} else {
logger.log(
"startBackgroundProcess: A background windows already exists. Cancelling."
);

resolve(true);
return;
}
Expand All @@ -75,6 +82,11 @@ const startBackgroundProcess = async () => {
// Set state
global.isBackgroundProcessActive = true;

backgroundWindow.on("closed", () => {
console.error("Background window closed unexpectedly");
captureException(new Error("Background window closed unexpectedly"));
})
Comment on lines +85 to +88
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Clear background window state on close to allow recovery.

The close handler reports the error but leaves global.backgroundWindow, state, and the active flag intact. That can block future restarts and keep IPC pointing at a destroyed window.

🛠️ Proposed fix
     backgroundWindow.on("closed", () => {
       console.error("Background window closed unexpectedly");
       captureException(new Error("Background window closed unexpectedly"));
+      global.isBackgroundProcessActive = false;
+      global.backgroundWindow = null;
+      void setState(null, { stateName: "backgroundWindow", newValue: null });
     })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
backgroundWindow.on("closed", () => {
console.error("Background window closed unexpectedly");
captureException(new Error("Background window closed unexpectedly"));
})
backgroundWindow.on("closed", () => {
console.error("Background window closed unexpectedly");
captureException(new Error("Background window closed unexpectedly"));
global.isBackgroundProcessActive = false;
global.backgroundWindow = null;
void setState(null, { stateName: "backgroundWindow", newValue: null });
})
🤖 Prompt for AI Agents
In `@src/main/actions/startBackgroundProcess.js` around lines 85 - 88, The
on("closed") handler for backgroundWindow currently logs and captures the error
but doesn't reset state; update that handler (backgroundWindow.on("closed",
...)) to: clear global.backgroundWindow (set to null), set any active flag such
as isBackgroundProcessActive to false, remove/clean any IPC listeners or
references tied to backgroundWindow, and ensure any local backgroundWindow
variable is nulled so future startBackgroundProcess()/restart logic can create a
new window; include a brief log entry indicating state was cleared after close.


backgroundWindow.webContents.on("did-finish-load", () => {
resolve(true);
});
Expand Down