diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java index 64b8e6cb73..a48b973416 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java @@ -159,20 +159,36 @@ public class UndertowServletWebServer implements WebServer { .info("Undertow started on port(s) " + getPortsDescription()); } catch (Exception ex) { - if (findBindException(ex) != null) { - List failedPorts = getConfiguredPorts(); - List actualPorts = getActualPorts(); - failedPorts.removeAll(actualPorts); - if (failedPorts.size() == 1) { - throw new PortInUseException( - failedPorts.iterator().next().getNumber()); + try { + if (findBindException(ex) != null) { + List failedPorts = getConfiguredPorts(); + List actualPorts = getActualPorts(); + failedPorts.removeAll(actualPorts); + if (failedPorts.size() == 1) { + throw new PortInUseException( + failedPorts.iterator().next().getNumber()); + } } + throw new WebServerException("Unable to start embedded Undertow", ex); + } + finally { + stopSilently(); } - throw new WebServerException("Unable to start embedded Undertow", ex); } } } + private void stopSilently() { + try { + if (this.undertow != null) { + this.undertow.stop(); + } + } + catch (Exception ex) { + // Ignore + } + } + private BindException findBindException(Exception ex) { Throwable candidate = ex; while (candidate != null) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java index 104af8fe4b..22eaa779e5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java @@ -88,20 +88,36 @@ public class UndertowWebServer implements WebServer { .info("Undertow started on port(s) " + getPortsDescription()); } catch (Exception ex) { - if (findBindException(ex) != null) { - List failedPorts = getConfiguredPorts(); - List actualPorts = getActualPorts(); - failedPorts.removeAll(actualPorts); - if (failedPorts.size() == 1) { - throw new PortInUseException( - failedPorts.iterator().next().getNumber()); + try { + if (findBindException(ex) != null) { + List failedPorts = getConfiguredPorts(); + List actualPorts = getActualPorts(); + failedPorts.removeAll(actualPorts); + if (failedPorts.size() == 1) { + throw new PortInUseException( + failedPorts.iterator().next().getNumber()); + } } + throw new WebServerException("Unable to start embedded Undertow", ex); + } + finally { + stopSilently(); } - throw new WebServerException("Unable to start embedded Undertow", ex); } } } + private void stopSilently() { + try { + if (this.undertow != null) { + this.undertow.stop(); + } + } + catch (Exception ex) { + // Ignore + } + } + private BindException findBindException(Exception ex) { Throwable candidate = ex; while (candidate != null) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java index c3e90a0078..324e32edab 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java @@ -303,6 +303,9 @@ public class UndertowServletWebServerFactoryTests int blockedPort) { assertThat(ex).isInstanceOf(PortInUseException.class); assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort); + Object undertow = ReflectionTestUtils.getField(this.webServer, "undertow"); + Object worker = ReflectionTestUtils.getField(undertow, "worker"); + assertThat(worker).isNull(); } }