Skip to content

Commit 6f05926

Browse files
committed
Merge pull request #44134 from nosan
* pr/44134: Destroy WebServer if ReactiveWebServerApplicationContext refresh fails Closes gh-44134
2 parents 0a67388 + c28c16e commit 6f05926

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContext.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,9 +66,10 @@ public final void refresh() throws BeansException, IllegalStateException {
6666
super.refresh();
6767
}
6868
catch (RuntimeException ex) {
69-
WebServerManager serverManager = this.serverManager;
70-
if (serverManager != null) {
71-
serverManager.getWebServer().stop();
69+
WebServer webServer = getWebServer();
70+
if (webServer != null) {
71+
webServer.stop();
72+
webServer.destroy();
7273
}
7374
throw ex;
7475
}
@@ -147,6 +148,10 @@ protected void doClose() {
147148
AvailabilityChangeEvent.publish(this, ReadinessState.REFUSING_TRAFFIC);
148149
}
149150
super.doClose();
151+
WebServer webServer = getWebServer();
152+
if (webServer != null) {
153+
webServer.destroy();
154+
}
150155
}
151156

152157
/**

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContextTests.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.boot.availability.ReadinessState;
3131
import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
3232
import org.springframework.boot.web.reactive.server.MockReactiveWebServerFactory;
33+
import org.springframework.boot.web.server.WebServer;
3334
import org.springframework.context.ApplicationContextException;
3435
import org.springframework.context.ApplicationEvent;
3536
import org.springframework.context.ApplicationListener;
@@ -42,6 +43,7 @@
4243
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4344
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4445
import static org.mockito.BDDMockito.then;
46+
import static org.mockito.Mockito.times;
4547

4648
/**
4749
* Tests for {@link ReactiveWebServerApplicationContext}.
@@ -121,13 +123,25 @@ void whenContextIsRefreshedThenLocalServerPortIsAvailableFromTheEnvironment() {
121123
}
122124

123125
@Test
124-
void whenContextIsClosedThenWebServerIsStopped() {
126+
void whenContextRefreshFailedThenWebServerIsStoppedAndDestroyed() {
127+
addWebServerFactoryBean();
128+
addHttpHandlerBean();
129+
this.context.registerBeanDefinition("refreshFailure", new RootBeanDefinition(RefreshFailure.class));
130+
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh);
131+
WebServer webServer = this.context.getWebServer();
132+
then(webServer).should(times(2)).stop();
133+
then(webServer).should().destroy();
134+
}
135+
136+
@Test
137+
void whenContextIsClosedThenWebServerIsStoppedAndDestroyed() {
125138
addWebServerFactoryBean();
126139
addHttpHandlerBean();
127140
this.context.refresh();
128141
MockReactiveWebServerFactory factory = this.context.getBean(MockReactiveWebServerFactory.class);
129142
this.context.close();
130-
then(factory.getWebServer()).should().stop();
143+
then(factory.getWebServer()).should(times(2)).stop();
144+
then(factory.getWebServer()).should().destroy();
131145
}
132146

133147
@Test

0 commit comments

Comments
 (0)