Skip to content

[v1.x] Resolve a failed SSE message POST instead of hanging the caller - #3892

Closed
Darshak03 wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
Darshak03:fix/v1.x-sse-post-error-resolves-request
Closed

Darshak03 wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
Darshak03:fix/v1.x-sse-post-error-resolves-request

Conversation

@Darshak03

Copy link
Copy Markdown

Fixes #2110 (the SSE half, on v1.x).

This brings the SSE fix from #3278 (on main) to v1.x.

Problem

When the SSE transport's message POST fails, with a non-2xx status or an exception, post_writer logs Error in post_writer and stops. The request that was sent never gets a response, so the caller waits out the full session read timeout, and every later message in that session is dropped because the writer is gone.

This matters most where response time is critical. A phone-call voice agent connects to its MCP server when a call is answered, and every second of silence counts. Today a failed POST turns into a long wait, and by the time the timeout fires the caller has often hung up. If the request failed at once, the agent could hand the call to a human agent while the caller is still on the line.

Fix

post_writer now resolves the failed request instead of stopping:

  • a non-2xx status: INTERNAL_ERROR "Server returned an error response";
  • a 404 on an endpoint that carries a session id: INVALID_REQUEST "Session terminated", as the streamable HTTP transport does;
  • an exception raised by the POST (network error, auth flow, event hook): CONNECTION_CLOSED;
  • a failed notification has no caller waiting, so it is only logged.

The writer keeps running, so the session stays usable after a failed POST.

The error is sent to the read stream from its own task. On v1.x the client session answers server requests (for example roots/list) inside its receive loop. If the writer delivered the error itself, it could block while that loop is blocked sending a reply to the writer, and both would hang. A test covers this interleaving.

Tests

New tests in tests/shared/test_sse.py use an in-process SSE server that fails chosen POSTs:

  • initialize answered with 302, 401, 429, 500 or 503 fails at once with McpError, with a 20 s session read timeout;
  • a later tools/call answered with 500 fails at once, and the next ping and tool call succeed;
  • a POST that raises fails its request with CONNECTION_CLOSED, and the session keeps working;
  • the 404 mapping with and without a session id;
  • a failed notification leaves the session usable;
  • a failed POST while a server request is being answered does not deadlock;
  • an error that can no longer be delivered, because the read stream closed, does not stop the writer.

All of them fail on unpatched v1.x. uv run pytest passes (1342 passed) with 100% coverage, and ruff check, ruff format --check and pyright are clean.

When a message POST failed with a non-2xx status or raised, post_writer
only logged it and stopped. The request waited out the session read
timeout, and every later message in the session was dropped.

The failed request now gets a JSON-RPC error with its id: INTERNAL_ERROR
for a non-2xx status, INVALID_REQUEST "Session terminated" for a 404 on
an endpoint that carries a session id (as the streamable HTTP transport
does), and CONNECTION_CLOSED when the POST raised. A failed notification
is only logged. The writer keeps running, so the session stays usable.

The error is delivered from its own task: the session answers server
requests inside its receive loop, so a reply can be waiting on the
writer while a POST fails, and delivering inline would deadlock.

Github-Issue: modelcontextprotocol#2110
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #2110.

If a maintainer assigns you to #2110, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take.

You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way.

CONTRIBUTING.md has the full reasoning, but in short:

  • We're a small team with very little capacity to review community PRs right now.
  • Many recent PRs are AI-generated with little human review, and reviewing one carefully still costs a maintainer as much time as it ever did. A well-described issue is usually more useful to us than the code.

Maintainers: reopen, remove missing-issue-link, or add bypass-issue-check to override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant