fix: handle concurrent sendMessage calls in StdioServerTransportProvider#834
Open
dominikozi wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
fix: handle concurrent sendMessage calls in StdioServerTransportProvider#834dominikozi wants to merge 2 commits intomodelcontextprotocol:mainfrom
dominikozi wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
chemicL
reviewed
Feb 24, 2026
|
|
||
| // Verify both concurrent responses complete without error | ||
| StepVerifier | ||
| .create(Mono.delay(java.time.Duration.ofSeconds(2)) |
Member
There was a problem hiding this comment.
The test passes if Duration.ofSeconds(3) is provided. Preferably, instead of using delays, you'd launch a swarm of threads trying to process the messages in parallel and ensure none of them fail and all the messages are processed.
Author
There was a problem hiding this comment.
Updated the test according to your suggestion. The delay-based test has been replaced with a swarm test, which sends 100 requests. The test verifies all response IDs are received, no failures occur, and that concurrency actually happened. Added a 15s Timeout, consistent with existing tests in the repository.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace tryEmitNext() with emitNext() using a busy-looping retry handler in StdioMcpSessionTransport.sendMessage() to support concurrent tool call responses without crashing.
Motivation and Context
Fixes #686.
When multiple tool calls are executed concurrently on MCP server using StdioServerTransportProvider, the server crashes with RuntimeException: Failed to enqueue message. The reason is that sendMessage() used tryEmitNext() on the outbound sink, which fails immediately when another thread is concurrently emitting instead of retrying.
How Has This Been Tested?
Added shouldHandleConcurrentMessages test that sends two concurrent JSON-RPC requests with delayed responses, verifying both complete without error and are written to the output stream. The test fails on main and passes with this change. Verified all existing tests continue to pass.
Breaking Changes
None.
Types of changes
[X] Bug fix (non-breaking change which fixes an issue)
[ ] New feature
[ ] Breaking change
[ ] Documentation update
Checklist
[X] I have read the MCP Documentation
[X] My code follows the repository's style guidelines
[X] New and existing tests pass locally
[X] I have added appropriate error handling
[X] I have added or updated documentation as needed
Additional context
The fix replaces tryEmitNext() with emitNext() using Sinks.EmitFailureHandler.busyLooping(Duration.ofSeconds(1)) in StdioMcpSessionTransport.sendMessage(). This is the standard Reactor pattern for thread safe emission from multiple threads. Non retryable failures still propagate immediately as Sinks.EmissionException. I dont think this requires a documentation update.