From cbd47f2c3a24959619bf6aceb21dba47ec58c1ea Mon Sep 17 00:00:00 2001 From: Andy <119136210+AndyMik90@users.noreply.github.com> Date: Wed, 7 Jan 2026 14:05:11 +0100 Subject: [PATCH] fix(insights): await async sendMessage to prevent race condition (#613) (#773) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(insights): await async sendMessage to prevent race condition (#613) The IPC handler for INSIGHTS_SEND_MESSAGE was declared async but never awaited the sendMessage() call. This caused race conditions where async environment setup (getAPIProfileEnv) wouldn't complete before the Python process was spawned. On Windows especially, this led to "Process exited with code 1" errors because environment variables weren't set in time. The fix adds await to ensure all async operations complete before returning, and wraps in try/catch to prevent unhandled rejections. Fixes #613 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 * fix: send errors to UI in catch block Address Gemini Code Assist feedback - errors caught in the try/catch block are now also sent to the renderer process via IPC_CHANNELS.INSIGHTS_ERROR. This ensures all error types (not just executor errors) are reported to the UI. Co-authored-by: gemini-code-assist[bot] 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 * chore: add config.json to gitignore Prevents worktree configuration files from being accidentally committed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --------- Co-authored-by: Claude Opus 4.5 Co-authored-by: Alex <63423455+AlexMadera@users.noreply.github.com> --- .gitignore | 1 + .../main/ipc-handlers/insights-handlers.ts | 24 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index e90fde6a..fa5bcb63 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,7 @@ lerna-debug.log* # Git Worktrees (parallel builds) # =========================== .worktrees/ +/config.json # =========================== # Auto Claude Generated diff --git a/apps/frontend/src/main/ipc-handlers/insights-handlers.ts b/apps/frontend/src/main/ipc-handlers/insights-handlers.ts index cef96a6d..11a18c0b 100644 --- a/apps/frontend/src/main/ipc-handlers/insights-handlers.ts +++ b/apps/frontend/src/main/ipc-handlers/insights-handlers.ts @@ -42,9 +42,27 @@ export function registerInsightsHandlers( return; } - // Note: Python environment initialization should be handled by insightsService - // or added here with proper dependency injection if needed - insightsService.sendMessage(projectId, project.path, message, modelConfig); + // Await the async sendMessage to ensure proper error handling and + // that all async operations (like getProcessEnv) complete before + // the handler returns. This fixes race conditions on Windows where + // environment setup wouldn't complete before process spawn. + try { + await insightsService.sendMessage(projectId, project.path, message, modelConfig); + } catch (error) { + // Errors during sendMessage (executor errors) are already emitted via + // the 'error' event, but we catch here to prevent unhandled rejection + // and ensure all error types are reported to the UI + console.error('[Insights IPC] Error in sendMessage:', error); + const mainWindow = getMainWindow(); + if (mainWindow) { + const errorMessage = error instanceof Error ? error.message : String(error); + mainWindow.webContents.send( + IPC_CHANNELS.INSIGHTS_ERROR, + projectId, + `Failed to send message: ${errorMessage}` + ); + } + } } );