Skip to content

Commit

Permalink
webContents.id cannot be accessed after destroyed in older electron
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Oct 2, 2023
1 parent 14e2cea commit 5847942
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ async function newProtocolRenderer(): Promise<void> {
WINDOW_ID_TO_WEB_CONTENTS = WINDOW_ID_TO_WEB_CONTENTS || new Map();

for (const wc of webContents.getAllWebContents()) {
if (KNOWN_RENDERERS.has(wc.id)) {
const wcId = wc.id;
if (KNOWN_RENDERERS.has(wcId)) {
continue;
}

const windowId: string | undefined = await wc.executeJavaScript('window.__SENTRY_RENDERER_ID__');

if (windowId) {
KNOWN_RENDERERS.add(wc.id);
WINDOW_ID_TO_WEB_CONTENTS.set(windowId, wc.id);
KNOWN_RENDERERS.add(wcId);
WINDOW_ID_TO_WEB_CONTENTS.set(windowId, wcId);

wc.once('destroyed', () => {
KNOWN_RENDERERS?.delete(wc.id);
KNOWN_RENDERERS?.delete(wcId);
WINDOW_ID_TO_WEB_CONTENTS?.delete(windowId);
});
}
Expand Down Expand Up @@ -191,12 +192,13 @@ function configureProtocol(options: ElectronMainOptionsInternal): void {
*/
function configureClassic(options: ElectronMainOptionsInternal): void {
ipcMain.on(IPCChannel.RENDERER_START, ({ sender }) => {
const id = sender.id;
// Keep track of renderers that are using IPC
KNOWN_RENDERERS = KNOWN_RENDERERS || new Set();
KNOWN_RENDERERS.add(sender.id);
KNOWN_RENDERERS.add(id);

sender.once('destroyed', () => {
KNOWN_RENDERERS?.delete(sender.id);
KNOWN_RENDERERS?.delete(id);
});
});
ipcMain.on(IPCChannel.EVENT, ({ sender }, jsonEvent: string) => handleEvent(options, jsonEvent, sender));
Expand Down

0 comments on commit 5847942

Please sign in to comment.