From 47f6e3148f6e0e84d4e08603e8f5dab1a7d39612 Mon Sep 17 00:00:00 2001 From: Oskar Nyberg Date: Wed, 23 Nov 2022 16:54:47 +0100 Subject: [PATCH] Fix race condition when updating IPC target when recreating window --- gui/src/main/ipc-event-channel.ts | 4 ++++ gui/src/main/user-interface.ts | 19 +++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/gui/src/main/ipc-event-channel.ts b/gui/src/main/ipc-event-channel.ts index 80fed8a034b0..a42b2bc1f163 100644 --- a/gui/src/main/ipc-event-channel.ts +++ b/gui/src/main/ipc-event-channel.ts @@ -10,3 +10,7 @@ export let IpcMainEventChannel = createIpcMain(ipcSchema, ipcMain, undefined); export function changeIpcWebContents(webContents: WebContents | undefined) { IpcMainEventChannel = createIpcMain(ipcSchema, ipcMain, webContents); } + +export function unsetIpcWebContents() { + changeIpcWebContents(undefined); +} diff --git a/gui/src/main/user-interface.ts b/gui/src/main/user-interface.ts index 6cb985b84528..2c1ac03f274b 100644 --- a/gui/src/main/user-interface.ts +++ b/gui/src/main/user-interface.ts @@ -10,7 +10,11 @@ import log from '../shared/logging'; import { Scheduler } from '../shared/scheduler'; import { SHOULD_DISABLE_DEVTOOLS_OPEN, SHOULD_FORWARD_RENDERER_LOG } from './command-line-options'; import { DaemonRpc } from './daemon-rpc'; -import { changeIpcWebContents, IpcMainEventChannel } from './ipc-event-channel'; +import { + changeIpcWebContents, + IpcMainEventChannel, + unsetIpcWebContents, +} from './ipc-event-channel'; import { WebContentsConsoleInput } from './logging'; import { isMacOs11OrNewer } from './platform-version'; import TrayIconController, { TrayIconType } from './tray-icon-controller'; @@ -51,11 +55,6 @@ export default class UserInterface implements WindowControllerDelegate { ) { const window = this.createWindow(); - changeIpcWebContents(window.webContents); - window.webContents.on('destroyed', () => { - changeIpcWebContents(undefined); - }); - this.windowController = this.createWindowController(window); this.tray = this.createTray(); } @@ -88,6 +87,10 @@ export default class UserInterface implements WindowControllerDelegate { const window = this.windowController.window; + // Make sure the IPC wrapper always has the latest webcontents if any + window.webContents.on('destroyed', unsetIpcWebContents); + changeIpcWebContents(window.webContents); + this.registerWindowListener(); this.addContextMenu(); @@ -149,6 +152,10 @@ export default class UserInterface implements WindowControllerDelegate { public async recreateWindow(isLoggedIn: boolean, tunnelState: TunnelState): Promise { if (this.tray) { this.tray.removeAllListeners(); + // Prevent the IPC webcontents reference to be reset when replacing window. Resetting wouldn't + // work since the old webContents is destroyed after the IPC wrapper has been updated with the + // new one. + this.windowController.webContents?.removeListener('destroyed', unsetIpcWebContents); const window = this.createWindow(); changeIpcWebContents(window.webContents);