java.lang.IllegalStateException: Cannot forward after response has been committed

Spring JIRA | Andreas Kluth | 2 years ago
  1. 0

    When the following code snippet is executed on Tomcat 7.0.59/8.0.20 or Jetty 9.2.9 instead of an error page either nothing (Tomcat) or a "Service Unavailable" (Jetty) page is displayed. Instead of the above behaviour the default (Spring Boot) white-label error page should be handling the faulty request, however fails at: {{org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration.SpelView.render(Map<String, ?>, HttpServletRequest, HttpServletResponse)}} I observed a similar issue: https://github.com/spring-projects/spring-boot/issues/1920 which was fixed in tomcat; however this scenario seems to be Spring Web MVC specific. h3. Sample Source {code} @Controller @EnableAutoConfiguration public class Application { @RequestMapping("/") @ResponseBody DeferredResult<String> home() { DeferredResult<String> result = new DeferredResult<>(2000); ForkJoinPool.commonPool().execute(() -> { doHeavyWork(result); }); return result; } private void doHeavyWork(DeferredResult<String> result) { try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.interrupted(); } result.setResult("Hello World"); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } } {code} h3. Stacktrace Jetty: {code} org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: STREAM at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:595) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.Server.handleAsync(Server.java:553) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347) at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:261) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.IllegalStateException: STREAM at org.eclipse.jetty.server.Response.getWriter(Response.java:920) at org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView.render(ErrorMvcAutoConfiguration.java:196) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) ... 21 common frames omitted {code} h3. Tomcat 8.0.20 {code} java.lang.IllegalStateException: Cannot forward after response has been committed at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:328) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318) at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:439) at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:305) at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:399) at org.apache.catalina.core.AsyncContextImpl.setErrorState(AsyncContextImpl.java:434) at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:289) at org.apache.coyote.http11.AbstractHttp11Processor.asyncDispatch(AbstractHttp11Processor.java:1705) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:650) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source) {code}

    Spring JIRA | 2 years ago | Andreas Kluth
    java.lang.IllegalStateException: Cannot forward after response has been committed
  2. 0

    When the following code snippet is executed on Tomcat 7.0.59/8.0.20 or Jetty 9.2.9 instead of an error page either nothing (Tomcat) or a "Service Unavailable" (Jetty) page is displayed. Instead of the above behaviour the default (Spring Boot) white-label error page should be handling the faulty request, however fails at: {{org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration.SpelView.render(Map<String, ?>, HttpServletRequest, HttpServletResponse)}} I observed a similar issue: https://github.com/spring-projects/spring-boot/issues/1920 which was fixed in tomcat; however this scenario seems to be Spring Web MVC specific. h3. Sample Source {code} @Controller @EnableAutoConfiguration public class Application { @RequestMapping("/") @ResponseBody DeferredResult<String> home() { DeferredResult<String> result = new DeferredResult<>(2000); ForkJoinPool.commonPool().execute(() -> { doHeavyWork(result); }); return result; } private void doHeavyWork(DeferredResult<String> result) { try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.interrupted(); } result.setResult("Hello World"); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } } {code} h3. Stacktrace Jetty: {code} org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: STREAM at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:595) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.Server.handleAsync(Server.java:553) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347) at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:261) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.IllegalStateException: STREAM at org.eclipse.jetty.server.Response.getWriter(Response.java:920) at org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView.render(ErrorMvcAutoConfiguration.java:196) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) ... 21 common frames omitted {code} h3. Tomcat 8.0.20 {code} java.lang.IllegalStateException: Cannot forward after response has been committed at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:328) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318) at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:439) at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:305) at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:399) at org.apache.catalina.core.AsyncContextImpl.setErrorState(AsyncContextImpl.java:434) at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:289) at org.apache.coyote.http11.AbstractHttp11Processor.asyncDispatch(AbstractHttp11Processor.java:1705) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:650) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source) {code}

    Spring JIRA | 2 years ago | Andreas Kluth
    java.lang.IllegalStateException: Cannot forward after response has been committed
  3. 0

    DeferredResult with time consuming processing resulting in exception

    Stack Overflow | 2 years ago | Dinesh Shende
    java.lang.IllegalStateException: Cannot forward after response has been committed
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Unexpected async behavior: Springs's @Async vs RxJava

    Stack Overflow | 1 year ago | baymon
    java.lang.IllegalStateException: Cannot forward after response has been committed
  6. 0

    Full Text Bug Listing

    apache.org | 3 months ago
    java.lang.IllegalStateException: Cannot forward after response has been committed

    2 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 forward after response has been committed

      at org.apache.catalina.core.ApplicationDispatcher.doForward()
    2. Glassfish Core
      CoyoteAdapter.asyncDispatch
      1. org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:328)
      2. org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
      3. org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:439)
      4. org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:305)
      5. org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:399)
      6. org.apache.catalina.core.AsyncContextImpl.setErrorState(AsyncContextImpl.java:434)
      7. org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:289)
      7 frames
    3. Grizzly HTTP
      NioEndpoint$SocketProcessor.run
      1. org.apache.coyote.http11.AbstractHttp11Processor.asyncDispatch(AbstractHttp11Processor.java:1705)
      2. org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:650)
      3. org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
      4. org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
      5. org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
      5 frames
    4. Java RT
      ThreadPoolExecutor$Worker.run
      1. java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      2. java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      2 frames
    5. Tomcat Util
      TaskThread$WrappingRunnable.run
      1. org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      1 frame
    6. Java RT
      Thread.run
      1. java.lang.Thread.run(Unknown Source)
      1 frame