-
Notifications
You must be signed in to change notification settings - Fork 2.6k
refactor(mcp): Implement reference counting for McpHub lifecycle #2310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Problem: Closing auxiliary windows (e.g., from "open in editor") could prematurely dispose the shared singleton McpHub instance, shutting down MCP servers unexpectedly while other panels (like the sidebar) might still be active. Solution: Implemented reference counting directly within the McpHub class to manage its own lifecycle based on active clients (ClineProvider instances). - Added `refCount`, `registerClient()`, and `unregisterClient()` methods to McpHub. - McpHub now disposes itself only when the last registered client unregisters (`refCount` reaches 0). - ClineProvider instances now call `mcpHub.registerClient()` upon initialization and `mcpHub.unregisterClient()` upon disposal. - Removed the direct `mcpHub.dispose()` call from ClineProvider.dispose. Benefit: This ensures the shared McpHub instance remains active as long as at least one ClineProvider instance is using it. Cleanup now correctly occurs only when the last provider is closed or during full extension deactivation. This centralizes the resource's lifecycle logic within the resource class itself. Files Changed: - src/services/mcp/McpHub.ts - src/core/webview/ClineProvider.ts
|
| * If the count reaches zero, disposes the hub. | ||
| */ | ||
| public async unregisterClient(): Promise<void> { | ||
| this.refCount-- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In unregisterClient(), consider adding a safeguard to prevent the reference count from dropping below zero before proceeding to dispose. This helps avoid potential underflow if unregisterClient() is called extra times.
| this.refCount-- | |
| if (this.refCount > 0) this.refCount-- |
mrubens
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Problem:
Closing auxiliary windows (e.g., from "open in editor") could prematurely dispose the shared singleton McpHub instance, shutting down MCP servers unexpectedly while other panels (like the sidebar) might still be active.
Solution:
Implemented reference counting directly within the McpHub class to manage its own lifecycle based on active clients (ClineProvider instances).
refCount,registerClient(), andunregisterClient()methods to McpHub.refCountreaches 0).mcpHub.registerClient()upon initialization andmcpHub.unregisterClient()upon disposal.mcpHub.dispose()call from ClineProvider.dispose.Benefit:
This ensures the shared McpHub instance remains active as long as at least one ClineProvider instance is using it. Cleanup now correctly occurs only when the last provider is closed or during full extension deactivation. This centralizes the resource's lifecycle logic within the resource class itself.
Files Changed:
Important
Implements reference counting in
McpHubto manage its lifecycle based on activeClineProviderinstances, preventing premature disposal.McpHubto manage lifecycle based on activeClineProviderinstances.McpHubdisposes itself only whenrefCountreaches 0.ClineProvidercallsregisterClient()on initialization andunregisterClient()on disposal.dispose()call fromClineProvider.dispose().registerClient()andunregisterClient()toMcpHub.McpHub.tsandClineProvider.ts.This description was created by
for 0c51f38. It will automatically update as commits are pushed.