-
Notifications
You must be signed in to change notification settings - Fork 644
Fix SendRequestAsync hang on Streamable HTTP SSE retry #1396
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
Open
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/replace-corrupted-pr-1381
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+35
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1d25d9b
Initial plan
Copilot 3686f6f
Fix SendRequestAsync hang when background SSE stream delivers respons…
Copilot fbf0638
Cancel SendToRelatedTransportAsync when response arrives via concurre…
Copilot 6a95b4c
Invert if block for common case, log send faults at debug level
Copilot 69ed9fb
Guard send/race block with tcs.Task.IsCompleted check to skip send wh…
Copilot 37809ca
Simplify ObserveSendFaults: remove #if NET block, inline ContinueWith…
Copilot 0dea2cc
Move session ID and background GET setup before SSE processing to pre…
Copilot 5dc6598
Fix WhenAny race: await initialize send to completion, only race non-…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -536,7 +536,38 @@ public async Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, Canc | |
| LogSendingRequest(EndpointName, request.Method); | ||
| } | ||
|
|
||
| await SendToRelatedTransportAsync(request, cancellationToken).ConfigureAwait(false); | ||
| // For most requests, wait for either the transport send to complete or for the | ||
| // response to arrive via a concurrent channel (e.g. the background GET SSE stream | ||
| // in Streamable HTTP). Without this, the foreground transport send could block | ||
| // indefinitely waiting for a response that was already delivered via a different stream. | ||
| // | ||
| // For the initialize request, always await the send to completion. The transport's | ||
| // SendMessageAsync has side effects (setting SessionId, starting background tasks) | ||
| // that must complete before subsequent messages are sent. | ||
| if (!tcs.Task.IsCompleted) | ||
| { | ||
| Task sendTask = SendToRelatedTransportAsync(request, cancellationToken); | ||
| if (method == RequestMethods.Initialize || | ||
| sendTask == await Task.WhenAny(sendTask, tcs.Task).ConfigureAwait(false)) | ||
| { | ||
| await sendTask.ConfigureAwait(false); | ||
| } | ||
| else | ||
| { | ||
| // The response arrived via a concurrent channel before the transport send completed. | ||
| // Let the send finish naturally but observe any faults at debug level. | ||
| _ = sendTask.ContinueWith( | ||
|
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. I'm not a fan of this at all. This seems way too cute. We shouldn't complicate the client logic because some servers might be leaving response streams open. I doubt that's what's happening in the conformance test anyway as I point out in #1368 (comment) |
||
| static (t, s) => | ||
| { | ||
| var handler = (McpSessionHandler)s!; | ||
| handler.LogTransportSendFaulted(handler.EndpointName, t.Exception!); | ||
| }, | ||
| this, | ||
| CancellationToken.None, | ||
| TaskContinuationOptions.OnlyOnFaulted, | ||
| TaskScheduler.Default); | ||
| } | ||
| } | ||
|
|
||
| // Now that the request has been sent, register for cancellation. If we registered before, | ||
| // a cancellation request could arrive before the server knew about that request ID, in which | ||
|
|
@@ -1078,4 +1109,7 @@ private static McpProtocolException CreateRemoteProtocolException(JsonRpcError e | |
|
|
||
| [LoggerMessage(Level = LogLevel.Trace, Message = "{EndpointName} session {SessionId} disposed with transport {TransportKind}")] | ||
| private partial void LogSessionDisposed(string endpointName, string sessionId, string transportKind); | ||
|
|
||
| [LoggerMessage(Level = LogLevel.Debug, Message = "{EndpointName} transport send faulted after response was already received.")] | ||
| private partial void LogTransportSendFaulted(string endpointName, Exception exception); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Responses should not be delivered to different streams in practice ignoring Tasks and SSE event stream resumption logic. The protocol spec opens the door for it but definitely discourages it, and our server should never send the response to another stream while leaving the HTTP response for the originating request open. If the conformance server is doing that, that seems like a major bug in the conformance MCP server.
Uh oh!
There was an error while loading. Please reload this page.
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.
@halter73, isn't that what the sse-retry test is written to do and test?
The client sends a tools/call via POST.
The server closes the POST SSE stream without sending the response.
The client reconnects via GET (a completely different HTTP request/stream).
The server delivers the tools/call response on the GET stream, a different stream than the one that originated the request.
Uh oh!
There was an error while loading. Please reload this page.
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.
But it still should complete the HTTP response unblocking StreamableHttpClientSessionTransport.SendMessageAsync. It's best to await it so client users know that as long as all their McpClient requests are complete so are all their HTTP requests, and if not, they can still cancel the underlying HTTP request. It's also nice to let unrecoverable HTTP errors bubble up as an observable exception instead of being forced to look at logs.