Skip to content

Commit 5229ac7

Browse files
committed
Stop throwing PortInUseException for unassignable address
Previously, an unassignable address would result in NettyWebServer incorrectly throwing a PortInUseException. Fixes gh-47618
1 parent d08ac5c commit 5229ac7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void start() throws WebServerException {
117117
catch (Exception ex) {
118118
PortInUseException.ifCausedBy(ex, ChannelBindException.class, (bindException) -> {
119119
if (bindException.localPort() > 0 && !isPermissionDenied(bindException.getCause())) {
120-
throw new PortInUseException(bindException.localPort(), ex);
120+
PortInUseException.throwIfPortBindingException(bindException, bindException::localPort);
121121
}
122122
});
123123
throw new WebServerException("Unable to start Netty", ex);

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)