Skip to content
Merged
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
4 changes: 2 additions & 2 deletions apps/desktop/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions apps/desktop/src/main/window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
Expand Down
26 changes: 17 additions & 9 deletions apps/desktop/src/main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
}

/**
Expand Down
Loading