gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

fix(core): handle unhandled promise rejection in mcp-client-manager

Open kamja44 opened this issue 2 months ago • 2 comments

  • Add outer try-catch block to catch initialization errors
  • Connect async IIFE rejection to Promise reject handler
  • Add tests for error scenarios during client initialization
  • Prevents unhandled promise rejection when McpClient constructor throws
  • Ensures one server failure doesn't stop other servers from starting

Fixes #14700

Summary

Fixes a structural issue in McpClientManager.addOrUpdateServer() that causes unhandled promise rejections during MCP server initialization. The Promise constructor received an unused _reject parameter, and the async IIFE lacked proper error handling, causing crashes when errors occurred in the McpClient constructor or disconnect method.

Details

Root cause: The current implementation creates a Promise with _reject parameter but never uses it. The async IIFE has no .catch() handler, so errors thrown before the try block become unhandled rejections.

Changes made:

  1. Changed _reject to reject and properly connected it
  2. Added outer try-catch to catch initialization errors (constructor, disconnect)
  3. Added .catch(reject) to async IIFE to handle any uncaught errors
  4. Added proper error logging via coreEvents.emitFeedback

Files modified:

  • packages/core/src/tools/mcp-client-manager.ts (lines 162-220)
  • packages/core/src/tools/mcp-client-manager.test.ts (added 2 test cases)

Note: This is NOT a duplicate of #8365. That issue was about MCP timeout errors in gemini.mjs and is already closed. This PR fixes a different structural problem in mcp-client-manager.ts that could cause unhandled rejections during initialization.

Related Issues

Fixes #14700

How to Validate

  1. Run unit tests:
npm run test mcp-client-manager.test.ts

## Pre-Merge Checklist

<!-- Check all that apply before requesting review or merging. -->

- [ ] Updated relevant documentation and README (if needed)
- [O] Added/updated tests (if needed)
- [ ] Noted breaking changes (if any)
- [ ] Validated on required platforms/methods:
  - [ ] MacOS
    - [ ] npm run
    - [ ] npx
    - [ ] Docker
    - [ ] Podman
    - [ ] Seatbelt
  - [ ] Windows
    - [ ] npm run
    - [ ] npx
    - [ ] Docker
  - [ ] Linux
    - [O] npm run
    - [ ] npx
    - [ ] Docker

kamja44 avatar Dec 08 '25 10:12 kamja44