Skip to content

Commit 4d9e11b

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 open 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 4d9e11b

1 file changed

Lines changed: 44 additions & 14 deletions

File tree

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

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -257,22 +257,46 @@ public void setExceptionHandler(Consumer<Throwable> handler) {
257257
}
258258

259259
private void handleException(Throwable t) {
260-
logger.debug("Handling exception for session {}", sessionIdOrPlaceholder(
261-
activeSession.get() != null ? activeSession.get().sessionId() : Optional.empty()), t);
262-
if (t instanceof McpTransportSessionNotFoundException) {
263-
McpTransportSession<?> invalidSession = this.activeSession.getAndSet(createTransportSession());
264-
logger.warn("Server does not recognize session {}. Invalidating.", invalidSession.sessionId());
265-
invalidSession.close();
260+
handleException(t, () -> {
261+
});
262+
}
263+
264+
/**
265+
* Handles {@code t}, running {@code beforeClose} once a session the server does not
266+
* recognise has been replaced and the exception handler notified, but before that
267+
* session is closed. Closing it disposes all of its open exchanges, which can include
268+
* the very exchange that reported {@code t}.
269+
*/
270+
private void handleException(Throwable t, Runnable beforeClose) {
271+
McpTransportSession<?> invalidSession = null;
272+
try {
273+
logger.debug("Handling exception for session {}", sessionIdOrPlaceholder(
274+
activeSession.get() != null ? activeSession.get().sessionId() : Optional.empty()), t);
275+
if (t instanceof McpTransportSessionNotFoundException) {
276+
invalidSession = this.activeSession.getAndSet(createTransportSession());
277+
logger.warn("Server does not recognize session {}. Invalidating.", invalidSession.sessionId());
278+
}
279+
Consumer<Throwable> handler = this.exceptionHandler.get();
280+
if (handler != null) {
281+
handler.accept(t);
282+
}
266283
}
267-
Consumer<Throwable> handler = this.exceptionHandler.get();
268-
if (handler != null) {
269-
handler.accept(t);
284+
finally {
285+
beforeClose.run();
286+
if (invalidSession != null) {
287+
invalidSession.close();
288+
}
270289
}
271290
}
272291

273292
private void handleExceptionSafely(Throwable t) {
293+
handleExceptionSafely(t, () -> {
294+
});
295+
}
296+
297+
private void handleExceptionSafely(Throwable t, Runnable beforeClose) {
274298
try {
275-
handleException(t);
299+
handleException(t, beforeClose);
276300
}
277301
catch (Exception e) {
278302
logger.error("Error handling exception {}", t.getMessage(), e);
@@ -588,10 +612,16 @@ else if (contentType.contains(APPLICATION_JSON)) {
588612
if (first.isOnError()) {
589613
// Handled before failing sendMessage(), so that a session the
590614
// server does not recognise is already invalidated by the time
591-
// the caller learns about it. Consumed here so that it is not
592-
// handled a second time below.
593-
handleExceptionSafely(first.getThrowable());
594-
deliveredSink.error(first.getThrowable());
615+
// the caller learns about it. sendMessage() fails before that
616+
// session is closed though: closing it disposes this exchange,
617+
// which would complete sendMessage() through doOnCancel below.
618+
// Consumed here so that it is not handled a second time below.
619+
Throwable error = first.getThrowable();
620+
handleExceptionSafely(error, () -> {
621+
if (error != null) {
622+
deliveredSink.error(error);
623+
}
624+
});
595625
return Flux.empty();
596626
}
597627
deliveredSink.success();

0 commit comments

Comments
 (0)