Skip to content

Commit

Permalink
Don't throw NettyWebServer on permission errors
Browse files Browse the repository at this point in the history
Update `NettyWebServer` so that the `PortInUseException` is not thrown
for permission denied errors.

Fixes gh-19807
  • Loading branch information
philwebb committed Apr 21, 2020
1 parent c761111 commit a2fdf23
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.time.Duration;

import io.netty.channel.unix.Errors.NativeIoException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.netty.ChannelBindException;
Expand All @@ -42,6 +43,11 @@
*/
public class NettyWebServer implements WebServer {

/**
* Permission denied error code from {@code errno.h}.
*/
private static final int ERROR_NO_EACCES = -13;

private static final Log logger = LogFactory.getLog(NettyWebServer.class);

private final HttpServer httpServer;
Expand All @@ -68,7 +74,7 @@ public void start() throws WebServerException {
}
catch (Exception ex) {
ChannelBindException bindException = findBindException(ex);
if (bindException != null) {
if (bindException != null && !isPermissionDenied(bindException.getCause())) {
throw new PortInUseException(bindException.localPort(), ex);
}
throw new WebServerException("Unable to start Netty", ex);
Expand All @@ -78,6 +84,17 @@ public void start() throws WebServerException {
}
}

private boolean isPermissionDenied(Throwable bindExceptionCause) {
try {
if (bindExceptionCause instanceof NativeIoException) {
return ((NativeIoException) bindExceptionCause).expectedErr() == ERROR_NO_EACCES;
}
}
catch (Throwable ex) {
}
return false;
}

private DisposableServer startHttpServer() {
if (this.lifecycleTimeout != null) {
return this.httpServer.handle(this.handlerAdapter).bindNow(this.lifecycleTimeout);
Expand Down

0 comments on commit a2fdf23

Please sign in to comment.