From ec09e4531449826e9459f5452bb31a08175728a4 Mon Sep 17 00:00:00 2001 From: "opencode-agent[bot]" <219766164+opencode-agent[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 03:37:24 +0000 Subject: [PATCH] fix(desktop): finish the macOS Cmd+Q quit after shutdown (#59144) Co-authored-by: Brendonovich <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: anmol0b <91365474+anmol0b@users.noreply.github.com> --- packages/desktop/src/main/lifecycle/index.ts | 26 +++++++++++++++---- packages/desktop/src/main/windows/index.ts | 6 ++--- packages/desktop/src/main/windows/recovery.ts | 6 ++--- .../desktop/src/main/windows/unresponsive.ts | 2 +- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/desktop/src/main/lifecycle/index.ts b/packages/desktop/src/main/lifecycle/index.ts index 5ed4fb312b8..df043c9b96f 100644 --- a/packages/desktop/src/main/lifecycle/index.ts +++ b/packages/desktop/src/main/lifecycle/index.ts @@ -8,7 +8,7 @@ import { emitIpcEvent } from "../ipc-events" import { DesktopLogging, scoped } from "../native/logging" import { DesktopStorage } from "../storage" import { safeWebContentsURL } from "../windows/state" -import { getLastFocusedWindow, getWindowByID, makeMainWindows, setAppQuitting, setRelaunchHandler } from "../windows" +import { getLastFocusedWindow, getWindowByID, mainWindows, setAppQuitting, setRelaunchHandler } from "../windows" import { marks } from "./marks" import { initializeFirstLaunchOnboarding } from "./onboarding" import { Shutdown } from "./shutdown" @@ -29,12 +29,21 @@ const runtime = Layer.effect( Effect.gen(function* () { const shutdown = yield* Shutdown.Service const runFork = Effect.runForkWith(yield* Effect.context()) - const windows = yield* makeMainWindows() + const windows = yield* mainWindows() const createWindow = windows.create const restoreWindows = windows.restore const pendingDeepLinks: string[] = [] let shutdownReady = false - const prepareToRestart = shutdown.run.pipe(Effect.ensuring(Effect.sync(() => (shutdownReady = true)))) + let quitRequested = false + + // A hung shutdown step would otherwise leave every quit waiting on it forever. + const prepareToRestart = shutdown.run.pipe( + Effect.timeoutOrElse({ + duration: "10 seconds", + orElse: () => Effect.logError("shutdown timed out, quitting anyway"), + }), + Effect.ensuring(Effect.sync(() => (shutdownReady = true))), + ) const focusWindow = (win: BrowserWindow | null) => { if (!win) return @@ -94,11 +103,15 @@ const runtime = Layer.effect( } const beforeQuit = (event: Event) => { + quitRequested = true setAppQuitting() if (shutdownReady) return event.preventDefault() - runFork(prepareToRestart.pipe(Effect.ensuring(Effect.sync(() => app.quit())))) + // Electron records the cancelled quit only after this event returns. A shutdown with nothing to + // stop finishes in a microtask, so re-quitting from it would be overwritten and the app would + // stay alive with no windows. Re-quit on the next tick instead. + runFork(prepareToRestart.pipe(Effect.ensuring(Effect.sync(() => setImmediate(() => app.quit()))))) } const willQuit = () => { @@ -125,11 +138,14 @@ const runtime = Layer.effect( runFork(prepareToRestart.pipe(Effect.ensuring(Effect.sync(() => app.quit())))) } + // Electron emits this instead of will-quit when it lost track of a quit, so finish the one we asked for. const windowAllClosed = () => { - if (process.platform !== "darwin") app.quit() + if (quitRequested || process.platform !== "darwin") app.quit() } const activate = () => { + if (quitRequested) return + if (BrowserWindow.getAllWindows().length === 0) restoreWindows() } diff --git a/packages/desktop/src/main/windows/index.ts b/packages/desktop/src/main/windows/index.ts index f19ade4ae52..39d10815168 100644 --- a/packages/desktop/src/main/windows/index.ts +++ b/packages/desktop/src/main/windows/index.ts @@ -25,7 +25,7 @@ import { import { registerRendererProtocol, setProtocolReporter } from "./protocol" import { loadWindow } from "./scheme" import { createWindowRegistry } from "./registry" -import { makeWindowRecovery } from "./recovery" +import { windowRecovery } from "./recovery" import { takeEarlyWindow, type EarlyWindow } from "./early" import { manageWindowState, readWindowState, resolveWindowState, windowStateFile } from "./window-state" import { allowRendererPermissions, wireNavigationPolicy, wireRendererHeaders } from "./security" @@ -114,13 +114,13 @@ export function setWindowThemeReady(win: BrowserWindow) { themeReady.get(win)?.() } -export const makeMainWindows = Effect.fn("Window.make")(function* () { +export const mainWindows = Effect.fn("Window.make")(function* () { const fs = yield* FileSystem.FileSystem const path = yield* Path.Path const paths = yield* DesktopPaths.resolve const storage = yield* DesktopStorage.Service const runFork = Effect.runForkWith(yield* Effect.context()) - const wireWindowRecovery = yield* makeWindowRecovery + const wireWindowRecovery = yield* windowRecovery const restore = () => { // The entry module created and showed the first restored window on ready; it is adopted here, diff --git a/packages/desktop/src/main/windows/recovery.ts b/packages/desktop/src/main/windows/recovery.ts index 79aa9480960..5e65db1368e 100644 --- a/packages/desktop/src/main/windows/recovery.ts +++ b/packages/desktop/src/main/windows/recovery.ts @@ -4,14 +4,14 @@ import { Effect } from "effect" import { DesktopLogging, scoped } from "../native/logging" import { nativeT } from "../native/translations" import { safeWindowURL } from "./state" -import { makeUnresponsiveSampler } from "./unresponsive" +import { unresponsiveSampler } from "./unresponsive" -export const makeWindowRecovery = Effect.gen(function* () { +export const windowRecovery = Effect.gen(function* () { const context = yield* Effect.context() const runFork = Effect.runForkWith(context) const runPromise = Effect.runPromiseWith(context) const logging = yield* DesktopLogging.Service - const createUnresponsiveSampler = yield* makeUnresponsiveSampler + const createUnresponsiveSampler = yield* unresponsiveSampler function wireWindowRecovery(win: BrowserWindow, name: string, relaunch: () => void) { let showing = false diff --git a/packages/desktop/src/main/windows/unresponsive.ts b/packages/desktop/src/main/windows/unresponsive.ts index 8c784ec2b26..2659e9dd1aa 100644 --- a/packages/desktop/src/main/windows/unresponsive.ts +++ b/packages/desktop/src/main/windows/unresponsive.ts @@ -7,7 +7,7 @@ const sampleInterval = 1000 const samplePeriod = 15000 -export const makeUnresponsiveSampler = Effect.gen(function* () { +export const unresponsiveSampler = Effect.gen(function* () { const runFork = Effect.runForkWith(yield* Effect.context()) function createUnresponsiveSampler(win: BrowserWindow, name: string) {