Skip to content

Commit 7f6c00d

Browse files
wilkinsonaphilwebb
authored andcommitted
Stop throwing PortInUseException for unassignable address
Fixes gh-47618
1 parent 855d696 commit 7f6c00d

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public class NettyWebServer implements WebServer {
6464
*/
6565
private static final int ERROR_NO_EACCES = -13;
6666

67+
/**
68+
* Address in use error code from {@code errno.h}.
69+
*/
70+
private static final int ERROR_ADDR_IN_USE = -98;
71+
6772
private static final Predicate<HttpServerRequest> ALWAYS = (request) -> true;
6873

6974
private static final Log logger = LogFactory.getLog(NettyWebServer.class);
@@ -115,11 +120,19 @@ public void start() throws WebServerException {
115120
this.disposableServer = startHttpServer();
116121
}
117122
catch (Exception ex) {
118-
PortInUseException.ifCausedBy(ex, ChannelBindException.class, (bindException) -> {
119-
if (bindException.localPort() > 0 && !isPermissionDenied(bindException.getCause())) {
120-
throw new PortInUseException(bindException.localPort(), ex);
123+
PortInUseException.ifCausedBy(ex, ChannelBindException.class, (channelBindException) -> {
124+
if (channelBindException.localPort() > 0 && !isPermissionDenied(channelBindException.getCause())) {
125+
PortInUseException.throwIfPortBindingException(channelBindException,
126+
channelBindException::localPort);
121127
}
122128
});
129+
if (ex instanceof ChannelBindException channelBindException) {
130+
PortInUseException.ifCausedBy(ex, NativeIoException.class, (nativeIoException) -> {
131+
if (nativeIoException.expectedErr() == ERROR_ADDR_IN_USE) {
132+
throw new PortInUseException(channelBindException.localPort(), ex);
133+
}
134+
});
135+
}
123136
throw new WebServerException("Unable to start Netty", ex);
124137
}
125138
if (this.disposableServer != null) {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package org.springframework.boot.web.embedded.netty;
1818

1919
import java.net.ConnectException;
20+
import java.net.InetAddress;
2021
import java.net.SocketAddress;
22+
import java.net.UnknownHostException;
2123
import java.time.Duration;
2224
import java.util.Arrays;
2325

@@ -45,6 +47,7 @@
4547
import org.springframework.boot.web.server.PortInUseException;
4648
import org.springframework.boot.web.server.Shutdown;
4749
import org.springframework.boot.web.server.Ssl;
50+
import org.springframework.boot.web.server.WebServerException;
4851
import org.springframework.http.MediaType;
4952
import org.springframework.http.client.ReactorResourceFactory;
5053
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
@@ -70,7 +73,7 @@
7073
class NettyReactiveWebServerFactoryTests extends AbstractReactiveWebServerFactoryTests {
7174

7275
@Test
73-
void exceptionIsThrownWhenPortIsAlreadyInUse() {
76+
void portInUseExceptionIsThrownWhenPortIsAlreadyInUse() {
7477
AbstractReactiveWebServerFactory factory = getFactory();
7578
factory.setPort(0);
7679
this.webServer = factory.getWebServer(new EchoHandler());
@@ -81,6 +84,14 @@ void exceptionIsThrownWhenPortIsAlreadyInUse() {
8184
.withCauseInstanceOf(Throwable.class);
8285
}
8386

87+
@Test
88+
void webServerExceptionIsThrownWhenAddressCannotBeAssigned() throws UnknownHostException {
89+
AbstractReactiveWebServerFactory factory = getFactory();
90+
factory.setPort(8080);
91+
factory.setAddress(InetAddress.getByName("1.2.3.4"));
92+
assertThatExceptionOfType(WebServerException.class).isThrownBy(factory.getWebServer(new EchoHandler())::start);
93+
}
94+
8495
@Test
8596
void getPortWhenDisposableServerPortOperationIsUnsupportedReturnsMinusOne() {
8697
NettyReactiveWebServerFactory factory = new NoPortNettyReactiveWebServerFactory(0);

0 commit comments

Comments
 (0)