From 543b947e79d862149a1e52a529c1944e53943b25 Mon Sep 17 00:00:00 2001 From: Sam Van Oort Date: Wed, 20 Apr 2016 18:00:10 -0400 Subject: [PATCH] Fix [JENKINS-34281] by running shutdown as system user Replicates the key bit of dad9b04422d572003c83f0fc4543060a70971cc0 But does not include the likely-unneeded ACL.SYSTEM change before System.exit(0) --- core/src/main/java/hudson/WebAppMain.java | 32 ++++++++++++++--------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/hudson/WebAppMain.java b/core/src/main/java/hudson/WebAppMain.java index a34d76c83c..fce3cafa1f 100644 --- a/core/src/main/java/hudson/WebAppMain.java +++ b/core/src/main/java/hudson/WebAppMain.java @@ -27,6 +27,7 @@ import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider import com.thoughtworks.xstream.core.JVM; import com.trilead.ssh2.util.IOUtils; import hudson.model.Hudson; +import hudson.security.ACL; import hudson.util.BootFailure; import jenkins.model.Jenkins; import hudson.util.HudsonIsLoading; @@ -244,7 +245,7 @@ public class WebAppMain implements ServletContextListener { }; initThread.start(); } catch (BootFailure e) { - e.publish(context,home); + e.publish(context, home); } catch (Error | RuntimeException e) { LOGGER.log(SEVERE, "Failed to initialize Jenkins",e); throw e; @@ -364,19 +365,24 @@ public class WebAppMain implements ServletContextListener { public void contextDestroyed(ServletContextEvent event) { try { - terminated = true; - Jenkins instance = Jenkins.getInstanceOrNull(); - if(instance!=null) - instance.cleanUp(); - Thread t = initThread; - if (t != null && t.isAlive()) { - LOGGER.log(Level.INFO, "Shutting down a Jenkins instance that was still starting up", new Throwable("reason")); - t.interrupt(); - } + ACL.impersonate(ACL.SYSTEM, new Runnable() { + @Override + public void run() { + terminated = true; + Jenkins instance = Jenkins.getInstanceOrNull(); + if (instance != null) + instance.cleanUp(); + Thread t = initThread; + if (t != null && t.isAlive()) { + LOGGER.log(Level.INFO, "Shutting down a Jenkins instance that was still starting up", new Throwable("reason")); + t.interrupt(); + } - // Logger is in the system classloader, so if we don't do this - // the whole web app will never be undepoyed. - Logger.getLogger("").removeHandler(handler); + // Logger is in the system classloader, so if we don't do this + // the whole web app will never be undepoyed. + Logger.getLogger("").removeHandler(handler); + } + }); } finally { JenkinsJVMAccess._setJenkinsJVM(false); }