Skip to content

[Server][Streamable HTTP] Concurrent SSE streams on one session can consume each other's client responses #544

Description

@mglaman

Summary

On Streamable HTTP (handshake era, SSE), two tool calls on one session that both send a server-to-client request, such as elicitation/create, can each receive the other's answer.

Cause

Protocol keeps pending server-to-client requests in one session-wide list, _mcp.pending_requests. The SSE loop in StreamableHttpTransport::createStreamedResponse() walks that whole list, not only the requests its own fiber sent:

$pendingRequests = $this->getPendingRequests($this->sessionId);
// ...
foreach ($pendingRequests as $pending) {
    $response = $this->checkForResponse($pending['request_id'], $this->sessionId);
    if (null !== $response) {
        $yielded = $this->sessionFiber->resume($response);
        // ...

With tool calls A and B open on one session, each waiting in ClientGateway::elicit():

  1. The client answers B's elicitation.
  2. A's loop polls first, finds B's answer, consumes it, and resumes A's fiber with it. A's tool continues with B's answer.
  3. B's answer is gone from the session. B's tool waits until its 120-second timeout.

The timeout branch has the same problem: A's loop can resume A's fiber with a timeout error for B's request ID.

I found this by reading the source on main (a5ed85f) and 0.8.1. I have not reproduced it end to end. Running two streams on one session at the same time needs a server that doesn't serialize requests per session.

Suggested fix

Record which stream sent each pending request, and have each loop check only its own. For example, track the request IDs the fiber yielded in the transport instance, and filter getPendingRequests() by them.

Context

In Drupal's mcp_server we lock the session for the length of a stream. That lock made a second elicitation wait, which hid this bug. We're changing the lock to cover each session write instead of the whole stream, so elicitation answers don't wait on the lease (mcp_server!88). That makes this race reachable.

Activity

  1. jarrettdustinqq commented on Oct 7, 2026

    @jarrettdustinqq

    I confirmed the ownership problem at a5ed85f and found a second state-lifecycle requirement that seems important for the fix.

    Today checkResponse() is the only server path that removes an entry from _mcp.pending_requests. The timeout branch in StreamableHttpTransport::createStreamedResponse() resumes the fiber with an error but does not remove that request. Therefore even an otherwise correctly owned timeout leaves a stale, already-expired pending entry in the session; a later stream can encounter it and immediately resume the wrong fiber with that old timeout. In the other direction, handleResponse() stores any keyed response without first checking that the request is still pending, so a response arriving after timeout can remain orphaned.

    I would make response/timeout resolution one atomic operation rather than adding only a read-side filter:

    1. Each pending record gets an opaque stream/fiber owner at creation.
    2. A loop may atomically claim and remove only a pending record with its owner.
    3. Response completion consumes both the response and pending record; timeout completion removes the pending record before resuming.
    4. A late response with no pending owner is discarded/logged, never left for a future fiber.

    The owner cannot safely be inferred by diffing the session-wide list: handleFiberYield() currently discards the request ID returned by Protocol::sendRequest(), and the whole-session read/modify/write race already tracked in #275 could overwrite that inference. Returning the created ID from the yield callback, or passing a transport-generated owner token into sendRequest(), would make the association explicit.

    Regression cases I would add:

    • A and B pending; B responds first: only B resumes and A remains pending.
    • A responds while B expires: each fiber receives only its own outcome.
    • After timeout, B is absent from pending state.
    • A late B response is ignored and cannot resume a later fiber.
    • Repeat the cases with an interleaving session save so the fix does not rely on non-atomic whole-session mutation.

    AI disclosure: ChatGPT helped inspect the current source and draft this review. This is source-level verification on a clean checkout; I did not execute a PHP concurrency harness.

  2. added
    ServerIssues & PRs related to the Server component
    bugSomething isn't working
    P2Moderate issues affecting some users, edge cases, potentially valuable feature
    on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Moderate issues affecting some users, edge cases, potentially valuable featureServerIssues & PRs related to the Server componentbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions