Skip to content

Commit bcfb1d1

Browse files
committed
Merge branch '1.5.x'
2 parents 325b2b0 + c44b912 commit bcfb1d1

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,36 @@ public void start() throws WebServerException {
159159
.info("Undertow started on port(s) " + getPortsDescription());
160160
}
161161
catch (Exception ex) {
162-
if (findBindException(ex) != null) {
163-
List<Port> failedPorts = getConfiguredPorts();
164-
List<Port> actualPorts = getActualPorts();
165-
failedPorts.removeAll(actualPorts);
166-
if (failedPorts.size() == 1) {
167-
throw new PortInUseException(
168-
failedPorts.iterator().next().getNumber());
162+
try {
163+
if (findBindException(ex) != null) {
164+
List<Port> failedPorts = getConfiguredPorts();
165+
List<Port> actualPorts = getActualPorts();
166+
failedPorts.removeAll(actualPorts);
167+
if (failedPorts.size() == 1) {
168+
throw new PortInUseException(
169+
failedPorts.iterator().next().getNumber());
170+
}
169171
}
172+
throw new WebServerException("Unable to start embedded Undertow", ex);
173+
}
174+
finally {
175+
stopSilently();
170176
}
171-
throw new WebServerException("Unable to start embedded Undertow", ex);
172177
}
173178
}
174179
}
175180

181+
private void stopSilently() {
182+
try {
183+
if (this.undertow != null) {
184+
this.undertow.stop();
185+
}
186+
}
187+
catch (Exception ex) {
188+
// Ignore
189+
}
190+
}
191+
176192
private BindException findBindException(Exception ex) {
177193
Throwable candidate = ex;
178194
while (candidate != null) {

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,36 @@ public void start() throws WebServerException {
8888
.info("Undertow started on port(s) " + getPortsDescription());
8989
}
9090
catch (Exception ex) {
91-
if (findBindException(ex) != null) {
92-
List<UndertowWebServer.Port> failedPorts = getConfiguredPorts();
93-
List<UndertowWebServer.Port> actualPorts = getActualPorts();
94-
failedPorts.removeAll(actualPorts);
95-
if (failedPorts.size() == 1) {
96-
throw new PortInUseException(
97-
failedPorts.iterator().next().getNumber());
91+
try {
92+
if (findBindException(ex) != null) {
93+
List<UndertowWebServer.Port> failedPorts = getConfiguredPorts();
94+
List<UndertowWebServer.Port> actualPorts = getActualPorts();
95+
failedPorts.removeAll(actualPorts);
96+
if (failedPorts.size() == 1) {
97+
throw new PortInUseException(
98+
failedPorts.iterator().next().getNumber());
99+
}
98100
}
101+
throw new WebServerException("Unable to start embedded Undertow", ex);
102+
}
103+
finally {
104+
stopSilently();
99105
}
100-
throw new WebServerException("Unable to start embedded Undertow", ex);
101106
}
102107
}
103108
}
104109

110+
private void stopSilently() {
111+
try {
112+
if (this.undertow != null) {
113+
this.undertow.stop();
114+
}
115+
}
116+
catch (Exception ex) {
117+
// Ignore
118+
}
119+
}
120+
105121
private BindException findBindException(Exception ex) {
106122
Throwable candidate = ex;
107123
while (candidate != null) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ protected void handleExceptionCausedByBlockedPort(RuntimeException ex,
303303
int blockedPort) {
304304
assertThat(ex).isInstanceOf(PortInUseException.class);
305305
assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort);
306+
Object undertow = ReflectionTestUtils.getField(this.webServer, "undertow");
307+
Object worker = ReflectionTestUtils.getField(undertow, "worker");
308+
assertThat(worker).isNull();
306309
}
307310

308311
}

0 commit comments

Comments
 (0)