Skip to content

fix(desktop): finish the macOS Cmd+Q quit after shutdown - #58949

Open
anmol0b wants to merge 1 commit into
anomalyco:devfrom
anmol0b:desktop-quit-hang
Open

anmol0b wants to merge 1 commit into
anomalyco:devfrom
anmol0b:desktop-quit-hang

Conversation

@anmol0b

@anmol0b anmol0b commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #56394

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

On macOS, pressing Cmd+Q closes the window but leaves the main process running. The app never reaches will-quit; Electron fires window-all-closed instead, and on macOS that handler does nothing. Reopening from the Dock then builds a window on a process that is half-quit, which is where the renderer stall comes from.

beforeQuit in lifecycle/index.ts cancels the first quit, runs prepareToRestart, then calls app.quit() again. With keyboard Cmd+Q, that second call runs from a microtask before Electron has finished handling the cancelled quit. Electron then clears its "quitting" flag, so nothing is left to finish the quit once the window closes. kcharlan's lldb trace in the issue shows exactly this. A quit started from JS, or by the quit Apple event, doesn't hit it.

The fix defers the second call to the next timer tick with setTimeout(() => app.quit(), 0). I didn't use app.exit(0) because it skips will-quit, so the storage close, the IPC cleanup and electron-updater's quit hook would no longer run. The relaunch and signal paths don't cancel a quit first, so they are unchanged.

Out of scope: skipping window restore in activate while a quit is in progress (suggested in the issue thread).

How did you verify your code works?

  • On a plain Electron 44.9.1 app with the same cancel and microtask re-issue, keyboard Cmd+Q left the process running in 3 of 3 runs (window-all-closed, no will-quit). With the setTimeout re-issue, all 3 runs reached will-quit and exited.
  • Desktop dev build (bun run dev) on dev, macOS 27, Apple Silicon: after keyboard Cmd+Q the process was still running with 0 windows 10s later. With this change it exited in 3 of 3 fresh launches.
  • bun typecheck in packages/desktop and bun run check pass.
  • No unit test: this only happens in macOS's native keyboard quit path, which bun test can't drive. Cmd+Q was sent through System Events, not pressed by hand.

Screenshots / recordings

Before: after Cmd+Q the process stays alive with no window. Reopening activates that same process, and the window stays on the loading splash.

image

After: Cmd+Q ends the process, and the next launch loads normally.

image
before: pid 34808 still running 8s after Cmd+Q, 0 windows; reopen lands on pid 34808
after:  pid 35640 gone 8s after Cmd+Q; reopen is a fresh pid 36360

before: [2594ms] before-quit -> reissue app.quit() -> before-quit -> [2716ms] window-all-closed -> still running
after:  before-quit -> deferred app.quit() -> before-quit -> will-quit -> quit

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #54296

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks sound to me. I couldn't verify it: this sandbox is Linux without a display, and the bug only shows up with keyboard Cmd+Q in the macOS desktop app.

From reading the code: prepareToRestart sets shutdownReady before the deferred app.quit() is scheduled, so the second before-quit returns early and continues to will-quit without looping. Shutdown.run is cached, so running it again in will-quit costs nothing. The relaunch and signal paths don't cancel a quit first, so leaving them as they are makes sense. tsc reports no errors in src/main/lifecycle.

Public API: unchanged.

This doesn't add a timeout if a shutdown step never finishes, which was the other possible cause raised in #56394. A separate change could guard against that if it still happens after this one.

Related: #54296

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: macOS desktop: Cmd+Q leaves a half-dead instance, and the next Dock click stalls the renderer

1 participant