Skip to content

Commit 235f6bd

Browse files
committed
HttpClientStreamableTransport: fix race condition on 404 response
- There was a race condition where a 404 to a sendMessage call triggered client-side session closing. This would ultimately close the reactive chain representing HTTP connections, including the POST for the sendMessage itself if it had not completed by then. This would cancel the Mono instead of propagating the error. - This was a race condition because before closing the connection, the DefaultMcpTransportSession needed to send a DELETE to the server, and waited for the response. Cancelling happened only when DELETE finished before sendMessage() finished, so it would not show often. - This materialized in CI as errors for `test404WithSessionId`. Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent 0e1a78f commit 235f6bd

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,13 @@ else if (contentType.contains(APPLICATION_JSON)) {
590590
// server does not recognise is already invalidated by the time
591591
// the caller learns about it. Consumed here so that it is not
592592
// handled a second time below.
593+
// Detached from the session first: invalidating the session
594+
// closes it, which would otherwise dispose this very exchange and
595+
// complete sendMessage() through doOnCancel below.
596+
Disposable ref = disposableRef.getAndSet(null);
597+
if (ref != null) {
598+
transportSession.removeConnection(ref);
599+
}
593600
handleExceptionSafely(first.getThrowable());
594601
deliveredSink.error(first.getThrowable());
595602
return Flux.empty();

0 commit comments

Comments
 (0)