tags:

views:

335

answers:

5

Today I fixed a bug in an application that might have lead to an endless loop in a servlet request/response cycle.

So just out of curiousity: What happens, if my servlet actually gets trapped in a for(;;) loop?

Is it somehow possible to recover? Will tomcat detect this? Can this instance be killed without restarting the server?

Or is this one of the worst things that can happen and a very quick way to kill a webcontainer?

EDIT: It was a true endless loop consuming CPU all the time but not memory. I kept it running for a few minutes. I think, I can confirm that tomcat will not detect this kind of thing :-)

+1  A: 

Not sure if Tomcat has such detection, but e.g Websphere's web container does. However, it obviously takes the container a relatively long time to detect a "hung" thread. Server under a load can be easily and quickly killed by such code.

david a.
Hey - good to know, that websphere detects something like this. Thanks for the info
Mo
+1  A: 

I don't think that Tomcat will detect an infinite loop. You might be able to stop the servlet using the Tomcat manager, if the servlet is not consuming all the CPU time with its loop. Otherwise, it's probably safest and easiest to just restart the server.

This is why you do extensive tests locally before deploying your apps ;-) and be very careful that all your loops have exit conditions...

David Zaslavsky
Let's say this bug was detected before it caused production trouble ;-)
Mo
good! And I certainly didn't mean to sound condescending at all - I'm sure you know the importance of testing.
David Zaslavsky
Don't worry - you didn't
Mo
+1  A: 

if the endless loop is caused by an endless redirect, it will sooner or later terminate that request with StackOverflowException.

it its a "true" endless loop, you might hog one CPU/core permanently and eventually crash the whole app with OutOfMemoryException.

tomcat has, as far as i know no explicit detection for these problems.

Andreas Petersson
A: 

I would imagine that you could handle this with an appropriate timeout setting:

http://tomcat.apache.org/connectors-doc/generic_howto/timeouts.html

duffymo
Hm... Are you sure? I read over the document you linked and I don't see how those timers could detect a "hung" (basically live-locked) thread. Please correct me if I'm wrong!
Mo
The timeouts duffymo linked to are for the JK connector, which connects Tomcat to Apache. So no, they probably won't help.
matt b
A: 

WebLogic can detect stuck threads: http://edocs.bea.com/wls/docs81/perform/WLSTuning.html#1125714

Haven't seen this in Tomcat, though.

Martin Dow