Added thread-safe initialization to the McpServerManager singleton pattern to
prevent potential race conditions when getInstance is called concurrently.
The changes include:
1. Added initializationPromise to track ongoing initialization
2. Implemented double-checked locking pattern:
- First check: Return existing instance if available
- Second check: Wait for any ongoing initialization
- Third check: Double-check inside initialization block
3. Added proper cleanup in finally block to prevent deadlocks
This ensures that:
- Only one McpHub instance is ever created
- Concurrent calls wait for initialization to complete
- Resources are properly initialized and cleaned up
- No memory leaks from incomplete initialization
The fix maintains the existing functionality while making it safe for
concurrent access in VS Code's multi-window environment.
When the MCP server was initialized before opening RooCode, the server list
would not appear in the settings. This was fixed by:
1. Adding proper server list initialization in ClineProvider.ts when the
webview launches, checking if mcpHub exists and sending its current
servers to the webview:
```typescript
if (this.mcpHub) {
this.postMessageToWebview({
type: "mcpServers",
mcpServers: this.mcpHub.getServers()
})
}
```
2. Using the public getServers() method from McpHub instead of relying on
internal state updates, ensuring consistent server list state across
the application.
The fix maintains clean separation of concerns and follows existing patterns
for state management between the extension and webview.
Problem:
- Multiple instances of the Roo Code application were launching separate MCP server instances
- This led to unnecessary resource consumption and potential conflicts between instances
Solution:
1. Created new McpServerManager singleton class to manage MCP server instances:
- Static getInstance() method ensures only one McpHub instance exists
- Tracks registered ClineProvider instances
- Handles cleanup on extension deactivation
2. Modified ClineProvider class:
- Changed mcpHub from private to protected
- Added getMcpHub() public getter method
- Updated initialization to use McpServerManager
- Added unregister logic in dispose()
3. Updated extension.ts to handle cleanup:
- Added McpServerManager cleanup in deactivate()
Technical Implementation:
- Uses WeakRef for provider tracking to allow proper garbage collection
- Maintains global state to track instance IDs
- Implements proper cleanup of resources on disposal
- Ensures backward compatibility with existing code
This change significantly improves resource usage and prevents potential conflicts
between multiple instances of the application while maintaining all existing
functionality.