From d850715eee623cd573da4e9e66643233334768c2 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sun, 17 Jul 2011 08:35:39 +0100 Subject: [PATCH] Fixed the problem where contextDestroyed() during initialization wasn't honored correctly. --- core/src/main/java/hudson/WebAppMain.java | 18 +++++++++++++++--- core/src/main/java/jenkins/model/Jenkins.java | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/hudson/WebAppMain.java b/core/src/main/java/hudson/WebAppMain.java index 0181227d20..7f238f7722 100644 --- a/core/src/main/java/hudson/WebAppMain.java +++ b/core/src/main/java/hudson/WebAppMain.java @@ -71,6 +71,8 @@ import java.security.Security; public final class WebAppMain implements ServletContextListener { private final RingBufferLogHandler handler = new RingBufferLogHandler(); private static final String APP = "app"; + private boolean terminated; + private Thread initThread; /** * Creates the sole instance of {@link jenkins.model.Jenkins} and register it to the {@link ServletContext}. @@ -207,11 +209,12 @@ public final class WebAppMain implements ServletContextListener { context.setAttribute(APP,new HudsonIsLoading()); - new Thread("hudson initialization thread") { + initThread = new Thread("hudson initialization thread") { @Override public void run() { try { - context.setAttribute(APP,new Hudson(home,context)); + Jenkins instance = new Hudson(home, context); + context.setAttribute(APP, instance); // trigger the loading of changelogs in the background, // but give the system 10 seconds so that the first page @@ -228,9 +231,14 @@ public final class WebAppMain implements ServletContextListener { } catch (Exception e) { LOGGER.log(Level.SEVERE, "Failed to initialize Jenkins",e); context.setAttribute(APP,new HudsonFailedToLoad(e)); + } finally { + Jenkins instance = Jenkins.getInstance(); + if(instance!=null) + instance.cleanUp(); } } - }.start(); + }; + initThread.start(); } catch (Error e) { LOGGER.log(Level.SEVERE, "Failed to initialize Jenkins",e); throw e; @@ -330,9 +338,13 @@ public final class WebAppMain implements ServletContextListener { } public void contextDestroyed(ServletContextEvent event) { + terminated = true; Jenkins instance = Jenkins.getInstance(); if(instance!=null) instance.cleanUp(); + Thread t = initThread; + if (t!=null) + t.interrupt(); // Logger is in the system classloader, so if we don't do this // the whole web app will never be undepoyed. diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 9814947d89..ad578cca6e 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -636,7 +636,7 @@ public class Jenkins extends AbstractCIBase implements ModifiableItemGroup