Skip to content

Commit d1b67fd

Browse files
committed
Netty: fix bind exception on startup
1 parent 0b3c25c commit d1b67fd

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

jooby/src/main/java/io/jooby/Server.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import javax.annotation.Nullable;
1010
import java.io.EOFException;
1111
import java.io.IOException;
12+
import java.net.BindException;
1213
import java.nio.channels.ClosedChannelException;
1314
import java.util.List;
15+
import java.util.Optional;
1416
import java.util.concurrent.Executor;
1517
import java.util.concurrent.atomic.AtomicBoolean;
1618

@@ -134,9 +136,27 @@ static boolean connectionLost(@Nullable Throwable cause) {
134136
String message = cause.getMessage();
135137
if (message != null) {
136138
String msg = message.toLowerCase();
137-
return msg.contains("reset by peer") || msg.contains("broken pipe") || msg.contains("forcibly closed");
139+
return msg.contains("reset by peer") || msg.contains("broken pipe") || msg
140+
.contains("forcibly closed");
138141
}
139142
}
140143
return (cause instanceof ClosedChannelException) || (cause instanceof EOFException);
141144
}
145+
146+
/**
147+
* Whenever the given exception is an address already in use. This probably won't work in none
148+
* English locale systems.
149+
*
150+
* @param cause Exception to check.
151+
* @return True address alaredy in use.
152+
*/
153+
static boolean isAddressInUse(@Nullable Throwable cause) {
154+
return (cause instanceof BindException) ||
155+
(Optional.ofNullable(cause)
156+
.map(Throwable::getMessage)
157+
.map(String::toLowerCase)
158+
.filter(msg -> msg.contains("address already in use"))
159+
.isPresent()
160+
);
161+
}
142162
}

modules/jooby-jetty/src/main/java/io/jooby/jetty/Jetty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public class Jetty extends io.jooby.Server.Base {
159159

160160
fireReady(applications);
161161
} catch (Exception x) {
162-
if (x.getCause() instanceof BindException) {
162+
if (io.jooby.Server.isAddressInUse(x.getCause())) {
163163
x = new BindException("Address already in use: " + options.getPort());
164164
}
165165
throw SneakyThrows.propagate(x);

modules/jooby-netty/src/main/java/io/jooby/netty/Netty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public class Netty extends Server.Base {
129129
throw SneakyThrows.propagate(x);
130130
} catch (ExecutionException x) {
131131
Throwable cause = x.getCause();
132-
if (cause instanceof BindException) {
132+
if (Server.isAddressInUse(cause)) {
133133
cause = new BindException("Address already in use: " + options.getPort());
134134
}
135135
throw SneakyThrows.propagate(cause);

modules/jooby-utow/src/main/java/io/jooby/utow/Utow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public class Utow extends Server.Base {
116116
return this;
117117
} catch (RuntimeException x) {
118118
Throwable cause = Optional.ofNullable(x.getCause()).orElse(x);
119-
if (cause instanceof BindException) {
119+
if (Server.isAddressInUse(cause)) {
120120
cause = new BindException("Address already in use: " + options.getPort());
121121
}
122122
throw SneakyThrows.propagate(cause);

0 commit comments

Comments
 (0)