-
Notifications
You must be signed in to change notification settings - Fork 13.5k
fix(core): prevent unhandled promise rejection on IDE MCP fetch failure #26484
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -495,23 +495,13 @@ export class IdeClient { | |
| } | ||
| } | ||
|
|
||
| private registerClientHandlers() { | ||
| // Must be invoked before client.connect so that errors surfaced during | ||
| // connection setup or by a long-lived SSE stream are routed through | ||
| // onerror instead of becoming unhandled promise rejections. | ||
| private registerTransportHandlers() { | ||
| if (!this.client) { | ||
| return; | ||
| } | ||
|
|
||
| this.client.setNotificationHandler( | ||
| IdeContextNotificationSchema, | ||
| (notification) => { | ||
| ideContextStore.set(notification.params); | ||
| const isTrusted = notification.params.workspaceState?.isTrusted; | ||
| if (isTrusted !== undefined) { | ||
| for (const listener of this.trustChangeListeners) { | ||
| listener(isTrusted); | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
| this.client.onerror = (_error) => { | ||
| const errorMessage = _error instanceof Error ? _error.message : `_error`; | ||
| this.setState( | ||
|
|
@@ -527,6 +517,25 @@ export class IdeClient { | |
| true, | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| private registerClientHandlers() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Registering handlers after connection introduces a race condition where early notifications might be missed. Since the MCP SDK allows registering handlers before connection, moving References
|
||
| if (!this.client) { | ||
| return; | ||
| } | ||
|
|
||
| this.client.setNotificationHandler( | ||
| IdeContextNotificationSchema, | ||
| (notification) => { | ||
| ideContextStore.set(notification.params); | ||
| const isTrusted = notification.params.workspaceState?.isTrusted; | ||
| if (isTrusted !== undefined) { | ||
| for (const listener of this.trustChangeListeners) { | ||
| listener(isTrusted); | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
| this.client.setNotificationHandler( | ||
| IdeDiffAcceptedNotificationSchema, | ||
| (notification) => { | ||
|
|
@@ -597,6 +606,7 @@ export class IdeClient { | |
| headers: authToken ? { Authorization: `Bearer ${authToken}` } : {}, | ||
| }, | ||
| }); | ||
| this.registerTransportHandlers(); | ||
| await this.client.connect(transport); | ||
| this.registerClientHandlers(); | ||
| await this.discoverTools(); | ||
|
|
@@ -630,6 +640,7 @@ export class IdeClient { | |
| command, | ||
| args, | ||
| }); | ||
| this.registerTransportHandlers(); | ||
| await this.client.connect(transport); | ||
| this.registerClientHandlers(); | ||
| await this.discoverTools(); | ||
|
|
||
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.
The fallback value
_erroris currently a string literal, which prevents detailed error logging. It should be updated to capture the actual error details for debugging. Additionally, ensure the resulting string is trimmed and falls back to a non-empty default to avoid uninformative messages from whitespace-only strings.References