Skip to content

Commit f9eb545

Browse files
authored
netty: Fix client keepalive initialization (again)
d116cc9 fixed the NPE, but the initialization of the manager happened _after_ newHandler() was called, so a null manager was passed to the handler. Fixes #2828
1 parent c4bbe66 commit f9eb545

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

netty/src/main/java/io/grpc/netty/NettyClientTransport.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import io.netty.channel.ChannelFutureListener;
5656
import io.netty.channel.ChannelHandlerContext;
5757
import io.netty.channel.ChannelOption;
58+
import io.netty.channel.EventLoop;
5859
import io.netty.channel.EventLoopGroup;
5960
import io.netty.channel.socket.nio.NioSocketChannel;
6061
import io.netty.handler.codec.http2.StreamBufferingEncoder.Http2ChannelClosedException;
@@ -165,14 +166,21 @@ public ClientStream newStream(MethodDescriptor<?, ?> method, Metadata headers) {
165166
public Runnable start(Listener transportListener) {
166167
lifecycleManager = new ClientTransportLifecycleManager(
167168
Preconditions.checkNotNull(transportListener, "listener"));
169+
EventLoop eventLoop = group.next();
170+
if (enableKeepAlive) {
171+
keepAliveManager = new KeepAliveManager(
172+
new ClientKeepAlivePinger(this), eventLoop, keepAliveDelayNanos, keepAliveTimeoutNanos,
173+
false);
174+
}
168175

169-
handler = newHandler();
176+
handler = NettyClientHandler.newHandler(lifecycleManager, keepAliveManager, flowControlWindow,
177+
maxHeaderListSize, Ticker.systemTicker());
170178
HandlerSettings.setAutoWindow(handler);
171179

172180
negotiationHandler = negotiator.newHandler(handler);
173181

174182
Bootstrap b = new Bootstrap();
175-
b.group(group);
183+
b.group(eventLoop);
176184
b.channel(channelType);
177185
if (NioSocketChannel.class.isAssignableFrom(channelType)) {
178186
b.option(SO_KEEPALIVE, true);
@@ -231,10 +239,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
231239
}
232240
});
233241

234-
if (enableKeepAlive) {
235-
keepAliveManager = new KeepAliveManager(
236-
new ClientKeepAlivePinger(this), channel.eventLoop(), keepAliveDelayNanos,
237-
keepAliveTimeoutNanos, false);
242+
if (keepAliveManager != null) {
238243
keepAliveManager.onTransportStarted();
239244
}
240245

@@ -307,9 +312,4 @@ private Status statusFromFailedFuture(ChannelFuture f) {
307312
}
308313
return Utils.statusFromThrowable(t);
309314
}
310-
311-
private NettyClientHandler newHandler() {
312-
return NettyClientHandler.newHandler(lifecycleManager, keepAliveManager, flowControlWindow,
313-
maxHeaderListSize, Ticker.systemTicker());
314-
}
315315
}

0 commit comments

Comments
 (0)