Skip to content

Commit a2ea939

Browse files
committed
fix(desktop): a window's late show after it closed no longer throws during quit
1 parent 1a7ba5c commit a2ea939

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ import { createRunLedger } from '@/main/terminal/run-ledger'
118118
import { installTray, type TrayHandle } from '@/main/tray'
119119
import { checkForUpdatesInteractive, initUpdater, type UpdaterHandle } from '@/main/updater'
120120
import { installBrowserUserAgent } from '@/main/user-agent'
121-
import { createMainWindow, setupPermissionHandlers } from '@/main/window'
121+
import {
122+
createMainWindow,
123+
detachVisibilityListenersOnClose,
124+
setupPermissionHandlers,
125+
} from '@/main/window'
122126
import { attachWindowOpenPolicy, isPopupContents, type WindowPolicyDeps } from '@/main/windows'
123127

124128
const logger = createLogger('DesktopMain')
@@ -318,6 +322,7 @@ function main(): void {
318322
return getWindows().at(-1) ?? null
319323
}
320324
installMainProcessFailureObservers({ events, getWindow: getMainWindow })
325+
app.on('browser-window-created', (_event, win) => detachVisibilityListenersOnClose(win))
321326
const windowForContents = (contents: WebContents) => {
322327
const win = BrowserWindow.fromWebContents(contents)
323328
return win && windows.has(win) && !win.isDestroyed() ? win : null

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventEmitter } from 'node:events'
12
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
23
import { createSecureWebPreferences } from '@/main/window-preferences'
34

@@ -7,7 +8,12 @@ import { BrowserWindow, dialog, screen, systemPreferences, WebContentsView } fro
78
import { registerAgentWebContents } from '@/main/browser-agent/registry'
89
import type { ConfigStore } from '@/main/config'
910
import type { EventRecorder } from '@/main/observability'
10-
import { createMainWindow, resolvePermission, setupPermissionHandlers } from '@/main/window'
11+
import {
12+
createMainWindow,
13+
detachVisibilityListenersOnClose,
14+
resolvePermission,
15+
setupPermissionHandlers,
16+
} from '@/main/window'
1117

1218
const APP = 'https://sim.ai'
1319

@@ -195,3 +201,36 @@ describe('createMainWindow', () => {
195201
expect(event.preventDefault).toHaveBeenCalledOnce()
196202
})
197203
})
204+
205+
describe('detachVisibilityListenersOnClose', () => {
206+
/** A window wired the way Electron wires every BrowserWindow: its visibility listener reads the native window. */
207+
function electronLikeWindow() {
208+
let destroyed = false
209+
const win = Object.assign(new EventEmitter(), {
210+
isVisible: () => {
211+
if (destroyed) throw new TypeError('Object has been destroyed')
212+
return true
213+
},
214+
destroy: () => {
215+
destroyed = true
216+
win.emit('closed')
217+
},
218+
})
219+
const visibilityChanged = () => win.isVisible()
220+
for (const event of ['show', 'hide', 'minimize', 'maximize', 'restore']) {
221+
win.on(event, visibilityChanged)
222+
}
223+
return win
224+
}
225+
226+
it('a visibility event delivered after the window closed no longer throws', () => {
227+
const win = electronLikeWindow()
228+
detachVisibilityListenersOnClose(win as never)
229+
230+
win.emit('show')
231+
win.destroy()
232+
233+
expect(() => win.emit('show')).not.toThrow()
234+
expect(() => win.emit('hide')).not.toThrow()
235+
})
236+
})

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ const MIN_HEIGHT = 600
1919
const WINDOW_TITLE = 'Sim'
2020
const BOUNDS_SAVE_DELAY_MS = 400
2121
const ROUTE_SAVE_DELAY_MS = 500
22+
/** The events Electron's own per-window `visibilityChanged` listener subscribes to. */
23+
const VISIBILITY_EVENTS = ['show', 'hide', 'minimize', 'maximize', 'restore'] as const
24+
25+
/**
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.
31+
*/
32+
export function detachVisibilityListenersOnClose(win: BrowserWindow): void {
33+
win.once('closed', () => {
34+
for (const event of VISIBILITY_EVENTS) win.removeAllListeners(event)
35+
})
36+
}
2237

2338
/**
2439
* The permission matrix: sanitized clipboard writes and microphone access for

0 commit comments

Comments
 (0)