Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference-counted SslEngines retain a reference to their parent SslContext #9626

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -181,6 +181,7 @@ protected void deallocate() {
boolean closed = leak.close(ReferenceCountedOpenSslEngine.this);
assert closed;
}
parentContext.release();
}
};

Expand Down Expand Up @@ -208,6 +209,7 @@ protected void deallocate() {
final ByteBufAllocator alloc;
private final OpenSslEngineMap engineMap;
private final OpenSslApplicationProtocolNegotiator apn;
private final ReferenceCountedOpenSslContext parentContext;
private final OpenSslSession session;
private final ByteBuffer[] singleSrcBuffer = new ByteBuffer[1];
private final ByteBuffer[] singleDstBuffer = new ByteBuffer[1];
Expand Down Expand Up @@ -366,6 +368,11 @@ public List<byte[]> getStatusResponses() {
}
}

// Now that everything looks good and we're going to successfully return the
// object so we need to retain a reference to the parent context.
parentContext = context;
parentContext.retain();

// Only create the leak after everything else was executed and so ensure we don't produce a false-positive for
// the ResourceLeakDetector.
leak = leakDetection ? leakDetector.track(this) : null;
Expand Down
Expand Up @@ -15,12 +15,15 @@
*/
package io.netty.handler.ssl;

import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test;

import javax.net.ssl.SSLEngine;

import static junit.framework.TestCase.*;

public class ReferenceCountedOpenSslEngineTest extends OpenSslEngineTest {

public ReferenceCountedOpenSslEngineTest(BufferType type, ProtocolCipherCombo combo, boolean delegate,
Expand Down Expand Up @@ -77,4 +80,23 @@ protected SslContext wrapContext(SslContext context) {
}
return context;
}

@Test
public void parentContextIsRetainedByChildEngines() throws Exception {
SslContext clientSslCtx = SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.sslProvider(sslClientProvider())
.protocols(protocols())
.ciphers(ciphers())
.build();

SSLEngine engine = clientSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
assertEquals(ReferenceCountUtil.refCnt(clientSslCtx), 2);

cleanupClientSslContext(clientSslCtx);
assertEquals(ReferenceCountUtil.refCnt(clientSslCtx), 1);

cleanupClientSslEngine(engine);
assertEquals(ReferenceCountUtil.refCnt(clientSslCtx), 0);
}
}