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:
parent
d11abbc442
commit
16b530d328
@ -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);
|
||||
}
|
||||
|
@ -216,6 +216,7 @@ public class BootstrapTest {
|
||||
}
|
||||
|
||||
private static final class TestLocalEventLoopGroup extends LocalEventLoopGroup {
|
||||
|
||||
ChannelPromise promise;
|
||||
TestLocalEventLoopGroup() {
|
||||
super(1);
|
||||
|
Loading…
Reference in New Issue
Block a user