Skip to content

The agent that grows with you

fix(desktop): install the macOS application menu after the first window exists

teknium140c5986d3e43
diff --git a/apps/desktop/electron/application-menu-startup.test.ts b/apps/desktop/electron/application-menu-startup.test.ts
new file mode 100644
index 00000000000..27063dd814c
--- /dev/null
+++ b/apps/desktop/electron/application-menu-startup.test.ts
@@ -0,0 +1,32 @@
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { installApplicationMenuAfterFirstWindow } from './application-menu-startup'
+
+// #115332: a key equivalent routed through the application menu's delegate
+// before any window exists segfaults the macOS shell on the updater relaunch.
+function record(isMac: boolean) {
+ const order: string[] = []
+
+ installApplicationMenuAfterFirstWindow<string>({
+ isMac,
+ buildMenu: () => {
+ order.push('build-menu')
+
+ return 'menu'
+ },
+ setApplicationMenu: menu => order.push(`set-menu:${menu}`),
+ createWindow: () => order.push('create-window')
+ })
+
+ return order
+}
+
+test('macOS installs the application menu only after the first window exists', () => {
+ assert.deepEqual(record(true), ['create-window', 'build-menu', 'set-menu:menu'])
+})
+
+test('other platforms create the window and never build an application menu', () => {
+ assert.deepEqual(record(false), ['create-window'])
+})
diff --git a/apps/desktop/electron/application-menu-startup.ts b/apps/desktop/electron/application-menu-startup.ts
new file mode 100644
index 00000000000..56e3d55a825
--- /dev/null
+++ b/apps/desktop/electron/application-menu-startup.ts
@@ -0,0 +1,34 @@
+/**
+ * Orders the macOS application menu relative to the first BrowserWindow.
+ *
+ * AppKit routes every key equivalent through the installed application
+ * menu's delegate (`_populateFromDelegateWithEventProvider`), and Electron's
+ * delegate segfaults when that callback fires before any window exists. The
+ * updater's relaunch races the user's keystroke against startup, so the shell
+ * died on both post-update relaunches reported in #115332.
+ *
+ * Two facts shape the fix:
+ * - Electron installs its own default menu at `will-finish-launching` (before
+ * `ready`) unless `Menu.setApplicationMenu` has already been called, so the
+ * suppression must run at module scope, not inside `whenReady`.
+ * - On macOS `Menu.setApplicationMenu(null)` never removes an already
+ * installed NSMenu; it only suppresses the default when it runs first.
+ *
+ * `installApplicationMenuAfterFirstWindow` is the ready-phase half: create the
+ * first window, then install the menu (macOS only; the other platforms ship
+ * without an application menu).
+ */
+export interface ApplicationMenuStartupDeps<TMenu> {
+ isMac: boolean
+ buildMenu: () => TMenu
+ setApplicationMenu: (menu: TMenu | null) => void
+ createWindow: () => void
+}
+
+export function installApplicationMenuAfterFirstWindow<TMenu>(deps: ApplicationMenuStartupDeps<TMenu>): void {
+ deps.createWindow()
+
+ if (deps.isMac) {
+ deps.setApplicationMenu(deps.buildMenu())
+ }
+}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 9238f2003de..3eecfd36c65 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -42,6 +42,7 @@ import {
withRetry
} from './api-transport'
import { appIconCandidates, resolveAppIcon } from './app-icon'
+import { installApplicationMenuAfterFirstWindow } from './application-menu-startup'
import {
stopBackendChild as stopBackendChildImpl,
stopBackendTreesForUpdate,
@@ -1361,6 +1362,14 @@ function previewFileMetadata(filePath, mimeType) {
app.setName(APP_NAME)
+// No application menu until the first window exists. Electron would otherwise
+// install its default menu at `will-finish-launching` (before `ready`), and a
+// key equivalent routed through that menu's delegate with no window open
+// segfaults the macOS shell — the updater relaunch races the user's keystroke
+// (#115332). Must run at module scope: on macOS a later `null` never removes
+// an installed menu. The real menu lands in installApplicationMenuAfterFirstWindow.
+Menu.setApplicationMenu(null)
+
// Windows toast notifications silently no-op unless an AppUserModelID is set:
// `new Notification().show()` returns without error and nothing appears. The
// AUMID must match the installed Start Menu shortcut's AUMID, which
@@ -18237,12 +18246,6 @@ app.whenReady().then(() => {
// connection resolution.
migrateLegacyEncryptedSecretsOnce()
- if (IS_MAC) {
- Menu.setApplicationMenu(buildApplicationMenu())
- } else {
- Menu.setApplicationMenu(null)
- }
-
installMediaPermissions()
installDownloadHandling()
registerMediaProtocol()
@@ -18283,7 +18286,12 @@ app.whenReady().then(() => {
// its worker waits for the install marker to clear, then reopens every scope
// captured by the original transaction before removing the journal entry.
void resumeManagedSshRecoveries()
- createWindow()
+ installApplicationMenuAfterFirstWindow({
+ isMac: IS_MAC,
+ buildMenu: buildApplicationMenu,
+ setApplicationMenu: menu => Menu.setApplicationMenu(menu),
+ createWindow
+ })
// Win/Linux cold start: the launching hermes:// URL is in our own argv.
const _coldStartLink = _extractDeepLink(process.argv)