diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java index a4c2ef8d96..a246d58ee9 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java @@ -191,6 +191,8 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, writer); if (encoderEnforceMaxConcurrentStreams) { if (connection.isServer()) { + encoder.close(); + reader.close(); throw new IllegalArgumentException( "encoderEnforceMaxConcurrentStreams: " + encoderEnforceMaxConcurrentStreams + " not supported for server"); @@ -210,8 +212,15 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http * {@link #encoderEnforceMaxConcurrentStreams(boolean)} (int)} */ public final T build(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder) { - // Call the abstract build method - T handler = build0(decoder, encoder); + final T handler; + try { + // Call the abstract build method + handler = build0(decoder, encoder); + } catch (RuntimeException e) { + encoder.close(); + decoder.close(); + throw e; + } // Setup post build options handler.gracefulShutdownTimeoutMillis(gracefulShutdownTimeoutMillis);