Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #368 Throw a marker exception with local address information when server bind fails #396

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/reactor/netty/tcp/DisposableServerBindException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2011-2018 Pivotal Software Inc, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty.tcp;

import java.net.SocketAddress;

/**
* An extended {@link java.net.BindException} that provides information
* for the local address.
*
* @author Violeta Georgieva
*/
public class DisposableServerBindException extends RuntimeException {
final SocketAddress localAddress;

DisposableServerBindException(String message, SocketAddress localAddress) {
super(message);
this.localAddress = localAddress;
}

@Override
public synchronized Throwable fillInStackTrace() {
return this;
}

public SocketAddress getLocalAddress() {
return localAddress;
}
}
15 changes: 11 additions & 4 deletions src/main/java/reactor/netty/tcp/TcpServerBind.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package reactor.netty.tcp;

import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Objects;
Expand Down Expand Up @@ -83,7 +84,7 @@ public Mono<? extends DisposableServer> bind(ServerBootstrap b) {

ChannelFuture f = bootstrap.bind();

DisposableBind disposableServer = new DisposableBind(sink, f, obs);
DisposableBind disposableServer = new DisposableBind(sink, f, obs, b.config().localAddress());
f.addListener(disposableServer);
sink.onCancel(disposableServer);
});
Expand Down Expand Up @@ -142,12 +143,14 @@ static final class DisposableBind
final MonoSink<DisposableServer> sink;
final ChannelFuture f;
final ConnectionObserver selectorObserver;
final SocketAddress localAddress;

DisposableBind(MonoSink<DisposableServer> sink, ChannelFuture f,
ConnectionObserver selectorObserver) {
ConnectionObserver selectorObserver, SocketAddress localAddress) {
this.sink = sink;
this.f = f;
this.selectorObserver = selectorObserver;
this.localAddress = localAddress;
}

@Override
Expand Down Expand Up @@ -179,8 +182,12 @@ public final void operationComplete(ChannelFuture f) {
}
return;
}
if (f.cause() != null) {
sink.error(f.cause());
Throwable t = f.cause();
if (t != null) {
if (t instanceof BindException) {
t = new DisposableServerBindException(t.getMessage() + " " + localAddress, localAddress);
Copy link
Contributor

@simonbasle simonbasle Jul 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to craft a dedicated message for the DSBE that is more explicit and keep the original BindException as a cause?
(here we completely lose the stacktrace, but maybe the BindException has no fillInStackTrace either?)

}
sink.error(t);
}
else {
sink.error(new IOException("error while binding to " + f.channel()));
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/reactor/netty/http/server/HttpServerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import reactor.netty.http.HttpResources;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;
import reactor.netty.tcp.DisposableServerBindException;
import reactor.netty.tcp.TcpClient;
import reactor.test.StepVerifier;
import reactor.util.context.Context;
Expand All @@ -66,6 +67,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;

/**
* @author Stephane Maldini
Expand Down Expand Up @@ -864,4 +866,29 @@ private Object getValueReflection(Object obj, String fieldName, int superLevel)
return null;
}
}

@Test
public void testIssue368() {
DisposableServer server =
HttpServer.create()
.port(8080)
.bindNow();
assertThat(server).isNotNull();

try {
HttpServer.create()
.port(8080)
.bindNow();
fail("The bind operation should fail");
}
catch (DisposableServerBindException e) {
assertThat(e.getMessage()).contains("Address already in use")
.contains("8080");
}
catch (Throwable t) {
fail("DisposableServerBindException is expected ", t);
}

server.dispose();
}
}