Skip to content

Commit a2fdf23

Browse files
committed
Don't throw NettyWebServer on permission errors
Update `NettyWebServer` so that the `PortInUseException` is not thrown for permission denied errors. Fixes gh-19807
1 parent c761111 commit a2fdf23

File tree

1 file changed

+18
-1
lines changed
  • spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty

1 file changed

+18
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.time.Duration;
2020

21+
import io.netty.channel.unix.Errors.NativeIoException;
2122
import org.apache.commons.logging.Log;
2223
import org.apache.commons.logging.LogFactory;
2324
import reactor.netty.ChannelBindException;
@@ -42,6 +43,11 @@
4243
*/
4344
public class NettyWebServer implements WebServer {
4445

46+
/**
47+
* Permission denied error code from {@code errno.h}.
48+
*/
49+
private static final int ERROR_NO_EACCES = -13;
50+
4551
private static final Log logger = LogFactory.getLog(NettyWebServer.class);
4652

4753
private final HttpServer httpServer;
@@ -68,7 +74,7 @@ public void start() throws WebServerException {
6874
}
6975
catch (Exception ex) {
7076
ChannelBindException bindException = findBindException(ex);
71-
if (bindException != null) {
77+
if (bindException != null && !isPermissionDenied(bindException.getCause())) {
7278
throw new PortInUseException(bindException.localPort(), ex);
7379
}
7480
throw new WebServerException("Unable to start Netty", ex);
@@ -78,6 +84,17 @@ public void start() throws WebServerException {
7884
}
7985
}
8086

87+
private boolean isPermissionDenied(Throwable bindExceptionCause) {
88+
try {
89+
if (bindExceptionCause instanceof NativeIoException) {
90+
return ((NativeIoException) bindExceptionCause).expectedErr() == ERROR_NO_EACCES;
91+
}
92+
}
93+
catch (Throwable ex) {
94+
}
95+
return false;
96+
}
97+
8198
private DisposableServer startHttpServer() {
8299
if (this.lifecycleTimeout != null) {
83100
return this.httpServer.handle(this.handlerAdapter).bindNow(this.lifecycleTimeout);

0 commit comments

Comments
 (0)