Commit c44b912f authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent c6817308
...@@ -160,21 +160,40 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine ...@@ -160,21 +160,40 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine
.info("Undertow started on port(s) " + getPortsDescription()); .info("Undertow started on port(s) " + getPortsDescription());
} }
catch (Exception ex) { catch (Exception ex) {
if (findBindException(ex) != null) { try {
List<Port> failedPorts = getConfiguredPorts(); if (findBindException(ex) != null) {
List<Port> actualPorts = getActualPorts(); List<Port> failedPorts = getConfiguredPorts();
failedPorts.removeAll(actualPorts); List<Port> actualPorts = getActualPorts();
if (failedPorts.size() == 1) { failedPorts.removeAll(actualPorts);
throw new PortInUseException( if (failedPorts.size() == 1) {
failedPorts.iterator().next().getNumber()); throw new PortInUseException(
failedPorts.iterator().next().getNumber());
}
} }
throw new EmbeddedServletContainerException(
"Unable to start embedded Undertow", ex);
}
finally {
stopSilently();
} }
throw new EmbeddedServletContainerException(
"Unable to start embedded Undertow", ex);
} }
} }
} }
private void stopSilently() {
try {
if (this.manager != null) {
this.manager.stop();
}
if (this.undertow != null) {
this.undertow.stop();
}
}
catch (Exception ex) {
// Ignore
}
}
private BindException findBindException(Exception ex) { private BindException findBindException(Exception ex) {
Throwable candidate = ex; Throwable candidate = ex;
while (candidate != null) { while (candidate != null) {
......
...@@ -318,6 +318,9 @@ public class UndertowEmbeddedServletContainerFactoryTests ...@@ -318,6 +318,9 @@ public class UndertowEmbeddedServletContainerFactoryTests
int blockedPort) { int blockedPort) {
assertThat(ex).isInstanceOf(PortInUseException.class); assertThat(ex).isInstanceOf(PortInUseException.class);
assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort); assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort);
Object undertow = ReflectionTestUtils.getField(this.container, "undertow");
Object worker = ReflectionTestUtils.getField(undertow, "worker");
assertThat(worker).isNull();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment