spring-projects / spring-projects/spring-framework

Failure in @ExceptionHandler from IOException: The response may not be written to once it has been closed

Open
#36,421 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: web status: waiting-for-triage
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

In Spring 7 Webmvc, I am getting exceptions propagated to the Servlet container after an @ExceptionHandler tries to render a result but the connection is closed. I was hoping the DisconnectedClientHelper would identify and silence this scenario, i.e. return true from isClientDisconnectedException(ex).

An example exception is:

[2026-03-05 16:44:49.245] WARN  [http-nio-9082-exec-7570 ] .m.m.a.ExceptionHandlerExceptionResolver Failure in @ExceptionHandler net.solarnetwork.central.web.support.WebServiceGlobalControllerSupport#handleTransientDataAccessException(TransientDataAccessException, WebRequest, Locale)
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: The response may not be written to once it has been closed
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: The response may not be written to once it has been closed
	at org.springframework.http.converter.AbstractJacksonHttpMessageConverter.writeInternal(AbstractJacksonHttpMessageConverter.java:443)
	at org.springframework.http.converter.AbstractJacksonHttpMessageConverter.writeInternal(AbstractJacksonHttpMessageConverter.java:443)
	at org.springframework.http.converter.AbstractSmartHttpMessageConverter.write(AbstractSmartHttpMessageConverter.java:117)
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:343)
	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:207)
	at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:78)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:135)
	at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:460)
	at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:72)
	at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:176)
	at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:78)
	at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1227)
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1035)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:866)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1003)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:892)
...
Caused by: tools.jackson.core.exc.JacksonIOException: The response may not be written to once it has been closed
 at [No location information]
	at tools.jackson.core.exc.JacksonIOException.construct(JacksonIOException.java:39)
	at tools.jackson.core.JsonGenerator._wrapIOFailure(JsonGenerator.java:2476)
	at tools.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:2343)
	at tools.jackson.core.json.UTF8JsonGenerator.flush(UTF8JsonGenerator.java:1235)
	at tools.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:975)
	at org.springframework.http.converter.AbstractJacksonHttpMessageConverter.writeInternal(AbstractJacksonHttpMessageConverter.java:435)
	... 134 common frames omitted
Caused by: java.io.IOException: The response may not be written to once it has been closed
	at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:360)
	at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:341)
	at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:99)
	at org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:639)
	at org.springframework.util.StreamUtils$NonClosingOutputStream.write(StreamUtils.java:284)
	at tools.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:2341)
	... 137 common frames omitted

Or is there a way to configure such exceptions to not propagate to the Servlet container?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DisconnectedClientHelper.isClientDisconnectedException(ex) and the MVC exception-resolution path shown in the stack trace, especially ExceptionHandlerExceptionResolver. Reproduce the closed-response IOException scenario and compare how it is classified and propagated. Done means the issue's expected handling or configuration path is established and covered by an appropriate Spring MVC test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.