Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Endless loop in a servlet - recovery possible?

Tags:

java

servlets

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 :-)

like image 862
Mo. Avatar asked Oct 15 '22 17:10

Mo.


2 Answers

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...

like image 71
David Z Avatar answered Oct 20 '22 18:10

David Z


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.

like image 23
david a. Avatar answered Oct 20 '22 17:10

david a.