0 Replies Latest reply on Jun 14, 2012 9:25 AM by Randall Casey

    Response already closed when starting async mode in servlet

    Randall Casey Newbie

      Hi all,

       

      I'm experiencing problems with starting asynchronous mode in my servlets (all are affected by this), running 7.1.1.Final. Every once in a while, maybe 1 in 20 calls, it throws an "IllegalStateException: Response has been closed already" when I call the startAsync method. There are no filters on either the request or response. The full (anonymised) stack trace is as follows:

      09:08:08,155 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/XXXWeb].[xxx.servlets.SetDataSelection2]] (http-ec2-XXX.eu-west-1.compute.amazonaws.com-XXX-8080-5) Servlet.service() for servlet xxx.servlets.SetDataSelection2 threw exception: java.lang.IllegalStateException: Response has been closed already

              at org.apache.catalina.connector.Request.startAsync(Request.java:3207) [jbossweb-7.0.13.Final.jar:]

              at org.apache.catalina.connector.Request.startAsync(Request.java:3197) [jbossweb-7.0.13.Final.jar:]

              at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:990) [jbossweb-7.0.13.Final.jar:]

              at xxx.servlets.SetDataSelection2.handleAsync(SetDataSelection2.java:85) [classes:]

              at xxx.servlets.SetDataSelection2.doPost(SetDataSelection2.java:73) [classes:]

              at xxx.servlets.SetDataSelection2.doGet(SetDataSelection2.java:60) [classes:]

              at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

              at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]

              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]

              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]

              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]

              at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]

              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]

              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]

              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]

              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]

              at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]

              at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]

              at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:925) [jbossweb-7.0.13.Final.jar:]

              at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_27]

       

      The relevant bits of code are as follows. As you can see, nothing much happens before I try to call startAsync:


      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


      doPost(request,response);

      }






      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      ++counter;
      System.out.println("Processing request " + counter + " on thread " + Thread.currentThread().getId() + ":" + Thread.currentThread().getName());

      handleAsync(request, response);
      System.out.println("Ready for next request");
      }

      private void handleAsync(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      AsyncContext asyncCtx = request.startAsync();



      // rest of method

      }

       

       

      I thought that it was maybe the caller closing the connection or something, but the caller receives a 500 http status code, indicating a server fault. Also, when I try a non-asynchronous version of the same code, this does not show the problem and is able to send back a valid response every time.

       

      I've been trying different things over the past few days, but I'm stumped. Anybody experienced something like this? Any pointers to how I can try to debug what's going on?

       

      Thanks,

        Randall