Skip to content

Commit a21bfc2

Browse files
committed
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
1 parent 8249929 commit a21bfc2

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,26 @@ public void stop() throws WebServerException {
279279
}
280280
try {
281281
this.undertow.stop();
282-
for (Closeable closeable : this.closeables) {
283-
closeable.close();
284-
}
285282
}
286283
catch (Exception ex) {
287284
throw new WebServerException("Unable to stop Undertow", ex);
288285
}
289286
}
290287
}
291288

289+
@Override
290+
public void destroy() {
291+
stop();
292+
try {
293+
for (Closeable closeable : this.closeables) {
294+
closeable.close();
295+
}
296+
}
297+
catch (IOException ex) {
298+
throw new WebServerException("Unable to destroy Undertow", ex);
299+
}
300+
}
301+
292302
@Override
293303
public int getPort() {
294304
List<Port> ports = getActualPorts();

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,6 @@ protected void portClashOfSecondaryConnectorResultsInPortInUseException() throws
255255
super.portClashOfSecondaryConnectorResultsInPortInUseException();
256256
}
257257

258-
@Test
259-
@Override
260-
@Disabled("Restart after stop is not supported with Undertow")
261-
protected void restartAfterStop() {
262-
}
263-
264-
@Test
265-
@Override
266-
@Disabled("Undertow's architecture prevents separating stop and destroy")
267-
protected void servletContextListenerContextDestroyedIsNotCalledWhenContainerIsStopped() {
268-
}
269-
270258
private void testAccessLog(String prefix, String suffix, String expectedFile)
271259
throws IOException, URISyntaxException {
272260
UndertowServletWebServerFactory factory = getFactory();

0 commit comments

Comments
 (0)