Reputation: 2117
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.
Upvotes: 0
Views: 1895
Reputation: 20862
Since this is a resource-management problem (you have a thread holding an InputStream
too long), you should be able to fix that: have the servlet that launched the thread (or submitted the task to an Executor
... you are using any Executor
for this, right?) wait for the thread to finish (or the Future
to 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.
Upvotes: 1
Reputation: 2117
update:
turned out that my spawned off Thread (which obviously had a reference to the Input and OutputStreams) did not release those references in time always. Thanks, Christopher, for the pointer!
While I agree that caching these objects is very bad practice and one should never do that, it is quite a disappointment that Tomcat does react so poorly to it. I'd expect better of an app server of that maturity...
Upvotes: 0