From 5434edd726f1f4a6c330f2a4ebf2e00841af0ed7 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 6 Feb 2024 16:46:04 +0100 Subject: [PATCH] Avoid sendError call when response committed already (Tomcat 10.1.16) Closes gh-32206 (cherry picked from commit 4ed337247ccd3e976cb544d831de023a1704c96d) --- .../DefaultHandlerExceptionResolver.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) 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 8dc9851bbe82..4067573c7e5a 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -464,8 +464,8 @@ protected ModelAndView handleErrorResponse(ErrorResponse errorResponse, response.sendError(status); } } - else { - logger.warn("Ignoring exception, response committed. : " + errorResponse); + else if (logger.isWarnEnabled()) { + logger.warn("Ignoring exception, response committed already: " + errorResponse); } return new ModelAndView(); @@ -524,14 +524,19 @@ protected ModelAndView handleTypeMismatch(TypeMismatchException ex, protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); + if (!response.isCommitted()) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST); + } + else if (logger.isWarnEnabled()) { + logger.warn("Ignoring exception, response committed already: " + ex); + } return new ModelAndView(); } /** * Handle the case where a * {@linkplain org.springframework.http.converter.HttpMessageConverter message converter} - * cannot write to an HTTP request. + * cannot write to an HTTP response. *

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. @@ -545,7 +550,12 @@ protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableExcept protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { - sendServerError(ex, request, response); + if (!response.isCommitted()) { + sendServerError(ex, request, response); + } + else if (logger.isWarnEnabled()) { + logger.warn("Ignoring exception, response committed already: " + ex); + } return new ModelAndView(); }