AbstractBootstrap can crash instead of failing promise, close #5387

Motivation:
When `ChannelFactory#newChannel` crashed, `AbstractBootstrap#initAndRegister` propagates the exception to the caller instead of failing the promise.

Modifications:
- Catch exceptions from `ChannelFactory#newChannel`.
- Notify promise of such failure.

Result:
`AbstractBootstrap` gracefully handles connect failures.
This commit is contained in:
Stephane Landelle 2016-06-13 15:09:05 +02:00 committed by Norman Maurer
parent d11abbc442
commit 16b530d328
2 changed files with 7 additions and 2 deletions

View File

@ -304,11 +304,15 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
}
final ChannelFuture initAndRegister() {
final Channel channel = channelFactory().newChannel();
Channel channel = null;
try {
channel = channelFactory().newChannel();
init(channel);
} catch (Throwable t) {
channel.unsafe().closeForcibly();
if (channel != null) {
// channel can be null if newChannel crashed (eg SocketException("too many open files"))
channel.unsafe().closeForcibly();
}
// as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
}

View File

@ -216,6 +216,7 @@ public class BootstrapTest {
}
private static final class TestLocalEventLoopGroup extends LocalEventLoopGroup {
ChannelPromise promise;
TestLocalEventLoopGroup() {
super(1);