Skip to content

Commit 7d68c7c

Browse files
committed
Merge branch '2.1.x' into 2.2.x
Closes gh-21052
2 parents 4e0fdbe + a2fdf23 commit 7d68c7c

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void start() throws WebServerException {
148148
}
149149
catch (IOException ex) {
150150
if (connector instanceof NetworkConnector && findBindException(ex) != null) {
151-
throw new PortInUseException(((NetworkConnector) connector).getPort());
151+
throw new PortInUseException(((NetworkConnector) connector).getPort(), ex);
152152
}
153153
throw ex;
154154
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.function.Predicate;
2323

24+
import io.netty.channel.unix.Errors.NativeIoException;
2425
import org.apache.commons.logging.Log;
2526
import org.apache.commons.logging.LogFactory;
2627
import reactor.netty.ChannelBindException;
@@ -47,7 +48,12 @@
4748
*/
4849
public class NettyWebServer implements WebServer {
4950

50-
private static final Predicate<HttpServerRequest> ALWAYS = (r) -> true;
51+
/**
52+
* Permission denied error code from {@code errno.h}.
53+
*/
54+
private static final int ERROR_NO_EACCES = -13;
55+
56+
private static final Predicate<HttpServerRequest> ALWAYS = (request) -> true;
5157

5258
private static final Log logger = LogFactory.getLog(NettyWebServer.class);
5359

@@ -81,8 +87,8 @@ public void start() throws WebServerException {
8187
}
8288
catch (Exception ex) {
8389
ChannelBindException bindException = findBindException(ex);
84-
if (bindException != null) {
85-
throw new PortInUseException(bindException.localPort());
90+
if (bindException != null && !isPermissionDenied(bindException.getCause())) {
91+
throw new PortInUseException(bindException.localPort(), ex);
8692
}
8793
throw new WebServerException("Unable to start Netty", ex);
8894
}
@@ -91,6 +97,17 @@ public void start() throws WebServerException {
9197
}
9298
}
9399

100+
private boolean isPermissionDenied(Throwable bindExceptionCause) {
101+
try {
102+
if (bindExceptionCause instanceof NativeIoException) {
103+
return ((NativeIoException) bindExceptionCause).expectedErr() == ERROR_NO_EACCES;
104+
}
105+
}
106+
catch (Throwable ex) {
107+
}
108+
return false;
109+
}
110+
94111
private DisposableServer startHttpServer() {
95112
HttpServer server = this.httpServer;
96113
if (this.routeProviders.isEmpty()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -151,7 +151,7 @@ public void start() throws WebServerException {
151151
List<Port> actualPorts = getActualPorts();
152152
failedPorts.removeAll(actualPorts);
153153
if (failedPorts.size() == 1) {
154-
throw new PortInUseException(failedPorts.iterator().next().getNumber());
154+
throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex);
155155
}
156156
}
157157
throw new WebServerException("Unable to start embedded Undertow", ex);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -109,7 +109,7 @@ public void start() throws WebServerException {
109109
List<UndertowWebServer.Port> actualPorts = getActualPorts();
110110
failedPorts.removeAll(actualPorts);
111111
if (failedPorts.size() == 1) {
112-
throw new PortInUseException(failedPorts.iterator().next().getNumber());
112+
throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex);
113113
}
114114
}
115115
throw new WebServerException("Unable to start embedded Undertow", ex);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PortInUseException.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -32,7 +32,16 @@ public class PortInUseException extends WebServerException {
3232
* @param port the port that was in use
3333
*/
3434
public PortInUseException(int port) {
35-
super("Port " + port + " is already in use", null);
35+
this(port, null);
36+
}
37+
38+
/**
39+
* Creates a new port in use exception for the given {@code port}.
40+
* @param port the port that was in use
41+
* @param cause the cause of the exception
42+
*/
43+
public PortInUseException(int port, Throwable cause) {
44+
super("Port " + port + " is already in use", cause);
3645
this.port = port;
3746
}
3847

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void exceptionIsThrownWhenPortIsAlreadyInUse() {
6262
this.webServer.start();
6363
factory.setPort(this.webServer.getPort());
6464
assertThatExceptionOfType(PortInUseException.class).isThrownBy(factory.getWebServer(new EchoHandler())::start)
65-
.satisfies(this::portMatchesRequirement);
65+
.satisfies(this::portMatchesRequirement).withCauseInstanceOf(Throwable.class);
6666
}
6767

6868
private void portMatchesRequirement(PortInUseException exception) {

0 commit comments

Comments
 (0)