Skip to content
Closed
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
13 changes: 11 additions & 2 deletions .github/workflows/desktop-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
name: e2e (${{ matrix.shard }}/${{ strategy.job-total }})
if: github.event_name != 'schedule' && github.event_name != 'push'
runs-on: macos-26
timeout-minutes: 30
timeout-minutes: 60
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -163,7 +163,7 @@ jobs:

- name: Run Playwright _electron smoke suite
working-directory: apps/desktop
run: bun run ../../scripts/desktop-e2e.ts ${{ matrix.shard }}/${{ strategy.job-total }}
run: bunx playwright test e2e/terminal-cancel.spec.ts --grep "the launch after|survives a quit" --repeat-each=30 --reporter=line,json
env:
PLAYWRIGHT_JSON_OUTPUT_FILE: test-results/desktop-smoke.json

Expand All @@ -175,6 +175,15 @@ jobs:
path: apps/desktop/test-results
retention-days: 7

- name: Upload fatal trace
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: fatal-trace-${{ matrix.shard }}
path: /tmp/sim-fatal-trace.log
if-no-files-found: ignore
retention-days: 7

- name: Upload smoke coverage and timings
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
Expand Down
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
15 changes: 15 additions & 0 deletions apps/desktop/src/main/observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ export interface MainProcessFailureObserverDeps {

const FAILURE_DEDUPE_MS = 5_000

function experimentTrace(line: string): void {
try {
appendFileSync(
'/tmp/sim-fatal-trace.log',
`[fatal-trace pid=${process.pid} ${new Date().toISOString()}] ${line}\n`
)
} catch {}
}

/**
* Records native child-process failures and gives a fatal main-process error a
* single native recovery surface. Error text is deliberately excluded from
Expand Down Expand Up @@ -121,6 +130,9 @@ export function installMainProcessFailureObservers({
name: 'main_unhandled_rejection' | 'main_uncaught_exception',
value: unknown
): void => {
experimentTrace(
`fatal ${name}\n${value instanceof Error ? `${value.name}: ${value.message}\n${value.stack}` : String(value)}`
)
if (fatalRecoveryOpen) return
fatalRecoveryOpen = true
events.record(name, { valueType: value instanceof Error ? 'Error' : typeof value })
Expand Down Expand Up @@ -171,6 +183,9 @@ export function installMainProcessFailureObservers({
app.on('child-process-gone', onChildProcessGone)
app.on('before-quit', answerOpenPromptAsQuit)
app.on('will-quit', onWillQuit)
app.on('before-quit', () => experimentTrace('before-quit'))
app.on('quit', () => experimentTrace('quit'))
experimentTrace('observers installed')
processSource.on('unhandledRejection', onUnhandledRejection)
processSource.on('uncaughtException', onUncaughtException)
}
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