When the contextDestroyed accesses a static method on a class which has not been loaded yet, the rest of the contextDestroyed code is not executed,e.g.,: package contexttest; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ContextTest implements ServletContextListener { public void contextDestroyed(ServletContextEvent arg0) { System.out.println("Context Destroyed"); MyTest.testStatic(); System.out.println("Context Destroyed Done"); } public void contextInitialized(ServletContextEvent arg0) { System.out.println("Context Initialized"); } } package contexttest; public class MyTest { public static void testStatic(){ System.out.println("My static method call"); } } When I created a war based on this code, and copy this war to the tomcat web apps directory, it logs 'Context Initialized' as expected. When I remove the war it logs: Context Destroyed My static method call Context Destroyed Done as expected. However when I copy the war to web apps, wait till it is initialized, and then touch the war (to simulate the update of a the war). It logs for the destroy event only: 'Context Destroyed' So the static method call and the 'Context Destroyed Done' is missing. When I execute the above on tomcat 7.0.42 it behaves as I would expect, so no difference between removing a war or updating the war.
Created attachment 31446 [details] test project
Are you sure the log hasn't just been buffered? Tomcat doesn't kill threads like that. Can you post a thread dump after "Context Destroyed" has been printed?
The test is using stdout. It is unlikely to be buffered for any noticeable length of time. I can reproduce this on 7.0.x. Odd. Trying on 8.0.x...
OK. Found the problem. Deleting the WAR triggered a code path the undeployed the app and then deleted the expanded directory so the new class was available to load during undeployment. Updating the WAR triggered a code path that deleted the expanded directory and then undeployed the app. That meant the new class was not available to load at the point. I've fixed this my making the process consistent (undeploy then delete). This has been fixed in 8.0.x for 8.0.6 onwards and 7.0.x for 7.0.54 onwards.
Is this bug fixed? I can reproduce this on 7.0.54....
Created attachment 31688 [details] contexttest.war WAR file, build from java files in "test project" Compiled with JDK 1.5 (can be used to test Tomcat 6, if anyone is interested)
(In reply to Yohei Hina from comment #5) > Is this bug fixed? Yes. Using the test war attached to this issue, the scenario in the Description above, current Tomcat 7.0.x and Java 7u55 on Windows, I see that it works correctly. [[[ Jun 04, 2014 3:29:30 PM org.apache.catalina.startup.HostConfig undeploy INFO: Undeploying context [/contexttest] Context Destroyed My static method call Context Destroyed Done Jun 04, 2014 3:29:30 PM org.apache.catalina.startup.HostConfig deployWAR INFO: Deploying web application archive ******\webapps\contexttest.war Context Initialized Jun 04, 2014 3:29:30 PM org.apache.catalina.startup.HostConfig deployWAR INFO: Deployment of web application archive ******\webapps\contexttest.war has finished in 131 ms ]]] The commit for this issue is r1582454 (r1582453 for Tomcat 8).
*** Bug 57122 has been marked as a duplicate of this bug. ***