From ae7891e7973ed940c9f49ef2b97deccf25e47fc8 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 3 Jun 2026 14:15:03 +0100 Subject: [PATCH] Revise disconnected client error handling in WebFlux A disconnected client error does not necessarily prevent us from setting the status of the WebFlux ServerHttpResponse, which is only gated by a committed flag and does not necessarily reflect the connection state. This is why we need to check if we have a disconnected client error first and handle it accordingly. We still set the response to 500 in case the disconnect client error is to a remote host in which case it will propagate to the client. Closes gh-36811 --- .../web/server/adapter/HttpWebHandlerAdapter.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java b/spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java index c34f872243a..125a38816ed 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java @@ -362,15 +362,14 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa ServerHttpResponse response = exchange.getResponse(); String logPrefix = exchange.getLogPrefix(); - // Sometimes a remote call error can look like a disconnected client. - // Try to set the response first before the "isDisconnectedClient" check. - - if (response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR)) { - logger.error(logPrefix + "500 Server Error for " + formatRequest(request), ex); + if (disconnectedClientHelper.checkAndLogClientDisconnectedException(ex)) { + // Attempt to send 500 in case of onward (rather than client) connection issue + response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); + observationContext.setConnectionAborted(true); return Mono.empty(); } - else if (disconnectedClientHelper.checkAndLogClientDisconnectedException(ex)) { - observationContext.setConnectionAborted(true); + else if (response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR)) { + logger.error(logPrefix + "500 Server Error for " + formatRequest(request), ex); return Mono.empty(); } else {