Sometimes HttpServletRequest#asyncStart throws IllegalStateException

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Sometimes HttpServletRequest#asyncStart throws IllegalStateException

Nicolò Boschi
Hi all,

I faced a problem while using asynchronous computing of the HTTP requests
My code is really simple

AsyncContext asyncContext = request.startAsync(request, response);

If I interrupt the request from the client, I got


java.lang.IllegalStateException: Calling [asyncStart()] is not valid for a
request with Async state [ERROR]
at
org.apache.tomcat.util.net.NioSelectorPool.write(NioSelectorPool.java:152)
at
org.apache.coyote.AsyncStateMachine.asyncStart(AsyncStateMachine.java:242)
at
org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.doWrite(NioEndpoint.java:1257)
at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:497)
at org.apache.coyote.Request.action(Request.java:432)
at
org.apache.tomcat.util.net.SocketWrapperBase.doWrite(SocketWrapperBase.java:741)
at
org.apache.tomcat.util.net.SocketWrapperBase.writeBlocking(SocketWrapperBase.java:561)
at
org.apache.catalina.core.AsyncContextImpl.setStarted(AsyncContextImpl.java:323)
at org.apache.catalina.connector.Request.startAsync(Request.java:1690)
at
org.apache.tomcat.util.net.SocketWrapperBase.write(SocketWrapperBase.java:505)
at
org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1050)
at
org.apache.coyote.http11.Http11OutputBuffer$SocketOutputBuffer.doWrite(Http11OutputBuffer.java:538)
at
javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:402)


What is the best way to handle this case?
Is there any variable in somewhere (AsyncContext object?) to check before
calling asyncStart() ?
Otherwise, should I catch the IllegalStateException?

I'm using v9.0.39

Nicolò Boschi
Reply | Threaded
Open this post in threaded view
|

Re: Sometimes HttpServletRequest#asyncStart throws IllegalStateException

Mark Thomas-2
On 04/01/2021 13:27, Nicolò Boschi wrote:

> Hi all,
>
> I faced a problem while using asynchronous computing of the HTTP requests
> My code is really simple
>
> AsyncContext asyncContext = request.startAsync(request, response);
>
> If I interrupt the request from the client, I got
>
>
> java.lang.IllegalStateException: Calling [asyncStart()] is not valid for a
> request with Async state [ERROR]

<snip/>

> What is the best way to handle this case?

Very carefully. Writing correct async code is non-trivial.

> Is there any variable in somewhere (AsyncContext object?) to check before
> calling asyncStart() ?

No. It should not be necessary. As long as no error has been observed in
the synchronous processing it will be safe to call startAsync().

The use of an AsyncListener is essential to receive notification of some
issues (like timeouts) but you'll still need to catch and handle any
exceptions when reading or writing.

> Otherwise, should I catch the IllegalStateException?

No. IllegalStateException indicates a bug (probably an application bug
but possibly a Tomcat one).

For the error you describe to occur the request has to be in async mode
when an error occurs and then startAsync() is called. That should never
happen. There are lots of different ways this could occur. The most
likely cause is incomplete handling of async errors.

Are you sure you need to use async for your use case? Switching to sync
will at least simplify things making further fault finding easier.

> I'm using v9.0.39

I'd update to 9.0.41 - I don't recall any async specific issues but
there are a couple of bugs you'll want to make sure you avoid.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]