From 58e30f1b5f21ce69d65678cb9ab4db71b9e31d46 Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Fri, 23 Oct 2015 14:03:08 -0700 Subject: [PATCH] Http2ConnectionHandler.BaseBuilder exception cleanup Motivation: Http2ConnectionHandler.BaseBuilder is constructing objects which have 'close' methods, but is not calling these methods in the event of an exception. Modifications: - Objects which implement 'close' should have this method called if an exception is thrown and the build operation can not complete normally. Result: Objects are closed even if the build process encounters an error. --- .../handler/codec/http2/Http2ConnectionHandler.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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 6322db80b8..9ffc48ed5b 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 @@ -188,6 +188,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"); @@ -207,8 +209,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);