jsr369-experts@servlet-spec.java.net

[jsr369-experts] Re: [servlet-spec users] Clarification of threading requirements of section 6.2.3

From: Stuart Douglas <sdouglas_at_redhat.com>
Date: Tue, 09 Dec 2014 08:04:50 +1100

Mark Thomas wrote:
> Hi,
>
> Section 6.2.3 includes the following text:
> <quote>
> A Filter and the target servlet or resource at the end of the filter
> chain must execute in the same invocation thread.
> </quote>
>
> As a result of a bug raised against Apache Tomcat [1] I am seeking
> clarification of the meaning of that sentence.
>
> As written, the meaning seems unambiguous. However, does it apply when
> the request is in asynchronous mode? To put it another way is the
> following legal?
> - filter calls ServletRequest.startAsync()
> - filter calls AsyncContext.start(Runnable)
> - That Runnable called FilterChain.doFilter()
>

Another potential problem with doing this is that AsyncContext.start()
does not wait for the current call stack to return to the container (at
least it is not required to). This means that you will have two threads
potentially modifying the response, which is not thread safe.

We could modify the behaviour of AsyncContext.start() to wait for the
request to return to the container before actually running the task.

I think that this is actually a real thread safety problem in the
Servlet API, any code that uses AsyncContext.start() and then wants to
modify the response cannot be sure the original call stack has returned,
and could potentially have thread safety issues.

Stuart