From edc00eef24c13b59f264e0be860fc8b9fd99bb52 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 3 Apr 2018 13:52:28 +0100 Subject: [PATCH] Ensure that Tomcat is completely stopped when its initialization fails Closes gh-12736 --- .../TomcatEmbeddedServletContainer.java | 1 + ...tEmbeddedServletContainerFactoryTests.java | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java index 56af3dfe9c..9b136eab2c 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java @@ -134,6 +134,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer } } catch (Exception ex) { + stopSilently(); throw new EmbeddedServletContainerException( "Unable to start embedded Tomcat", ex); } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java index d7ba095f3a..5a0edda05f 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java @@ -23,6 +23,7 @@ import java.util.Arrays; import java.util.Locale; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -491,7 +492,18 @@ public class TomcatEmbeddedServletContainerFactoryTests @Test public void faultyFilterCausesStartFailure() throws Exception { - AbstractEmbeddedServletContainerFactory factory = getFactory(); + final AtomicReference tomcatReference = new AtomicReference(); + TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory( + 0) { + + @Override + protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( + Tomcat tomcat) { + tomcatReference.set(tomcat); + return super.getTomcatEmbeddedServletContainer(tomcat); + } + + }; factory.addInitializers(new ServletContextInitializer() { @Override @@ -518,7 +530,13 @@ public class TomcatEmbeddedServletContainerFactoryTests }); this.thrown.expect(EmbeddedServletContainerException.class); - factory.getEmbeddedServletContainer().start(); + try { + factory.getEmbeddedServletContainer(); + } + finally { + assertThat(tomcatReference.get().getServer().getState()) + .isEqualTo(LifecycleState.STOPPED); + } } @Override