From 3437d113d160d7f00dff9e7d65164c97d15c4c7d Mon Sep 17 00:00:00 2001 From: Satya Patel Date: Thu, 27 Aug 2026 11:29:11 -0700 Subject: [PATCH] fix(desktop): don't throw persisting windows when quit precedes app-state init Quitting before initAppState() completes made persistOpenWindows() hit the appState proxy's not-initialized throw via pruneWindowScopedState(), which escaped the async before-quit handler and skipped runQuitCleanup entirely. Guard persistOpenWindows() on isAppStateInitialized(): pre-init there are no windows to snapshot, and writing the empty set would clobber the previous session's restore state. Refs DESKTOP-13X Claude-Session: https://claude.ai/code/session_01MBhi2zapr6fFn3NLgfxhjA --- apps/desktop/src/main/lib/app-state/index.ts | 4 ++++ apps/desktop/src/main/windows/main.ts | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/main/lib/app-state/index.ts b/apps/desktop/src/main/lib/app-state/index.ts index 8c24b6d80e9..b1073216fd6 100644 --- a/apps/desktop/src/main/lib/app-state/index.ts +++ b/apps/desktop/src/main/lib/app-state/index.ts @@ -94,6 +94,10 @@ export async function initAppState(): Promise { console.log(`App state initialized at: ${APP_STATE_PATH}`); } +export function isAppStateInitialized(): boolean { + return _appState !== null; +} + export const appState = new Proxy({} as AppStateDB, { get(_target, prop) { if (!_appState) { diff --git a/apps/desktop/src/main/windows/main.ts b/apps/desktop/src/main/windows/main.ts index bf90d288e0d..ba138a70d2e 100644 --- a/apps/desktop/src/main/windows/main.ts +++ b/apps/desktop/src/main/windows/main.ts @@ -17,7 +17,11 @@ import { env } from "shared/env.shared"; import type { AgentLifecycleEvent } from "shared/notification-types"; import { createIPCHandler } from "trpc-electron/main"; import { productName } from "~/package.json"; -import { appState, pruneWindowScopedState } from "../lib/app-state"; +import { + appState, + isAppStateInitialized, + pruneWindowScopedState, +} from "../lib/app-state"; import { browserManager } from "../lib/browser/browser-manager"; import { attachEditContextMenu } from "../lib/edit-context-menu"; import { createApplicationMenu } from "../lib/menu"; @@ -298,6 +302,10 @@ function snapshotWindowState(window: BrowserWindow): WindowState { /** Persist every open window's bounds + org so they can be restored on relaunch. */ export function persistOpenWindows(): void { + // Quit before initAppState() completed: windows are only created after init, + // so there is nothing to snapshot — appState access below would throw, and + // writing the empty set would clobber the previous session's restore state. + if (!isAppStateInitialized()) return; const persisted: PersistedWindow[] = getAllWindows() .filter((w) => !w.isDestroyed()) .map((w) => ({