Streamable HTTP: error replies from synchronous handlers are sometimes lost (the worker writing the reply is interrupted mid-flush)
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 42/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- java, spring, spring-boot
- Bereich
- backend, networking
Rechercherichtung
Start with McpServerFeatures, McpStreamableServerSession.handle, and WebMvcStreamableServerTransportProvider.handlePost, then trace the Mono.fromCallable(...).subscribeOn(...) error path described in the issue. Unpack and run the attached lost-reply Gradle project with its 16-thread JUnit test and -Dlostreply.assertNone=true. Done means every throwing-handler request receives a complete JSON-RPC error reply with no retries or lost responses, supported by a deterministic regression test.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Related: reactor-core issue https://github.com/reactor/reactor-core/issues/4483 (root cause).
Component: synchronous server features, run as Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()) (McpServerFeatures, prompt and tool fromSync), with the reply written inline on that worker (McpStreamableServerSession handle → onErrorResume(...) → flatMap(connection::sendMessage)), served by the Spring WebMVC Streamable-HTTP transport WebMvcStreamableServerTransportProvider, which subscribes with block() on the servlet container thread. That transport ships in org.springframework.ai:mcp-spring-webmvc (tested 2.0.2 and 2.1.0-RC2); up to 0.18.5 it shipped as io.modelcontextprotocol.sdk:mcp-spring-webmvc.
Versions seen: mcp-core 2.1.0 with Spring AI 2.0.2 on Spring Boot 4.1.2; Spring AI 2.1.0-RC2 on Spring Boot 4.2.0-RC2; SDK 0.18.5 on Spring Boot 4.0.6, 4.0.9 and 4.1.2; reactor-core 3.8.5 and 3.8.8 (and, by the Spring Boot 4.2.0-RC2 BOM, 3.9.0-RC1 [I]); mcp-core 2.2.0-M2 in the Spring AI 2.1.0-RC2 row; JDK 25. The relevant mcp-core code is the same in 0.18.5, 2.1.0 and 2.2.0.
Labels: [V] verified by a run or by reading the sources; [I] inferred.
Summary
When a synchronous prompt (or tool) handler throws, the server answers with a JSON-RPC error. Under concurrency, a fraction of those error replies never reach the client: the connection closes with zero bytes, or after the response headers and before the terminating chunk. For each zero-byte loss the server logs, from the transport's sendMessage:
Failed to send message to session <id>: ServletResponse failed to flushBuffer: java.io.IOException: The current thread was interrupted
on a boundedElastic thread [V]. Replies from handlers that return normally were not lost [V].
Cause [V, traced]
The thread writing the reply is interrupted by a race in reactor-core's subscribeOn for callables (reactor-core issue linked above, with a Reactor-only reproduction):
- The handler throws on the
boundedElasticworker. Reactor'sCallableSubscribeOnSubscription.runcancels its own subscription (Operators.onOperatorError) and then signalsonErrordownstream on the same worker. The SDK'sonErrorResumebuilds the error reply, andsendMessagewrites and flushes it, still on that worker. - Meanwhile the servlet container thread, inside
block()inWebMvcStreamableServerTransportProvider.handlePost, finishesMonoSubscribeOnCallable.subscribe.setMainFuturefinds the subscription cancelled and disposes the task withFuture.cancel(true)(SchedulerTask.dispose), which interrupts the worker. - If the interrupt lands during the flush, the flush throws,
sendMessagelogs the error above and callssseBuilder.error(e), and the client gets a closed connection.
An opt-in trace in the attached project records both stacks. The interrupting one, on the container thread [V]:
SchedulerTask.dispose(SchedulerTask.java:130)
FluxSubscribeOnCallable$CallableSubscribeOnSubscription.setMainFuture(FluxSubscribeOnCallable.java:201)
MonoSubscribeOnCallable.subscribe(MonoSubscribeOnCallable.java:53)
... MonoUsingWhen.subscribe ...
Mono.block(Mono.java:1772)
WebMvcStreamableServerTransportProvider.lambda$handlePost$18(WebMvcStreamableServerTransportProvider.java:454) (0.18.5; Spring AI 2.0.2: lambda$handlePost$19(...java:507))
SseServerResponse.writeToInternal(...)
The losses that end after the headers have no matching server log line; [I] they are probably the same interrupt hitting a later write or the final flush in close(), where the exception is not logged.
Reproduction (attached: lost-reply, a standalone Gradle project with one JUnit test)
A Spring Boot application hosts the transport at /mcp, with one prompt that has a required argument. 16 sessions (one per thread) send prompts/get with the argument missing, so the handler throws and the server answers with an in-process JSON-RPC error. HTTP/1.1, Accept: application/json, text/event-stream, the Mcp-Session-Id header, a unique request id; no retry; a reply that does not arrive complete counts as lost. Two client modes: a new JDK HttpClient per request, and a new plain Socket per request (Connection: close, read to end of stream). -Pvariant=old|current and -PbootVersion=... select the dependencies; -Dlostreply.outcome=result supplies the argument (control); -Dlostreply.traceInterrupts=true records the stacks.
Results [V] (10,000 requests per run, 16 threads, JDK 25; the rate varies with machine and load, so treat it as an order of magnitude):
| Transport | Spring Boot (Spring Framework, Tomcat) | Client | Lost / attempts | Server "interrupted" lines |
|---|---|---|---|---|
| 0.18.5 | 4.0.6 (7.0.7, 11.0.21) | HttpClient |
1443 / 30,000 (4.8%) | 1410, equal to the zero-byte losses |
| 0.18.5 | 4.0.6 | socket | 434 / 20,000 (2.2%) | 406, equal to the zero-byte losses |
| 0.18.5 | 4.0.9 (7.0.10, 11.0.27) | HttpClient |
22 / 30,000 (0.07%) | 22 |
| 0.18.5 | 4.0.9 | socket | 15 / 20,000 (0.08%) | 13 |
| Spring AI 2.0.2 | 4.1.2 (7.0.10, 11.0.27) | HttpClient |
21 / 30,000 (0.07%) | 18 |
| Spring AI 2.0.2 | 4.1.2 | socket | 14 / 20,000 (0.07%) | 13 |
| Spring AI 2.1.0-RC2 | 4.2.0-RC2 (7.1.0-RC2, 11.0.27) | HttpClient / socket |
11 and 5 / 10,000 (one run each) | 11 and 5 |
| control (handler returns): 0.18.5 / Spring AI 2.0.2 | 4.0.6 / 4.1.2 | HttpClient |
0 / 10,000 each | 0 |
Most rows were run before the project gained its ok/unexpected counters and the renamed failure kind; the loss logic is unchanged, and no run recorded an unexpected status. In every run the server "interrupted" line count equals the client's zero-byte losses; the rest of the losses ended after the headers. Newer Spring Framework makes the window much smaller (about 66 times fewer losses with HttpClient and about 29 times with the socket from Spring Boot 4.0.6 to 4.0.9), but does not close it. Why Spring Framework 7.0.10 helps is not known [I: it probably changes how long the worker's flush takes relative to the container thread's setMainFuture].
Possible mitigations in the SDK [I, none tested]
- Catch the handler's exception inside the callable passed to
Mono.fromCallableand turn it into the error result there, so the callable does not throw and Reactor does not cancel the subscription (step 1 above never happens). - Or do not write the reply on the
subscribeOnworker (for examplepublishOnanother scheduler beforesendMessage). - For users:
immediateExecution(true)on the synchronous server runs handlers withoutsubscribeOn, which should avoid the race, at the cost of running handlers on the calling thread.
Ask
- Would the maintainers take one of the mitigations above while the Reactor issue is open?
- Would they accept a regression test along the lines of the attached project (a loopback server, 16 threads, a throwing handler, no retry, assert zero lost replies;
-Dlostreply.assertNone=true)? A client that retries hides the problem, so the test must count losses with no retry.
- Vorherrschende Sprache
- Java
- Sterne
- 3.8k
- Forks
- 1.1k
- Ø Merge
- 1 T. 3 Std.
- Gemergte PRs (30 T.)
- 38
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus modelcontextprotocol/java-sdk
-
Modern `HttpServletMcpTransport` does not check `Mcp-Name` when `params.name` or `params.uri` is not a JSON stringEvtl. vergeben @starryeye hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
modelcontextprotocol/java-sdk#1352 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Unknown tool error always says "Unknown tool: invalid_tool_name" instead of the name that was calledOffen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 85/100
modelcontextprotocol/java-sdk#1346 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
modelcontextprotocol/java-sdk#1345 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Tool result _meta is lost when the server automatically adds text content for structured outputOffenarea/server legacy P3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
modelcontextprotocol/java-sdk#1330 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Client request handlers that complete empty send no JSON-RPC responseEvtl. vergeben @1fanwang hat das vor 83 Tagen übernommen. Offenarea/client bug P2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
modelcontextprotocol/java-sdk#1124 · 6 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in modelcontextprotocol/java-sdk
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
-
crash
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
-
account bug course exam iris lecture
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag