Harden dev menu rebuild and extension startup paths#703
Conversation
Guard development-only menu and devtools initialization paths against BrowserWindow teardown races. This avoids Object has been destroyed errors when the menu is rebuilt during window shutdown, adds an explicit catch for asynchronous menu rebuild failures, and downgrades optional React DevTools installation failures to a compact warning instead of a noisy stack trace during development startup.
WalkthroughThis pull request improves error handling and window lifecycle management across three files. It replaces promise chaining with explicit try/catch in extension installation, adds window liveness checks to prevent operations on destroyed windows, and ensures promise rejections are properly handled to avoid unhandled rejection warnings. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/main/menu.ts (1)
163-170:⚠️ Potential issue | 🟠 MajorHandle rejected
buildMenu()promise in theme updates.Line 170 calls
buildMenu()without error handling. SincebuildMenu()can reject if template building fails (viabuildDarwinTemplate()orbuildDefaultTemplate()), unhandled rejections will occur during theme toggles. The pattern for proper error handling already exists elsewhere in the codebase (seesrc/main/modules/ipc/main.ts:handleWindowRebuildMenu).Suggested fix
updateAppTheme() { const newTheme = nativeTheme.shouldUseDarkColors ? 'light' : 'dark' nativeTheme.themeSource = newTheme store.set('theme', newTheme) if (this.hasLiveWindow()) { this.mainWindow.webContents.send('system:update-theme') } - void this.buildMenu() + void this.buildMenu().catch((error) => { + console.error('Error rebuilding application menu:', error) + }) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/main/menu.ts` around lines 163 - 170, updateAppTheme toggles theme and calls buildMenu() without handling rejections; wrap the buildMenu() call in a promise rejection handler (e.g., use await in an async context or append .catch(...)) so any errors from buildMenu()/buildDarwinTemplate()/buildDefaultTemplate() are caught and handled similarly to handleWindowRebuildMenu in src/main/modules/ipc/main.ts (log the error and avoid unhandled rejection). Locate updateAppTheme, nativeTheme, store, hasLiveWindow, mainWindow.webContents.send and replace the bare void this.buildMenu() call with a safe invocation that catches and logs the error.
🧹 Nitpick comments (1)
src/main/menu.ts (1)
144-160: Remove duplicate context-menu listeners on repeated menu rebuilds.
buildMenu()is called multiple times (initial setup, IPC rebuild handler, theme updates), and each call invokessetupDevelopmentEnvironment(), which registers a newwebContents.on('context-menu', ...)handler without removing the previous one. This accumulates listeners over dev sessions. UsewebContents.removeAllListeners('context-menu')before registering, or switch to.once()if this listener should only fire once per rebuild.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/main/menu.ts` around lines 144 - 160, The context-menu handler registered in setupDevelopmentEnvironment() (called from buildMenu()) is being added repeatedly via this.mainWindow.webContents.on('context-menu', ...) causing duplicate listeners; before registering the handler, remove existing handlers with this.mainWindow.webContents.removeAllListeners('context-menu') (or use .once(...) if it should fire only once) and then add the listener that calls this.mainWindow.webContents.inspectElement(x, y); update setupDevelopmentEnvironment() to perform this cleanup using the webContents API to prevent accumulating listeners across rebuilds.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@src/main/menu.ts`:
- Around line 163-170: updateAppTheme toggles theme and calls buildMenu()
without handling rejections; wrap the buildMenu() call in a promise rejection
handler (e.g., use await in an async context or append .catch(...)) so any
errors from buildMenu()/buildDarwinTemplate()/buildDefaultTemplate() are caught
and handled similarly to handleWindowRebuildMenu in src/main/modules/ipc/main.ts
(log the error and avoid unhandled rejection). Locate updateAppTheme,
nativeTheme, store, hasLiveWindow, mainWindow.webContents.send and replace the
bare void this.buildMenu() call with a safe invocation that catches and logs the
error.
---
Nitpick comments:
In `@src/main/menu.ts`:
- Around line 144-160: The context-menu handler registered in
setupDevelopmentEnvironment() (called from buildMenu()) is being added
repeatedly via this.mainWindow.webContents.on('context-menu', ...) causing
duplicate listeners; before registering the handler, remove existing handlers
with this.mainWindow.webContents.removeAllListeners('context-menu') (or use
.once(...) if it should fire only once) and then add the listener that calls
this.mainWindow.webContents.inspectElement(x, y); update
setupDevelopmentEnvironment() to perform this cleanup using the webContents API
to prevent accumulating listeners across rebuilds.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 1008fc7e-70b1-40cc-bcbd-bdd9ba2e8830
📒 Files selected for processing (3)
src/main/main.tssrc/main/menu.tssrc/main/modules/ipc/main.ts
Pull request info
References
If applicable substitute the issue reference to the one that this PR refers
This PR resolve the N/A.
Link to Jira task
N/A
Description of the changes proposed
context-menulisteners from accumulating across menu rebuildsbuildMenu()promises during theme updates and IPC-triggered rebuildsDOD checklist