From 33988a4621349b8522e94b2eadde4f28a2844065 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 15 Sep 2026 16:26:37 +0100 Subject: [PATCH] Refine lost connection checks in DefaultHandlerExceptionResolver This commit adds additional "disconnected client" checks for HttpMessageNotReadableException and HttpMessageNotWriteableException, both of which wrap I/O errors and could be due to a lost connection. Closes gh-37151 --- .../mvc/support/DefaultHandlerExceptionResolver.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java index 2e73da9b6de..9f264e70ee4 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java @@ -619,6 +619,8 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes *

The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}. * Alternatively, a fallback view could be chosen, or the HttpMessageNotReadableException could be * rethrown as-is. + *

If the root cause suggests a "lost connection", handling is delegated instead to + * {@link #handleDisconnectedClientException(Exception, HttpServletRequest, HttpServletResponse, Object)}. * @param ex the HttpMessageNotReadableException to be handled * @param request current HTTP request * @param response current HTTP response @@ -629,6 +631,10 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { + if (DisconnectedClientHelper.isClientDisconnectedException(ex)) { + return handleDisconnectedClientException(ex, request, response, handler); + } + if (!response.isCommitted()) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); } @@ -645,6 +651,8 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes *

The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. * Alternatively, a fallback view could be chosen, or the HttpMessageNotWritableException could * be rethrown as-is. + *

If the root cause suggests a "lost connection", handling is delegated instead to + * {@link #handleDisconnectedClientException(Exception, HttpServletRequest, HttpServletResponse, Object)}. * @param ex the HttpMessageNotWritableException to be handled * @param request current HTTP request * @param response current HTTP response @@ -655,6 +663,10 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { + if (DisconnectedClientHelper.isClientDisconnectedException(ex)) { + return handleDisconnectedClientException(ex, request, response, handler); + } + if (!response.isCommitted()) { sendServerError(ex, request, response); }