Skip to content

Commit c44b912

Browse files
committed
Ensure that Undertow is stopped when it fails to start
Previously, if Undertow failed to start, some of Undertow's internal components would have been started but the started field of UndertowEmbeddedServletContainer remained false. This meant that when stop() was called nothing was done as the container believed it had not been started. This commit updates UndertowEmbeddedServletContainer to stop both the DeploymentManager and the Undertow instance in start() if an exception is thrown. This aligns the behaviour of UndertowEmbeddedServletContainer with that of the Tomcat equivalent. Closes gh-10528
1 parent c681730 commit c44b912

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,40 @@ public void start() throws EmbeddedServletContainerException {
160160
.info("Undertow started on port(s) " + getPortsDescription());
161161
}
162162
catch (Exception ex) {
163-
if (findBindException(ex) != null) {
164-
List<Port> failedPorts = getConfiguredPorts();
165-
List<Port> actualPorts = getActualPorts();
166-
failedPorts.removeAll(actualPorts);
167-
if (failedPorts.size() == 1) {
168-
throw new PortInUseException(
169-
failedPorts.iterator().next().getNumber());
163+
try {
164+
if (findBindException(ex) != null) {
165+
List<Port> failedPorts = getConfiguredPorts();
166+
List<Port> actualPorts = getActualPorts();
167+
failedPorts.removeAll(actualPorts);
168+
if (failedPorts.size() == 1) {
169+
throw new PortInUseException(
170+
failedPorts.iterator().next().getNumber());
171+
}
170172
}
173+
throw new EmbeddedServletContainerException(
174+
"Unable to start embedded Undertow", ex);
175+
}
176+
finally {
177+
stopSilently();
171178
}
172-
throw new EmbeddedServletContainerException(
173-
"Unable to start embedded Undertow", ex);
174179
}
175180
}
176181
}
177182

183+
private void stopSilently() {
184+
try {
185+
if (this.manager != null) {
186+
this.manager.stop();
187+
}
188+
if (this.undertow != null) {
189+
this.undertow.stop();
190+
}
191+
}
192+
catch (Exception ex) {
193+
// Ignore
194+
}
195+
}
196+
178197
private BindException findBindException(Exception ex) {
179198
Throwable candidate = ex;
180199
while (candidate != null) {

spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ protected void handleExceptionCausedByBlockedPort(RuntimeException ex,
318318
int blockedPort) {
319319
assertThat(ex).isInstanceOf(PortInUseException.class);
320320
assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort);
321+
Object undertow = ReflectionTestUtils.getField(this.container, "undertow");
322+
Object worker = ReflectionTestUtils.getField(undertow, "worker");
323+
assertThat(worker).isNull();
321324
}
322325

323326
}

0 commit comments

Comments
 (0)