java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

Spring JIRA | Victor Lyuboslavsky | 2 years ago
  1. 0

    The fix for SPR-11705 does not work with Tomcat 8.0.21 {code} public void sendError(int sc, String msg) throws IOException { copyBodyToResponse(); super.sendError(sc, msg); this.statusCode = sc; } {code} When {{copyBodyToResponse()}} is called, Tomcat considers the response committed. Then, when {{sendError}} is called, Tomcat throws {code} org.glassfish.jersey.server.ServerRuntime$Responder - Error while closing the output stream in order to commit response. java.lang.IllegalStateException: Cannot call sendError() after the response has been committed at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:462) at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:120) at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:120) at org.springframework.web.util.ContentCachingResponseWrapper.sendError(ContentCachingResponseWrapper.java:83) {code} Tomcat considers the response committed because contentLength is more than 0, and it is the same as contentWritten. From org/apache/catalina/connector/Response.java: {code} public boolean isAppCommitted() { return (this.appCommitted || isCommitted() || isSuspended() || ((getContentLength() > 0) && (getContentWritten() >= getContentLength()))); } {code} I propose that whenever content length > 0, sendError should not be called. My current workaround: {code} public class BackendContentCachingResponseWrapper extends ContentCachingResponseWrapper { public BackendContentCachingResponseWrapper(HttpServletResponse response) { super(response); } @Override public void sendError(int sc, String msg) throws IOException { try { super.sendError(sc, msg); } catch (IllegalStateException e) { // ignore } } } {code}

    Spring JIRA | 2 years ago | Victor Lyuboslavsky
    java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
  2. 0

    The fix for SPR-11705 does not work with Tomcat 8.0.21 {code} public void sendError(int sc, String msg) throws IOException { copyBodyToResponse(); super.sendError(sc, msg); this.statusCode = sc; } {code} When {{copyBodyToResponse()}} is called, Tomcat considers the response committed. Then, when {{sendError}} is called, Tomcat throws {code} org.glassfish.jersey.server.ServerRuntime$Responder - Error while closing the output stream in order to commit response. java.lang.IllegalStateException: Cannot call sendError() after the response has been committed at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:462) at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:120) at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:120) at org.springframework.web.util.ContentCachingResponseWrapper.sendError(ContentCachingResponseWrapper.java:83) {code} Tomcat considers the response committed because contentLength is more than 0, and it is the same as contentWritten. From org/apache/catalina/connector/Response.java: {code} public boolean isAppCommitted() { return (this.appCommitted || isCommitted() || isSuspended() || ((getContentLength() > 0) && (getContentWritten() >= getContentLength()))); } {code} I propose that whenever content length > 0, sendError should not be called. My current workaround: {code} public class BackendContentCachingResponseWrapper extends ContentCachingResponseWrapper { public BackendContentCachingResponseWrapper(HttpServletResponse response) { super(response); } @Override public void sendError(int sc, String msg) throws IOException { try { super.sendError(sc, msg); } catch (IllegalStateException e) { // ignore } } } {code}

    Spring JIRA | 2 years ago | Victor Lyuboslavsky
    java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
  3. 0

    An established connection was aborted by the software in your host machine tomcat jackson

    Stack Overflow | 2 years ago | Piotr Tempes
    java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Tomcat CSRF Filter configuration

    Stack Overflow | 2 years ago | Nilesh
    java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
  6. 0

    Geometry from vividsolutions JTS fails when creating JSON

    Stack Overflow | 2 years ago | finnetrolle
    java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

  1. tvrmsmith 114 times, last 1 month ago
  2. Andreas Häber 1 times, last 6 months ago
29 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. java.lang.IllegalStateException

    Cannot call sendError() after the response has been committed

    at org.apache.catalina.connector.ResponseFacade.sendError()
  2. Glassfish Core
    ResponseFacade.sendError
    1. org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:462)
    1 frame
  3. JavaServlet
    HttpServletResponseWrapper.sendError
    1. javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:120)
    2. javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:120)
    2 frames
  4. Spring
    ContentCachingResponseWrapper.sendError
    1. org.springframework.web.util.ContentCachingResponseWrapper.sendError(ContentCachingResponseWrapper.java:83)
    1 frame