Skip to content

Commit 4d14586

Browse files
authored
Fix leak in SniClientTest. (netty#8324)
Motivation: We need to release the ReferenceCountedSslContext to eliminate resource leaks. Reported in https://garage.netty.io/teamcity/viewLog.html?buildId=33353&buildTypeId=netty_build_oraclejdk8&tab=buildLog#_focus=157264. Modifications: Call release on the SslContext instances. Result: No more leaks in tests.
1 parent 60a7ece commit 4d14586

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

handler/src/test/java/io/netty/handler/ssl/SniClientTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
2929
import io.netty.handler.ssl.util.SelfSignedCertificate;
3030
import io.netty.util.Mapping;
31+
import io.netty.util.ReferenceCountUtil;
32+
import io.netty.util.ReferenceCounted;
3133
import io.netty.util.concurrent.Promise;
3234
import io.netty.util.internal.PlatformDependent;
3335
import org.junit.Assert;
@@ -95,12 +97,13 @@ private static void testSniClient(SslProvider sslServerProvider, SslProvider ssl
9597
String sniHostName = "sni.netty.io";
9698
LocalAddress address = new LocalAddress("test");
9799
EventLoopGroup group = new DefaultEventLoopGroup(1);
100+
SelfSignedCertificate cert = new SelfSignedCertificate();
101+
SslContext sslServerContext = null;
102+
SslContext sslClientContext = null;
103+
98104
Channel sc = null;
99105
Channel cc = null;
100106
try {
101-
SelfSignedCertificate cert = new SelfSignedCertificate();
102-
103-
final SslContext sslServerContext;
104107
if ((sslServerProvider == SslProvider.OPENSSL || sslServerProvider == SslProvider.OPENSSL_REFCNT)
105108
&& !OpenSsl.useKeyManagerFactory()) {
106109
sslServerContext = SslContextBuilder.forServer(cert.certificate(), cert.privateKey())
@@ -118,6 +121,7 @@ private static void testSniClient(SslProvider sslServerProvider, SslProvider ssl
118121
.build();
119122
}
120123

124+
final SslContext finalContext = sslServerContext;
121125
final Promise<String> promise = group.next().newPromise();
122126
ServerBootstrap sb = new ServerBootstrap();
123127
sc = sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {
@@ -127,7 +131,7 @@ protected void initChannel(Channel ch) throws Exception {
127131
@Override
128132
public SslContext map(String input) {
129133
promise.setSuccess(input);
130-
return sslServerContext;
134+
return finalContext;
131135
}
132136
}));
133137
}
@@ -136,12 +140,12 @@ public SslContext map(String input) {
136140
TrustManagerFactory tmf = PlatformDependent.javaVersion() >= 8 ?
137141
SniClientJava8TestUtil.newSniX509TrustmanagerFactory(sniHostName) :
138142
InsecureTrustManagerFactory.INSTANCE;
139-
SslContext sslContext = SslContextBuilder.forClient().trustManager(tmf)
143+
sslClientContext = SslContextBuilder.forClient().trustManager(tmf)
140144
.sslProvider(sslClientProvider).build();
141145
Bootstrap cb = new Bootstrap();
142146

143147
SslHandler handler = new SslHandler(
144-
sslContext.newEngine(ByteBufAllocator.DEFAULT, sniHostName, -1));
148+
sslClientContext.newEngine(ByteBufAllocator.DEFAULT, sniHostName, -1));
145149
cc = cb.group(group).channel(LocalChannel.class).handler(handler)
146150
.connect(address).syncUninterruptibly().channel();
147151
Assert.assertEquals(sniHostName, promise.syncUninterruptibly().getNow());
@@ -160,6 +164,11 @@ public SslContext map(String input) {
160164
if (sc != null) {
161165
sc.close().syncUninterruptibly();
162166
}
167+
ReferenceCountUtil.release(sslServerContext);
168+
ReferenceCountUtil.release(sslClientContext);
169+
170+
cert.delete();
171+
163172
group.shutdownGracefully();
164173
}
165174
}

0 commit comments

Comments
 (0)