mcp: better handling for streamable context cancellation#677
Merged
findleyr merged 2 commits intomodelcontextprotocol:mainfrom Dec 3, 2025
Merged
mcp: better handling for streamable context cancellation#677findleyr merged 2 commits intomodelcontextprotocol:mainfrom
findleyr merged 2 commits intomodelcontextprotocol:mainfrom
Conversation
After walking through our handling of streamable client context cancellation (due to encountering a shutdown deadlock), I think I've settled on a more coherent strategy for handling call cancellation: - In our call handler, retire the request if the call exits due to cancellation: the caller will never see the actual result anyway. - In connectSSE, use the actual request context (the same context used in Write) for the client request, so that it terminates when the context is cancelled. Thread through the initialization context for the standalone SSE request. Also, a couple minor improvements: - Use a detached context for the background context of the client connection. We want to preserve context values (see modelcontextprotocol#513), but it is not right to cancel the connection after Connect has already returned, if the context times out. - Don't use Last-Event-ID != "" as the signal for whether the connectSSE call is initial: if the standalone SSE stream disconnects without an event ID, we'll still reconnect it, and don't want to do so without a delay. + tests, updating the streamable client connection test harness to accomodate the new aspects being exercised. Fixes modelcontextprotocol#662
mcp/streamable.go
Outdated
| func (c *streamableClientConn) handleSSE(ctx context.Context, requestSummary string, resp *http.Response, persistent bool, forCall *jsonrpc2.Request) { | ||
| for { | ||
| // Connection was successful. Continue the loop with the new response. | ||
| // TODO: we should set a reasonable limit on the number of times we'll try |
Contributor
There was a problem hiding this comment.
How serious are the implications of not doing this (setting a reasonable retry limit?)
If this also cause a resource leak then it feels like it makes sense to create an explicit bug so we don't forget to fix this too
Related to comment on line 1839 below too
markus-kusano
approved these changes
Dec 3, 2025
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
After walking through our handling of streamable client context cancellation (due to encountering a shutdown deadlock), I think I've settled on a more coherent strategy for handling call cancellation:
Also, a couple minor improvements:
Fixes #662