You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Adwait Kumar Singh <ad...@gmail.com> on 2023/11/25 05:30:11 UTC

Possible way to avoid Tomcat from recycling the request/response on error?

Hi,

I am trying to build a Streaming solution on top of Tomcat and using Async
servlets. What I am doing is I start an asynchronous process using
`startAsync()` and then attach a ReadListener which streams the data
forward `onDataAvailable`, the consumer consumes this on another thread.
Now the problem I am running into is if the there is a
SocketTimeoutException during the read, onError of ReadListener is
triggered and I pass that forward, however I don't call
AsyncContext.complete, because I want the async thread processing the data
to be first able to deal with the exception and then I close it on the
async thread when its done.

However what I am observing is irrespective of if my not closing the
context, it's closed by default by Tomcat and the request/response objects
are recycled. Due to this during the error handling when I am trying to
read some headers I get a IllegalStateException. I don't want to do the
clean up on the IO thread since it can be heavy and I don't want to
overwhelm the IO threads.

Is there a way around this, to keep the async context open even on an error
and not close it till complete is invoked?

Thanks,
Adwait.

Re: Possible way to avoid Tomcat from recycling the request/response on error?

Posted by Mark Thomas <ma...@apache.org>.
On 27/11/2023 01:49, Adwait Kumar Singh wrote:
> Hmm, this gives me an impression that the Servlet APIs expect the
> request/response processing to *always *happen on the container thread.
> If I attempt to perform it on a non-container thread after making the
> request async, I run into the risk of the Request/Response objects being
> recycled without my non-container thread being aware of it or having to
> block my container thread.

The concurrency requirements for asynchronous processing are set out in 
section 2.3.3.4 of the Servlet specification.

Implementing Error handling is significantly more complicated with 
asynchronous servlets but it boils down to avoid accessing the request, 
response and associated objects after complete()/dispatch() have been 
called.

Mark

> 
> On Sat, Nov 25, 2023 at 5:42 AM Mark Thomas <ma...@apache.org> wrote:
> 
>> On 25/11/2023 05:30, Adwait Kumar Singh wrote:
>>
>>> Is there a way around this, to keep the async context open even on an
>> error
>>> and not close it till complete is invoked?
>>
>> No. The spec requires the error handler to call complete() in onError()
>> and error handler doesn't, the container must.
>>
>> Mark
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Possible way to avoid Tomcat from recycling the request/response on error?

Posted by Adwait Kumar Singh <ad...@gmail.com>.
Hmm, this gives me an impression that the Servlet APIs expect the
request/response processing to *always *happen on the container thread.
If I attempt to perform it on a non-container thread after making the
request async, I run into the risk of the Request/Response objects being
recycled without my non-container thread being aware of it or having to
block my container thread.

On Sat, Nov 25, 2023 at 5:42 AM Mark Thomas <ma...@apache.org> wrote:

> On 25/11/2023 05:30, Adwait Kumar Singh wrote:
>
> > Is there a way around this, to keep the async context open even on an
> error
> > and not close it till complete is invoked?
>
> No. The spec requires the error handler to call complete() in onError()
> and error handler doesn't, the container must.
>
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Possible way to avoid Tomcat from recycling the request/response on error?

Posted by Mark Thomas <ma...@apache.org>.
On 25/11/2023 05:30, Adwait Kumar Singh wrote:

> Is there a way around this, to keep the async context open even on an error
> and not close it till complete is invoked?

No. The spec requires the error handler to call complete() in onError() 
and error handler doesn't, the container must.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org