diff --git a/.github/workflows/desktop-e2e.yml b/.github/workflows/desktop-e2e.yml index 9999764568b..0016634530c 100644 --- a/.github/workflows/desktop-e2e.yml +++ b/.github/workflows/desktop-e2e.yml @@ -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: @@ -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 @@ -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 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/observability.ts b/apps/desktop/src/main/observability.ts index 3a60b566f48..9ca4369e773 100644 --- a/apps/desktop/src/main/observability.ts +++ b/apps/desktop/src/main/observability.ts @@ -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 @@ -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 }) @@ -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) } 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) + }) + } + } } /**