diff --git a/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java b/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java index bdbfe1c235..7bfab1ab23 100644 --- a/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java +++ b/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java @@ -235,7 +235,6 @@ public class ReferenceCountedOpenSslEngine extends SSLEngine implements Referenc int peerPort, boolean jdkCompatibilityMode, boolean leakDetection) { super(peerHost, peerPort); OpenSsl.ensureAvailability(); - leak = leakDetection ? leakDetector.track(this) : null; this.alloc = checkNotNull(alloc, "alloc"); apn = (OpenSslApplicationProtocolNegotiator) context.applicationProtocolNegotiator(); session = new OpenSslSession(context.sessionContext()); @@ -288,6 +287,7 @@ public class ReferenceCountedOpenSslEngine extends SSLEngine implements Referenc PlatformDependent.throwException(cause); } } + leak = leakDetection ? leakDetector.track(this) : null; } /** diff --git a/handler/src/test/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngineTest.java b/handler/src/test/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngineTest.java index 6d38940cbf..ddbd0b16de 100644 --- a/handler/src/test/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngineTest.java +++ b/handler/src/test/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngineTest.java @@ -15,7 +15,9 @@ */ package io.netty.handler.ssl; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import io.netty.util.ReferenceCountUtil; +import org.junit.Test; import javax.net.ssl.SSLEngine; @@ -54,4 +56,14 @@ public class ReferenceCountedOpenSslEngineTest extends OpenSslEngineTest { protected void cleanupServerSslEngine(SSLEngine engine) { ReferenceCountUtil.release(engine); } + + @Test(expected = NullPointerException.class) + public void testNotLeakOnException() throws Exception { + clientSslCtx = SslContextBuilder.forClient() + .trustManager(InsecureTrustManagerFactory.INSTANCE) + .sslProvider(sslClientProvider()) + .build(); + + clientSslCtx.newEngine(null); + } }