I have built a streaming relay server using Tomcat.
Simple idea is: one client does a POST, another client does a GET. The servlet spawns off a Thread and does a simple byte shuffling until the InputSteam (from the POST) is empty. When done closes / answers both requests. All is fine and it works perfectly, BUT:
Tomcat seems to reuse request objects and even the InputStream objects! Every 10th POST or so, the InputStream can’t be read because already closed. Having a closer look at the logs I realize that the exact same InputStream object was used (and thus closed) by an earlier request. Turns out even the HttpServletRequest object is the exact same.
What is going on here? Why is Tomcat reusing objects which obviously haven’t been reset properly? I’ve tried it with version 7.0.29 and 6.0.16, same thing.
Since this is a resource-management problem (you have a thread holding an
InputStreamtoo long), you should be able to fix that: have the servlet that launched the thread (or submitted the task to anExecutor… you are using anyExecutorfor this, right?) wait for the thread to finish (or theFutureto complete) and release its resources. If you post the code to your byte-pump thread (and relevant parts of the servlet(s)), I can show you how to improve them.