From 6f8b2072e78b10484c6af72fa1fa6d9f6b735d07 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 9 Aug 2016 18:14:22 +0200 Subject: [PATCH] Ensure we only call ReferenceCountUtil.safeRelease(...) in finalize() if the refCnt() > 0 Motivation: We need to ensure we only call ReferenceCountUtil.safeRelease(...) in finalize() if the refCnt() > 0 as otherwise we will log a message about IllegalReferenceCountException. Modification: Check for a refCnt() > 0 before try to release Result: No more IllegalReferenceCountException produced when run finalize() on OpenSsl* objects that where explicit released before. --- handler/src/main/java/io/netty/handler/ssl/OpenSsl.java | 8 ++++++++ .../main/java/io/netty/handler/ssl/OpenSslContext.java | 4 +--- .../src/main/java/io/netty/handler/ssl/OpenSslEngine.java | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java b/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java index a1516702b7..31b8121dff 100644 --- a/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java +++ b/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java @@ -18,6 +18,8 @@ package io.netty.handler.ssl; import io.netty.buffer.ByteBuf; import io.netty.handler.ssl.util.SelfSignedCertificate; +import io.netty.util.ReferenceCountUtil; +import io.netty.util.ReferenceCounted; import io.netty.util.internal.NativeLibraryLoader; import io.netty.util.internal.SystemPropertyUtil; import io.netty.util.internal.logging.InternalLogger; @@ -432,4 +434,10 @@ public final class OpenSsl { private static String normalize(String value) { return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", ""); } + + static void releaseIfNeeded(ReferenceCounted counted) { + if (counted.refCnt() > 0) { + ReferenceCountUtil.safeRelease(counted); + } + } } diff --git a/handler/src/main/java/io/netty/handler/ssl/OpenSslContext.java b/handler/src/main/java/io/netty/handler/ssl/OpenSslContext.java index 46c0807d7f..4f159ce2c2 100644 --- a/handler/src/main/java/io/netty/handler/ssl/OpenSslContext.java +++ b/handler/src/main/java/io/netty/handler/ssl/OpenSslContext.java @@ -22,8 +22,6 @@ import java.security.cert.Certificate; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLException; -import static io.netty.util.ReferenceCountUtil.safeRelease; - /** * This class will use a finalizer to ensure native resources are automatically cleaned up. To avoid finalizers * and manually release the native memory see {@link ReferenceCountedOpenSslContext}. @@ -53,6 +51,6 @@ public abstract class OpenSslContext extends ReferenceCountedOpenSslContext { @SuppressWarnings("FinalizeDeclaration") protected final void finalize() throws Throwable { super.finalize(); - safeRelease(this); + OpenSsl.releaseIfNeeded(this); } } diff --git a/handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java b/handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java index 6155177f49..2ab7c5c489 100644 --- a/handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java +++ b/handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java @@ -37,6 +37,6 @@ public final class OpenSslEngine extends ReferenceCountedOpenSslEngine { @SuppressWarnings("FinalizeDeclaration") protected void finalize() throws Throwable { super.finalize(); - safeRelease(this); + OpenSsl.releaseIfNeeded(this); } }