mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
Stop throwing PortInUseException for unassignable address
Fixes gh-47618
This commit is contained in:
committed by
Phillip Webb
parent
855d6960e3
commit
7f6c00d52a
+16
-3
@@ -64,6 +64,11 @@ public class NettyWebServer implements WebServer {
|
||||
*/
|
||||
private static final int ERROR_NO_EACCES = -13;
|
||||
|
||||
/**
|
||||
* Address in use error code from {@code errno.h}.
|
||||
*/
|
||||
private static final int ERROR_ADDR_IN_USE = -98;
|
||||
|
||||
private static final Predicate<HttpServerRequest> ALWAYS = (request) -> true;
|
||||
|
||||
private static final Log logger = LogFactory.getLog(NettyWebServer.class);
|
||||
@@ -115,11 +120,19 @@ public class NettyWebServer implements WebServer {
|
||||
this.disposableServer = startHttpServer();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
PortInUseException.ifCausedBy(ex, ChannelBindException.class, (bindException) -> {
|
||||
if (bindException.localPort() > 0 && !isPermissionDenied(bindException.getCause())) {
|
||||
throw new PortInUseException(bindException.localPort(), ex);
|
||||
PortInUseException.ifCausedBy(ex, ChannelBindException.class, (channelBindException) -> {
|
||||
if (channelBindException.localPort() > 0 && !isPermissionDenied(channelBindException.getCause())) {
|
||||
PortInUseException.throwIfPortBindingException(channelBindException,
|
||||
channelBindException::localPort);
|
||||
}
|
||||
});
|
||||
if (ex instanceof ChannelBindException channelBindException) {
|
||||
PortInUseException.ifCausedBy(ex, NativeIoException.class, (nativeIoException) -> {
|
||||
if (nativeIoException.expectedErr() == ERROR_ADDR_IN_USE) {
|
||||
throw new PortInUseException(channelBindException.localPort(), ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
throw new WebServerException("Unable to start Netty", ex);
|
||||
}
|
||||
if (this.disposableServer != null) {
|
||||
|
||||
+12
-1
@@ -17,7 +17,9 @@
|
||||
package org.springframework.boot.web.embedded.netty;
|
||||
|
||||
import java.net.ConnectException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -45,6 +47,7 @@ import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFac
|
||||
import org.springframework.boot.web.server.PortInUseException;
|
||||
import org.springframework.boot.web.server.Shutdown;
|
||||
import org.springframework.boot.web.server.Ssl;
|
||||
import org.springframework.boot.web.server.WebServerException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ReactorResourceFactory;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
@@ -70,7 +73,7 @@ import static org.mockito.Mockito.mock;
|
||||
class NettyReactiveWebServerFactoryTests extends AbstractReactiveWebServerFactoryTests {
|
||||
|
||||
@Test
|
||||
void exceptionIsThrownWhenPortIsAlreadyInUse() {
|
||||
void portInUseExceptionIsThrownWhenPortIsAlreadyInUse() {
|
||||
AbstractReactiveWebServerFactory factory = getFactory();
|
||||
factory.setPort(0);
|
||||
this.webServer = factory.getWebServer(new EchoHandler());
|
||||
@@ -81,6 +84,14 @@ class NettyReactiveWebServerFactoryTests extends AbstractReactiveWebServerFactor
|
||||
.withCauseInstanceOf(Throwable.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void webServerExceptionIsThrownWhenAddressCannotBeAssigned() throws UnknownHostException {
|
||||
AbstractReactiveWebServerFactory factory = getFactory();
|
||||
factory.setPort(8080);
|
||||
factory.setAddress(InetAddress.getByName("1.2.3.4"));
|
||||
assertThatExceptionOfType(WebServerException.class).isThrownBy(factory.getWebServer(new EchoHandler())::start);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPortWhenDisposableServerPortOperationIsUnsupportedReturnsMinusOne() {
|
||||
NettyReactiveWebServerFactory factory = new NoPortNettyReactiveWebServerFactory(0);
|
||||
|
||||
Reference in New Issue
Block a user