diff --git a/app/main.ts b/app/main.ts index 30574193c87..346d346f4e2 100644 --- a/app/main.ts +++ b/app/main.ts @@ -670,10 +670,20 @@ async function getTitleBarOverlay(): Promise { } async function safeLoadURL(window: BrowserWindow, url: string): Promise { + let wasDestroyed = false; + const onDestroyed = () => { + wasDestroyed = true; + }; + + window.webContents.on('did-stop-loading', onDestroyed); + window.webContents.on('destroyed', onDestroyed); try { await window.loadURL(url); } catch (error) { - if (windowState.readyForShutdown() && error?.code === 'ERR_FAILED') { + if ( + (wasDestroyed || windowState.readyForShutdown()) && + error?.code === 'ERR_FAILED' + ) { getLogger().warn( 'safeLoadURL: ignoring ERR_FAILED because we are shutting down', error @@ -681,6 +691,9 @@ async function safeLoadURL(window: BrowserWindow, url: string): Promise { return; } throw error; + } finally { + window.webContents.removeListener('did-stop-loading', onDestroyed); + window.webContents.removeListener('destroyed', onDestroyed); } }