Skip to content

fix(core): remove pending response entries on request timeout and cancellation - #1134

Open
Lubaoshuai wants to merge 1 commit into
modelcontextprotocol:mainfrom
Lubaoshuai:fix/pending-response-timeout-leak
Open

Lubaoshuai wants to merge 1 commit into
modelcontextprotocol:mainfrom
Lubaoshuai:fix/pending-response-timeout-leak

Conversation

@Lubaoshuai

Copy link
Copy Markdown

Fixes #1133

McpClientSession.sendRequest and McpServerSession.sendRequest left their pendingResponses entry in place when the downstream .timeout(...) fired or the caller cancelled — the entry was only removed on a response or a send failure. Since request IDs are unique per request, a timed-out request leaked its entry forever; KeepAliveScheduler pings amplify this into one leaked entry per interval on sessions with a dead peer.

Both methods now mirror the cleanup pattern already used by McpStreamableServerSession.McpStreamableServerSessionStream.sendRequest: doOnError + doOnCancel remove the entry after the timeout boundary. Removal is idempotent, and a late response for a removed id already takes the existing "unexpected response" path.

How verified

  • New testRequestTimeoutRemovesPendingResponse in McpClientSessionTests: a request against a 50 ms-timeout session is let expire, then the pendingResponses map is asserted empty.
  • ./mvnw -pl mcp-core test passes locally.

…cellation

McpClientSession and McpServerSession left the pendingResponses entry in
place when the downstream timeout fired or the caller cancelled, so each
timed-out request leaked its entry forever (request IDs are unique).
Mirror the doOnError/doOnCancel cleanup the streamable session variant
already performs after .timeout(requestTimeout).

Fixes modelcontextprotocol#1133
@Lubaoshuai
Lubaoshuai force-pushed the fix/pending-response-timeout-leak branch from b25d2cc to 9b8604e Compare September 13, 2026 18:03
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.

McpClientSession/McpServerSession: pending response entries leak when a request times out or is cancelled

1 participant