diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ServerUpgradeCodec.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ServerUpgradeCodec.java index 6b25e66efd..84911ecd4f 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ServerUpgradeCodec.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ServerUpgradeCodec.java @@ -146,19 +146,19 @@ public class Http2ServerUpgradeCodec implements HttpServerUpgradeHandler.Upgrade try { // Add the HTTP/2 connection handler to the pipeline immediately following the current handler. ctx.pipeline().addAfter(ctx.name(), handlerName, connectionHandler); - connectionHandler.onHttpServerUpgrade(settings); + // Add also all extra handlers as these may handle events / messages produced by the connectionHandler. + // See https://github.com/netty/netty/issues/9314 + if (handlers != null) { + final String name = ctx.pipeline().context(connectionHandler).name(); + for (int i = handlers.length - 1; i >= 0; i--) { + ctx.pipeline().addAfter(name, null, handlers[i]); + } + } + connectionHandler.onHttpServerUpgrade(settings); } catch (Http2Exception e) { ctx.fireExceptionCaught(e); ctx.close(); - return; - } - - if (handlers != null) { - final String name = ctx.pipeline().context(connectionHandler).name(); - for (int i = handlers.length - 1; i >= 0; i--) { - ctx.pipeline().addAfter(name, null, handlers[i]); - } } } diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ServerUpgradeCodecTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ServerUpgradeCodecTest.java index 02fa243629..50161bbdeb 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ServerUpgradeCodecTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ServerUpgradeCodecTest.java @@ -76,6 +76,11 @@ public class Http2ServerUpgradeCodecTest { // Flush the channel to ensure we write out all buffered data channel.flush(); + channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf()); + Http2FrameInboundWriter writer = new Http2FrameInboundWriter(channel); + writer.writeInboundSettings(new Http2Settings()); + writer.writeInboundRstStream(Http2CodecUtil.HTTP_UPGRADE_STREAM_ID, Http2Error.CANCEL.code()); + assertSame(handler, channel.pipeline().remove(handler.getClass())); assertNull(channel.pipeline().get(handler.getClass())); assertTrue(channel.finish()); @@ -85,6 +90,10 @@ public class Http2ServerUpgradeCodecTest { assertNotNull(settingsBuffer); settingsBuffer.release(); + ByteBuf buf = channel.readOutbound(); + assertNotNull(buf); + buf.release(); + assertNull(channel.readOutbound()); }