Skip to content

Commit c2fdbe0

Browse files
committed
fix(desktop): skip Electron's visibility listener on a destroyed window
1 parent 84bfe5c commit c2fdbe0

3 files changed

Lines changed: 37 additions & 18 deletions

File tree

‎apps/desktop/src/main/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ import { checkForUpdatesInteractive, initUpdater, type UpdaterHandle } from '@/m
120120
import { installBrowserUserAgent } from '@/main/user-agent'
121121
import {
122122
createMainWindow,
123-
detachVisibilityListenersOnClose,
123+
guardVisibilityListenersAfterDestroy,
124124
setupPermissionHandlers,
125125
} from '@/main/window'
126126
import { attachWindowOpenPolicy, isPopupContents, type WindowPolicyDeps } from '@/main/windows'
@@ -322,7 +322,7 @@ function main(): void {
322322
return getWindows().at(-1) ?? null
323323
}
324324
installMainProcessFailureObservers({ events, getWindow: getMainWindow })
325-
app.on('browser-window-created', (_event, win) => detachVisibilityListenersOnClose(win))
325+
app.on('browser-window-created', (_event, win) => guardVisibilityListenersAfterDestroy(win))
326326
const windowForContents = (contents: WebContents) => {
327327
const win = BrowserWindow.fromWebContents(contents)
328328
return win && windows.has(win) && !win.isDestroyed() ? win : null

‎apps/desktop/src/main/window.test.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { ConfigStore } from '@/main/config'
1010
import type { EventRecorder } from '@/main/observability'
1111
import {
1212
createMainWindow,
13-
detachVisibilityListenersOnClose,
13+
guardVisibilityListenersAfterDestroy,
1414
resolvePermission,
1515
setupPermissionHandlers,
1616
} from '@/main/window'
@@ -202,32 +202,43 @@ describe('createMainWindow', () => {
202202
})
203203
})
204204

205-
describe('detachVisibilityListenersOnClose', () => {
205+
describe('guardVisibilityListenersAfterDestroy', () => {
206206
/** A window wired the way Electron wires every BrowserWindow: its visibility listener reads the native window. */
207207
function electronLikeWindow() {
208208
let destroyed = false
209+
const visibilityChecks = vi.fn()
209210
const win = Object.assign(new EventEmitter(), {
211+
isDestroyed: () => destroyed,
210212
isVisible: () => {
211213
if (destroyed) throw new TypeError('Object has been destroyed')
214+
visibilityChecks()
212215
return true
213216
},
214217
destroy: () => {
215218
destroyed = true
216-
win.emit('closed')
217219
},
218220
})
219221
const visibilityChanged = () => win.isVisible()
220222
for (const event of ['show', 'hide', 'minimize', 'maximize', 'restore']) {
221223
win.on(event, visibilityChanged)
222224
}
223-
return win
225+
return { win, visibilityChecks }
224226
}
225227

226-
it('a visibility event delivered after the window closed no longer throws', () => {
227-
const win = electronLikeWindow()
228-
detachVisibilityListenersOnClose(win as never)
228+
it('keeps tracking visibility while the window lives', () => {
229+
const { win, visibilityChecks } = electronLikeWindow()
230+
guardVisibilityListenersAfterDestroy(win as never)
229231

230232
win.emit('show')
233+
win.emit('minimize')
234+
235+
expect(visibilityChecks).toHaveBeenCalledTimes(2)
236+
})
237+
238+
it('a visibility event delivered after the window was destroyed no longer throws', () => {
239+
const { win } = electronLikeWindow()
240+
guardVisibilityListenersAfterDestroy(win as never)
241+
231242
win.destroy()
232243

233244
expect(() => win.emit('show')).not.toThrow()

‎apps/desktop/src/main/window.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,24 @@ const ROUTE_SAVE_DELAY_MS = 500
2323
const VISIBILITY_EVENTS = ['show', 'hide', 'minimize', 'maximize', 'restore'] as const
2424

2525
/**
26-
* Drops a closed window's visibility listeners. macOS can deliver a window's
27-
* `show` (an occlusion change) after the window has closed and been destroyed,
28-
* as when the app quits just as its first window appears; Electron's own
29-
* listener then reads `isVisible()` from the dead window and throws "Object has
30-
* been destroyed" as an uncaught main-process exception during shutdown.
26+
* Makes Electron's own per-window visibility listener skip a destroyed window.
27+
* macOS can deliver a window's `show` (an occlusion change) after the window
28+
* has been destroyed, as when the app quits just as its first window appears;
29+
* that listener then reads `isVisible()` from the dead window and throws
30+
* "Object has been destroyed" as an uncaught main-process exception during
31+
* shutdown. Call it from `browser-window-created`, which Electron emits right
32+
* after subscribing that listener, so it is the only one on these events.
3133
*/
32-
export function detachVisibilityListenersOnClose(win: BrowserWindow): void {
33-
win.once('closed', () => {
34-
for (const event of VISIBILITY_EVENTS) win.removeAllListeners(event)
35-
})
34+
export function guardVisibilityListenersAfterDestroy(win: BrowserWindow): void {
35+
const emitter: NodeJS.EventEmitter = win
36+
for (const event of VISIBILITY_EVENTS) {
37+
for (const listener of emitter.listeners(event) as ((...args: unknown[]) => void)[]) {
38+
emitter.removeListener(event, listener)
39+
emitter.on(event, (...args: unknown[]) => {
40+
if (!win.isDestroyed()) listener.apply(win, args)
41+
})
42+
}
43+
}
3644
}
3745

3846
/**

0 commit comments

Comments
 (0)