Skip to content

Commit f6a0dc1

Browse files
nquinquenelKehrlann
authored andcommitted
fix: make STDIO transport sendMessage thread-safe (#304)
Concurrent calls to `sendMessage` on the STDIO transports raced on the unicast sink and sporadically failed with `FAIL_NON_SERIALIZED`, dropping the message with `RuntimeException("Failed to enqueue message")`. Switch `tryEmitNext` to `emitNext` with a `busyLooping` EmitFailureHandler so concurrent emissions are serialized as recommended by the Reactor maintainers. Applied on both `StdioServerTransportProvider` and `StdioClientTransport`. Also adds a regression test that fans `sendMessage` out across 16 parallel rails and verifies all 500 emissions complete successfully and every message is written to the output stream.
1 parent 5931625 commit f6a0dc1

3 files changed

Lines changed: 59 additions & 16 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/client/transport/StdioClientTransport.java‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,13 @@ private void handleIncomingErrors() {
258258

259259
@Override
260260
public Mono<Void> sendMessage(JSONRPCMessage message) {
261-
if (this.outboundSink.tryEmitNext(message).isSuccess()) {
262-
// TODO: essentially we could reschedule ourselves in some time and make
263-
// another attempt with the already read data but pause reading until
264-
// success
265-
// In this approach we delegate the retry and the backpressure onto the
266-
// caller. This might be enough for most cases.
261+
try {
262+
// busyLooping retries FAIL_NON_SERIALIZED under concurrent senders
263+
this.outboundSink.emitNext(message, Sinks.EmitFailureHandler.busyLooping(Duration.ofMillis(100)));
267264
return Mono.empty();
268265
}
269-
else {
270-
return Mono.error(new RuntimeException("Failed to enqueue message"));
266+
catch (Sinks.EmissionException e) {
267+
return Mono.error(new RuntimeException("Failed to enqueue message", e));
271268
}
272269
}
273270

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/StdioServerTransportProvider.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import java.io.InputStreamReader;
1111
import java.io.OutputStream;
1212
import java.nio.charset.StandardCharsets;
13+
import java.time.Duration;
1314
import java.util.List;
1415
import java.util.concurrent.Executors;
1516
import java.util.concurrent.atomic.AtomicBoolean;
1617
import java.util.function.Function;
1718

1819
import io.modelcontextprotocol.json.TypeRef;
19-
import io.modelcontextprotocol.spec.McpError;
2020
import io.modelcontextprotocol.spec.McpSchema;
2121
import io.modelcontextprotocol.spec.McpSchema.JSONRPCMessage;
2222
import io.modelcontextprotocol.spec.McpServerSession;
@@ -175,11 +175,12 @@ public StdioMcpSessionTransport() {
175175
public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
176176

177177
return Mono.zip(inboundReady.asMono(), outboundReady.asMono()).then(Mono.defer(() -> {
178-
if (outboundSink.tryEmitNext(message).isSuccess()) {
178+
try {
179+
outboundSink.emitNext(message, Sinks.EmitFailureHandler.busyLooping(Duration.ofMillis(100)));
179180
return Mono.empty();
180181
}
181-
else {
182-
return Mono.error(new RuntimeException("Failed to enqueue message"));
182+
catch (Sinks.EmissionException e) {
183+
return Mono.error(new RuntimeException("Failed to enqueue message", e));
183184
}
184185
}));
185186
}

‎mcp-test/src/test/java/io/modelcontextprotocol/server/transport/StdioServerTransportProviderTests.java‎

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import org.junit.jupiter.api.AfterEach;
2626
import org.junit.jupiter.api.BeforeEach;
2727
import org.junit.jupiter.api.Test;
28+
import reactor.core.publisher.Flux;
2829
import reactor.core.publisher.Mono;
30+
import reactor.core.scheduler.Schedulers;
2931
import reactor.test.StepVerifier;
3032

3133
import static org.assertj.core.api.Assertions.assertThat;
@@ -99,7 +101,7 @@ void shouldCreateSessionWhenSessionFactoryIsSet() {
99101
}
100102

101103
@Test
102-
void shouldHandleIncomingMessages() throws Exception {
104+
void shouldHandleIncomingMessages() {
103105

104106
String jsonMessage = "{\"jsonrpc\":\"2.0\",\"method\":\"test\",\"params\":{},\"id\":1}\n";
105107
InputStream stream = new ByteArrayInputStream(jsonMessage.getBytes(StandardCharsets.UTF_8));
@@ -229,7 +231,7 @@ void shouldHandleNotificationBeforeSessionFactoryIsSet() {
229231
}
230232

231233
@Test
232-
void shouldHandleInvalidJsonMessage() throws Exception {
234+
void shouldHandleInvalidJsonMessage() {
233235

234236
// Write an invalid JSON message to the input stream
235237
String jsonMessage = "{invalid json}\n";
@@ -248,7 +250,7 @@ void shouldHandleInvalidJsonMessage() throws Exception {
248250
}
249251

250252
@Test
251-
void shouldRejectInboundMessageExceedingMaxSize() throws Exception {
253+
void shouldRejectInboundMessageExceedingMaxSize() {
252254
// A line larger than the configured limit that never terminates with a newline.
253255
// BufferedReader#readLine would buffer the whole thing; the bounded reader must
254256
// abort instead.
@@ -291,7 +293,7 @@ void shouldRejectNonPositiveMaxSize() {
291293
}
292294

293295
@Test
294-
void shouldHandleSessionClose() throws Exception {
296+
void shouldHandleSessionClose() {
295297
// Set session factory
296298
transportProvider.setSessionFactory(sessionFactory);
297299

@@ -302,4 +304,47 @@ void shouldHandleSessionClose() throws Exception {
302304
verify(mockSession).closeGracefully();
303305
}
304306

307+
@Test
308+
void shouldHandleConcurrentSendMessage() {
309+
// Redirect the transport output to a buffer so we can verify every message lands
310+
ByteArrayOutputStream output = new ByteArrayOutputStream();
311+
PrintStream outputPrintStream = new PrintStream(output, true, StandardCharsets.UTF_8);
312+
transportProvider = new StdioServerTransportProvider(McpJsonDefaults.getMapper(), System.in, outputPrintStream);
313+
314+
// Capture the inner McpServerTransport handed to the session factory
315+
AtomicReference<McpServerTransport> transportRef = new AtomicReference<>();
316+
McpServerSession.Factory capturingFactory = transport -> {
317+
transportRef.set(transport);
318+
return mockSession;
319+
};
320+
321+
// Set session factory
322+
transportProvider.setSessionFactory(capturingFactory);
323+
324+
McpServerTransport transport = transportRef.get();
325+
assertThat(transport).isNotNull();
326+
327+
// Fan sendMessage out across 16 parallel rails to race against the unicast sink
328+
int messageCount = 500;
329+
Flux<Integer> concurrentSends = Flux.range(0, messageCount)
330+
.parallel(16)
331+
.runOn(Schedulers.parallel())
332+
.flatMap(i -> transport
333+
.sendMessage(
334+
new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION, "test/notification", Map.of()))
335+
.thenReturn(i))
336+
.sequential();
337+
338+
// Every send should complete successfully (no FAIL_NON_SERIALIZED errors)
339+
StepVerifier.create(concurrentSends).expectNextCount(messageCount).verifyComplete();
340+
341+
// Writes happen asynchronously on the outbound scheduler; wait briefly for drain
342+
// and verify every message was written as its own newline-delimited JSON line
343+
StepVerifier
344+
.create(Mono.delay(java.time.Duration.ofMillis(500))
345+
.then(Mono.fromCallable(() -> output.toString(StandardCharsets.UTF_8).lines().count())))
346+
.assertNext(lineCount -> assertThat(lineCount).isEqualTo(messageCount))
347+
.verifyComplete();
348+
}
349+
305350
}

0 commit comments

Comments
 (0)