Bug 56321

Summary: ServletContextListener.contextDestroyed not completely executed when updating war
Product: Tomcat 7 Reporter: M. Laponder <marco.laponder>
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal CC: michael.bauland, yohei_hina
Priority: P2    
Version: 7.0.52   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: test project
contexttest.war

Description M. Laponder 2014-03-27 10:43:45 UTC
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.
Comment 1 M. Laponder 2014-03-27 10:44:55 UTC
Created attachment 31446 [details]
test project
Comment 2 Christopher Schultz 2014-03-27 16:49:04 UTC
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?
Comment 3 Mark Thomas 2014-03-27 19:48:00 UTC
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...
Comment 4 Mark Thomas 2014-03-27 20:13:55 UTC
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.
Comment 5 Yohei Hina 2014-06-04 09:22:29 UTC
Is this bug fixed?

I can reproduce this on 7.0.54....
Comment 6 Konstantin Kolinko 2014-06-04 11:25:46 UTC
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)
Comment 7 Konstantin Kolinko 2014-06-04 11:32:27 UTC
(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).
Comment 8 Konstantin Kolinko 2014-10-21 12:42:52 UTC
*** Bug 57122 has been marked as a duplicate of this bug. ***