views:

162

answers:

3

I have a Tomcat installed as a Windows service, and sometimes it hangs when I try to shut it down (through services.msc). I tried attaching to it with a debugger, but I don't seem to be able to understand why it's hanging.

When I try to look at one of the threads by doing "Thread.getThreads()[0]", I get an error from the debugger: "Stack frame unavailable".

If instead of shutting down the entire Tomcat, I just unload all my servlets' contexts, everything works perfectly.

How would you proceed in debugging this? Is this by-definition a bug in Tomcat?

+1  A: 

I don't think it is a bug in tomcat, since my tomcats are shut down ok.

If you say that when you unload all the web apps everything is ok, maybe the problem is with one of the web apps. Try to unload just one at a time. shut down the tomcat and see what happens. if it is a web app that causes the problem, once you unload it you won't have the problem.

One more question - what happen when you do it from the command line (not from the services screen)?

Edit

In order to debug remotely you need to add the following to the JAVA_OPTS

JAVA_OPTS=%JAVA_OPTS% -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n

Then you can attach an IDE like eclipse to the process (look for remote debugging). You can download the source from here. What you are looking for (I guess) is methods implementing Lifecycle.stop() or implementations of LifecycleListener. Sorry I cannot be more specific.

Edit 2

One more thing - if it is your development machine, I found it best to run eclipse not as a service but from the command line or directly from the IDE

David Rabinowitz
I'll try "net stop" to take it down.Even if the problem is in one of my web apps, how do I debug it? The problem is not deterministic so isolating the specific app might be difficult.
ripper234
"net stop" is working from the services screen. Have you tried %CATALINA_HOME%\bin\shutdown.bat?
David Rabinowitz
It seems like shutdown.bat is not relevant. It's not my personal dev machine, and I want to understand the root cause of the problem here. Tomcat should stop properly through "net stop".
ripper234
A: 

One thing you could try doing is using a profiler to see what objects are left in memory after a shutdown.

I ran into a similar problem where my codebase had a 'logical memory leak.' The wordage needs to be carefully chosen because java garbage collection does prevent most memory leaks. Logical memory leaks are created by programmers who are not as careful as they should be, or are under too much of a time crunch.

Anyway, we used JProfile to see what objects were still alive in the virtual machine, and we found out that some threads were not properly taken care of during a shutdown. If you are able to find out what objects are still alive, you will have a hint as to why tomcat is not completely shutting down.

bogertron
+1  A: 

For completeness sake, I must ask: have you checked Tomcat's own log files?

%TomcatHome%\logs

which is probably something like...

C:\Program Files\Apache Software Foundation\Tomcat 6.0\logs

Please forgive me if this is the first thing you tried, but you did not mention it, so I figured I'd ask just to be sure.

Willfulwizard
Amm..Hmmm....I must confess I haven't. I'll try this the next time I debug the problem.
ripper234