Bug 65001

Summary: HTTPNIO non-blocking IO servlet 3.1 API may fail to alert webapp that a request is complete
Product: Tomcat 9 Reporter: frrajott
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 9.0.41   
Target Milestone: -----   
Hardware: All   
OS: All   
Attachments: Test servlet

Description frrajott 2020-12-17 15:38:53 UTC
Created attachment 37624 [details]
Test servlet

Under certain IO error conditions, tomcat will fail to alert that an asynchronous request is completed and destroyed.
Both the AsyncListener's onComplete method and ServletRequestListener's requestDestroyed method are not called.
This may cause a webapp to believe that a request is still valid while tomcat has already recycled the objects.

I was able to reproduce this issue using a WriteListener (non-blocking IO servlet API 3.1) with the httpnio connector on the latest tomcat release (9.0.41).
This appears to be caused when an IOException is raised while attempting to write to the ServletOutputChannel during the WriteListener's onWritePossible callback.

It also seems possible to trigger this issue by manually throwing an exception in the onWritePossible callback.

The following exception can also be seen in the logs:
java.lang.IllegalStateException: Calling [asyncPostProcess()] is not valid for a request with Async state [ERROR]

I've attached a test servlet that demonstrates this issue.
To simulate IO errors, an http request is aborted before the full response body is read. The servlet then records various async callbacks it receives and it can be seen that some callbacks are not always called, i.e. the AsyncListener's onComplete callback and ServletRequestListener's requestDestroyed callback.
The servlet compares synchronous requests, asynchronous requests, and asynchronous requests with non-blocking IO.
Comment 1 Mark Thomas 2021-01-07 14:19:26 UTC
Thanks for the really good test case. I've found a (hopefully the) bug. I'm currently working on a fix.
Comment 2 Mark Thomas 2021-01-07 16:43:27 UTC
Thanks again for the test case. A good test case really does make a huge difference to how easily we can fix a bug.

Fixed in:
- 10.0.x for 10.0.1 onwards
- 9.0.x for 9.0.42 onwards
- 8.5.x for 8.5.62 onwards