Skip to content

Commit 2a9e6a2

Browse files
committed
Merge branch '3.5.x'
Fixes gh-48059
2 parents 4730dcf + 5954918 commit 2a9e6a2

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

module/spring-boot-reactor-netty/src/main/java/org/springframework/boot/reactor/netty/NettyWebServer.java

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

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

7075
private static final Log logger = LogFactory.getLog(NettyWebServer.class);
@@ -119,11 +124,19 @@ public void start() throws WebServerException {
119124
this.disposableServer = disposableServer;
120125
}
121126
catch (Exception ex) {
122-
PortInUseException.ifCausedBy(ex, ChannelBindException.class, (bindException) -> {
123-
if (bindException.localPort() > 0 && !isPermissionDenied(bindException.getCause())) {
124-
throw new PortInUseException(bindException.localPort(), ex);
127+
PortInUseException.ifCausedBy(ex, ChannelBindException.class, (channelBindException) -> {
128+
if (channelBindException.localPort() > 0 && !isPermissionDenied(channelBindException.getCause())) {
129+
PortInUseException.throwIfPortBindingException(channelBindException,
130+
channelBindException::localPort);
125131
}
126132
});
133+
if (ex instanceof ChannelBindException channelBindException) {
134+
PortInUseException.ifCausedBy(ex, NativeIoException.class, (nativeIoException) -> {
135+
if (nativeIoException.expectedErr() == ERROR_ADDR_IN_USE) {
136+
throw new PortInUseException(channelBindException.localPort(), ex);
137+
}
138+
});
139+
}
127140
throw new WebServerException("Unable to start Netty", ex);
128141
}
129142
logger.info(getStartedOnMessage(disposableServer));

module/spring-boot-reactor-netty/src/test/java/org/springframework/boot/reactor/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.reactor.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

@@ -44,6 +46,7 @@
4446
import org.springframework.boot.web.server.PortInUseException;
4547
import org.springframework.boot.web.server.Shutdown;
4648
import org.springframework.boot.web.server.Ssl;
49+
import org.springframework.boot.web.server.WebServerException;
4750
import org.springframework.boot.web.server.reactive.AbstractReactiveWebServerFactory;
4851
import org.springframework.boot.web.server.reactive.AbstractReactiveWebServerFactoryTests;
4952
import org.springframework.boot.web.server.reactive.ConfigurableReactiveWebServerFactory;
@@ -72,7 +75,7 @@
7275
class NettyReactiveWebServerFactoryTests extends AbstractReactiveWebServerFactoryTests {
7376

7477
@Test
75-
void exceptionIsThrownWhenPortIsAlreadyInUse() {
78+
void portInUseExceptionIsThrownWhenPortIsAlreadyInUse() {
7679
AbstractReactiveWebServerFactory factory = getFactory();
7780
factory.setPort(0);
7881
this.webServer = factory.getWebServer(new EchoHandler());
@@ -83,6 +86,14 @@ void exceptionIsThrownWhenPortIsAlreadyInUse() {
8386
.withCauseInstanceOf(Throwable.class);
8487
}
8588

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

0 commit comments

Comments
 (0)