Browse Source

Delay ServletContext destruction until Undertow is destroyed

Previously, all destruction was done in the stop method including
closing any Closeables registered with the server. One of these
Closeables managed the lifecycle of the DeploymentManager for the
servlet deployment. Closing it made the servlet context unusable
in `@PreDestroy` methods and upon restart.

This commit moves the closing of the registered Closeables into
destroy(). This allows `@PreDestory` methods to use the
ServletContext. It also allows the server to be stopped and then
restarted without making the ServletContext unusable as it's left
running while the server itself is stopped and not accepting
requests.

Fixes gh-47141
pull/48100/head
Andy Wilkinson 4 months ago
parent
commit
a21bfc2ff5
  1. 16
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java
  2. 12
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java

16
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java

@ -279,9 +279,6 @@ public class UndertowWebServer implements WebServer { @@ -279,9 +279,6 @@ public class UndertowWebServer implements WebServer {
}
try {
this.undertow.stop();
for (Closeable closeable : this.closeables) {
closeable.close();
}
}
catch (Exception ex) {
throw new WebServerException("Unable to stop Undertow", ex);
@ -289,6 +286,19 @@ public class UndertowWebServer implements WebServer { @@ -289,6 +286,19 @@ public class UndertowWebServer implements WebServer {
}
}
@Override
public void destroy() {
stop();
try {
for (Closeable closeable : this.closeables) {
closeable.close();
}
}
catch (IOException ex) {
throw new WebServerException("Unable to destroy Undertow", ex);
}
}
@Override
public int getPort() {
List<Port> ports = getActualPorts();

12
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java

@ -255,18 +255,6 @@ class UndertowServletWebServerFactoryTests extends AbstractServletWebServerFacto @@ -255,18 +255,6 @@ class UndertowServletWebServerFactoryTests extends AbstractServletWebServerFacto
super.portClashOfSecondaryConnectorResultsInPortInUseException();
}
@Test
@Override
@Disabled("Restart after stop is not supported with Undertow")
protected void restartAfterStop() {
}
@Test
@Override
@Disabled("Undertow's architecture prevents separating stop and destroy")
protected void servletContextListenerContextDestroyedIsNotCalledWhenContainerIsStopped() {
}
private void testAccessLog(String prefix, String suffix, String expectedFile)
throws IOException, URISyntaxException {
UndertowServletWebServerFactory factory = getFactory();

Loading…
Cancel
Save