Skip to content

Conversation

@findleyr
Copy link
Contributor

@findleyr findleyr commented Dec 3, 2025

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 Context propagation is broken in StreamableClientTransport #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 #662

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
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@findleyr findleyr merged commit 272e0cd into modelcontextprotocol:main Dec 3, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use call context when reconnecting

2 participants