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,6 +160,7 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine
.info("Undertow started on port(s) " + getPortsDescription());
}
catch (Exception ex) {
try {
if (findBindException(ex) != null) {
List<Port> failedPorts = getConfiguredPorts();
List<Port> actualPorts = getActualPorts();
......@@ -172,6 +173,24 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine
throw new EmbeddedServletContainerException(
"Unable to start embedded Undertow", ex);
}
finally {
stopSilently();
}
}
}
}
private void stopSilently() {
try {
if (this.manager != null) {
this.manager.stop();
}
if (this.undertow != null) {
this.undertow.stop();
}
}
catch (Exception ex) {
// Ignore
}
}
......
......@@ -318,6 +318,9 @@ public class UndertowEmbeddedServletContainerFactoryTests
int blockedPort) {
assertThat(ex).isInstanceOf(PortInUseException.class);
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