Skip to content

Commit 757aa64

Browse files
mkwphilwebb
authored andcommitted
Ensure web containers are stopped after close
Update `EmbeddedServletContainer` implementations to ensure that stop can be called even if start has not. This allows servers that are partially started during `initialize()` to still be shut down. This commit fixes a regression caused by commit 0af53b3. See gh-8036 Fixes gh-8224 Closes gh-8227
1 parent c06a977 commit 757aa64

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainer.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,6 @@ else if (handler instanceof HandlerCollection) {
205205
@Override
206206
public void stop() {
207207
synchronized (this.monitor) {
208-
if (!this.started) {
209-
return;
210-
}
211208
this.started = false;
212209
try {
213210
this.server.stop();

spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,6 @@ Map<Service, Connector[]> getServiceConnectors() {
279279
@Override
280280
public void stop() throws EmbeddedServletContainerException {
281281
synchronized (this.monitor) {
282-
if (!this.started) {
283-
return;
284-
}
285282
try {
286283
this.started = false;
287284
try {

spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ public void sslCiphersConfiguration() throws Exception {
143143
.isEmpty();
144144
}
145145

146+
@Test
147+
public void stopNoStart() throws Exception {
148+
JettyEmbeddedServletContainerFactory factory = getFactory();
149+
this.container = factory
150+
.getEmbeddedServletContainer(exampleServletRegistration());
151+
this.container.stop();
152+
Server server = ((JettyEmbeddedServletContainer) this.container).getServer();
153+
assertThat(server.isStopped()).isTrue();
154+
}
155+
146156
@Override
147157
protected void addConnector(final int port,
148158
AbstractEmbeddedServletContainerFactory factory) {

spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,16 @@ public void startupFailureDoesNotResultInUnstoppedThreadsBeingReported()
352352
.doesNotContain("appears to have started a thread named [main]");
353353
}
354354

355+
@Test
356+
public void stopNoStart() throws Exception {
357+
TomcatEmbeddedServletContainerFactory factory = getFactory();
358+
this.container = factory
359+
.getEmbeddedServletContainer(exampleServletRegistration());
360+
this.container.stop();
361+
Tomcat tomcat = ((TomcatEmbeddedServletContainer) this.container).getTomcat();
362+
assertThat(tomcat.getServer().getState()).isSameAs(LifecycleState.DESTROYED);
363+
}
364+
355365
@Override
356366
protected void addConnector(int port,
357367
AbstractEmbeddedServletContainerFactory factory) {

0 commit comments

Comments
 (0)