When using Comet Servlet 3 Push of ZK 6.5.1.1 EE on GlassFish 3.1.2 an IllegalStateException is thrown by Grizzly.
[#|2013-07-24T15:28:56.897+0200|WARNING|glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=22;_ThreadName=Thread-2;|StandardWrapperValve[CometAsyncServlet]: PWC1406: Servlet.service() for servlet CometAsyncServlet threw exception java.lang.IllegalStateException: Not Suspended at com.sun.grizzly.tcp.Response.resume(Response.java:768) at org.apache.catalina.connector.Request.asyncComplete(Request.java:3993) at org.apache.catalina.connector.AsyncContextImpl.complete(AsyncContextImpl.java:242) at org.zkoss.zkmax.ui.comet.CometAsyncServlet$AsyncInfo.complete(CometAsyncServlet.java:153) at org.zkoss.zkex.ui.comet.CometServerPush.cleanAsyncInfo(CometServerPush.java:316) at org.zkoss.zkex.ui.comet.CometServerPush.processRequest(CometServerPush.java:248) at org.zkoss.zkmax.ui.comet.CometServerPush.processRequest(CometServerPush.java:107) at org.zkoss.zkmax.ui.comet.CometAsyncServlet.doPost(CometAsyncServlet.java:117) at javax.servlet.http.HttpServlet.service(HttpServlet.java:688) at javax.servlet.http.HttpServlet.service(HttpServlet.java:770) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:662) |#]
The exception is thrown when the following steps happen:
First request:
CometAsyncServlet.doPost() is called which starts a new AsyncContext. This calls com.sun.grizzly.tcp.Response.suspend() on instance A. Back in CometAsyncServlet, CometServerPush.processRequest() is called which leads to Response.resume() is called on instance A. Everything works fine.
Second request:
CometAsyncServlet.doPost() is called which starts a new AsyncContext. This calls com.sun.grizzly.tcp.Response.suspend() on instance B. Back in CometAsyncServlet, CometServerPush.processRequest() is called. AsyncInfo's complete() method is called, which leads to Response.resume() beeing called for instance A. But this method has been called before during the first request, so the Grizzly implementation throws an IllegalStateException, which leads to a call of Response.reset() on instance B. Afterwards Response.resume() is called on instance B, which again throws an IllegalStateException.
In my opinion the best solution to this problem is to catch the IllegalStateException in AsyncInfo.complete() method.