Java – Debugging problems shutting down Apache Tomcat

apachedebuggingjavatomcat

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?

Best Answer

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