diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameCodec.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameCodec.java index 6a2f37832c..41a87f9074 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameCodec.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameCodec.java @@ -159,12 +159,6 @@ public class Http2FrameCodec extends Http2ConnectionHandler { private int numBufferedStreams; private final IntObjectMap frameStreamToInitializeMap = new IntObjectHashMap(8); - private final ChannelFutureListener bufferedStreamsListener = new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) { - numBufferedStreams--; - } - }; Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway) { @@ -420,26 +414,33 @@ public class Http2FrameCodec extends Http2ConnectionHandler { // We should not re-use ids. assert old == null; - // Clean up the stream being initialized if writing the headers fails. - promise.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture channelFuture) { - if (!channelFuture.isSuccess()) { - frameStreamToInitializeMap.remove(streamId); - } - } - }); - encoder().writeHeaders(ctx, streamId, headersFrame.headers(), headersFrame.padding(), headersFrame.isEndStream(), promise); if (!promise.isDone()) { numBufferedStreams++; - promise.addListener(bufferedStreamsListener); + // Clean up the stream being initialized if writing the headers fails and also + // decrement the number of buffered streams. + promise.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) { + numBufferedStreams--; + + handleHeaderFuture(channelFuture, streamId); + } + }); + } else { + handleHeaderFuture(promise, streamId); } } } + private void handleHeaderFuture(ChannelFuture channelFuture, int streamId) { + if (!channelFuture.isSuccess()) { + frameStreamToInitializeMap.remove(streamId); + } + } + private void onStreamActive0(Http2Stream stream) { if (stream.id() != Http2CodecUtil.HTTP_UPGRADE_STREAM_ID && connection().local().isValidStreamId(stream.id())) {