From c2fdbe0833aafda2fe1825407e454a5a9f344f5b Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 24 Nov 2026 19:18:49 -0800 Subject: [PATCH] fix(desktop): skip Electron's visibility listener on a destroyed window --- apps/desktop/src/main/index.ts | 4 ++-- apps/desktop/src/main/window.test.ts | 25 ++++++++++++++++++------- apps/desktop/src/main/window.ts | 26 +++++++++++++++++--------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts index 2812b3ad3b2..eb9e22470fb 100644 --- a/apps/desktop/src/main/index.ts +++ b/apps/desktop/src/main/index.ts @@ -120,7 +120,7 @@ import { checkForUpdatesInteractive, initUpdater, type UpdaterHandle } from '@/m import { installBrowserUserAgent } from '@/main/user-agent' import { createMainWindow, - detachVisibilityListenersOnClose, + guardVisibilityListenersAfterDestroy, setupPermissionHandlers, } from '@/main/window' import { attachWindowOpenPolicy, isPopupContents, type WindowPolicyDeps } from '@/main/windows' @@ -322,7 +322,7 @@ function main(): void { return getWindows().at(-1) ?? null } installMainProcessFailureObservers({ events, getWindow: getMainWindow }) - app.on('browser-window-created', (_event, win) => detachVisibilityListenersOnClose(win)) + app.on('browser-window-created', (_event, win) => guardVisibilityListenersAfterDestroy(win)) const windowForContents = (contents: WebContents) => { const win = BrowserWindow.fromWebContents(contents) return win && windows.has(win) && !win.isDestroyed() ? win : null diff --git a/apps/desktop/src/main/window.test.ts b/apps/desktop/src/main/window.test.ts index fe4d9c88d06..9472fef0ba5 100644 --- a/apps/desktop/src/main/window.test.ts +++ b/apps/desktop/src/main/window.test.ts @@ -10,7 +10,7 @@ import type { ConfigStore } from '@/main/config' import type { EventRecorder } from '@/main/observability' import { createMainWindow, - detachVisibilityListenersOnClose, + guardVisibilityListenersAfterDestroy, resolvePermission, setupPermissionHandlers, } from '@/main/window' @@ -202,32 +202,43 @@ describe('createMainWindow', () => { }) }) -describe('detachVisibilityListenersOnClose', () => { +describe('guardVisibilityListenersAfterDestroy', () => { /** A window wired the way Electron wires every BrowserWindow: its visibility listener reads the native window. */ function electronLikeWindow() { let destroyed = false + const visibilityChecks = vi.fn() const win = Object.assign(new EventEmitter(), { + isDestroyed: () => destroyed, isVisible: () => { if (destroyed) throw new TypeError('Object has been destroyed') + visibilityChecks() return true }, destroy: () => { destroyed = true - win.emit('closed') }, }) const visibilityChanged = () => win.isVisible() for (const event of ['show', 'hide', 'minimize', 'maximize', 'restore']) { win.on(event, visibilityChanged) } - return win + return { win, visibilityChecks } } - it('a visibility event delivered after the window closed no longer throws', () => { - const win = electronLikeWindow() - detachVisibilityListenersOnClose(win as never) + it('keeps tracking visibility while the window lives', () => { + const { win, visibilityChecks } = electronLikeWindow() + guardVisibilityListenersAfterDestroy(win as never) win.emit('show') + win.emit('minimize') + + expect(visibilityChecks).toHaveBeenCalledTimes(2) + }) + + it('a visibility event delivered after the window was destroyed no longer throws', () => { + const { win } = electronLikeWindow() + guardVisibilityListenersAfterDestroy(win as never) + win.destroy() expect(() => win.emit('show')).not.toThrow() diff --git a/apps/desktop/src/main/window.ts b/apps/desktop/src/main/window.ts index 6f0de4f05a7..9e2b7ae713c 100644 --- a/apps/desktop/src/main/window.ts +++ b/apps/desktop/src/main/window.ts @@ -23,16 +23,24 @@ const ROUTE_SAVE_DELAY_MS = 500 const VISIBILITY_EVENTS = ['show', 'hide', 'minimize', 'maximize', 'restore'] as const /** - * Drops a closed window's visibility listeners. macOS can deliver a window's - * `show` (an occlusion change) after the window has closed and been destroyed, - * as when the app quits just as its first window appears; Electron's own - * listener then reads `isVisible()` from the dead window and throws "Object has - * been destroyed" as an uncaught main-process exception during shutdown. + * Makes Electron's own per-window visibility listener skip a destroyed window. + * macOS can deliver a window's `show` (an occlusion change) after the window + * has been destroyed, as when the app quits just as its first window appears; + * that listener then reads `isVisible()` from the dead window and throws + * "Object has been destroyed" as an uncaught main-process exception during + * shutdown. Call it from `browser-window-created`, which Electron emits right + * after subscribing that listener, so it is the only one on these events. */ -export function detachVisibilityListenersOnClose(win: BrowserWindow): void { - win.once('closed', () => { - for (const event of VISIBILITY_EVENTS) win.removeAllListeners(event) - }) +export function guardVisibilityListenersAfterDestroy(win: BrowserWindow): void { + const emitter: NodeJS.EventEmitter = win + for (const event of VISIBILITY_EVENTS) { + for (const listener of emitter.listeners(event) as ((...args: unknown[]) => void)[]) { + emitter.removeListener(event, listener) + emitter.on(event, (...args: unknown[]) => { + if (!win.isDestroyed()) listener.apply(win, args) + }) + } + } } /**