Skip to content

mcp: fix concurrent request response collection race#299628

Merged
connor4312 merged 2 commits intomainfrom
connor4312/mcp-enablement
Mar 6, 2026
Merged

mcp: fix concurrent request response collection race#299628
connor4312 merged 2 commits intomainfrom
connor4312/mcp-enablement

Conversation

@connor4312
Copy link
Member

  • JsonRpcProtocol.handleMessage now returns JsonRpcMessage[] containing
    responses generated by incoming requests, rather than delegating
    response collection to callers via side-channel state
  • McpGatewaySession simplified by removing _pendingResponses and
    _isCollectingPostResponses fields, which were susceptible to racing
    under concurrent HTTP POSTs. Now directly uses handleMessage's
    return value for the response body
  • _send callback still invoked for all messages (backward compatible
    with McpServerRequestHandler and SSE notification broadcast)
  • Updated tests to assert on handleMessage return values

Fixes #297780

(Commit message generated by Copilot)

- JsonRpcProtocol.handleMessage now returns JsonRpcMessage[] containing
  responses generated by incoming requests, rather than delegating
  response collection to callers via side-channel state
- McpGatewaySession simplified by removing _pendingResponses and
  _isCollectingPostResponses fields, which were susceptible to racing
  under concurrent HTTP POSTs. Now directly uses handleMessage's
  return value for the response body
- _send callback still invoked for all messages (backward compatible
  with McpServerRequestHandler and SSE notification broadcast)
- Updated tests to assert on handleMessage return values

Fixes #297780

(Commit message generated by Copilot)
Copilot AI review requested due to automatic review settings March 5, 2026 22:43
@connor4312 connor4312 added this to the 1.112.0 milestone Mar 5, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR removes side-channel response collection to fix a race when concurrent HTTP POSTs are processed, by having JsonRpcProtocol.handleMessage return the response message(s) generated while handling incoming requests.

Changes:

  • JsonRpcProtocol.handleMessage now returns JsonRpcMessage[] containing generated responses.
  • McpGatewaySession no longer uses _pendingResponses / _isCollectingPostResponses and instead returns handleMessage’s replies directly.
  • Tests updated to assert on handleMessage return values.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/vs/platform/mcp/node/mcpGatewaySession.ts Removes racy side-channel response collection and uses returned replies.
src/vs/base/common/jsonRpcProtocol.ts Changes protocol API to return generated reply messages while still calling _send.
src/vs/base/test/common/jsonRpcProtocol.test.ts Updates assertions to validate handleMessage reply arrays.

You can also share your feedback on Copilot code review. Take the survey.

- Adds JSDoc to handleMessage clarifying what is returned (only responses
  for incoming requests), ordering guarantees for batch inputs, and that
  responses are still emitted via _send callback to avoid double-sending
- Tightens _handleRequest return type from Promise<JsonRpcMessage> to
  Promise<JsonRpcResponse>, enforcing that only valid responses are
  returned. Introduces JsonRpcResponse type alias for better type safety
- Expands error handling tests to assert that returned replies match
  what is emitted via _send for both JsonRpcError and generic error paths

Fixes #297780

(Commit message generated by Copilot)
@connor4312 connor4312 enabled auto-merge (squash) March 6, 2026 22:38
@connor4312 connor4312 merged commit 57479c0 into main Mar 6, 2026
20 checks passed
@connor4312 connor4312 deleted the connor4312/mcp-enablement branch March 6, 2026 22:52
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.

MCP Gateway performance needs improvement

3 participants