Skip to content

Commit 7d55555

Browse files
committed
fix: release old SslContext on certificate reload to prevent native memory leak (#10395)
1 parent c9e51d6 commit 7d55555

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

proxy/src/main/java/org/apache/rocketmq/proxy/grpc/ProxyAndTlsProtocolNegotiator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import io.grpc.netty.shaded.io.netty.handler.ssl.util.SelfSignedCertificate;
4646
import io.grpc.netty.shaded.io.netty.util.AsciiString;
4747
import io.grpc.netty.shaded.io.netty.util.CharsetUtil;
48+
import io.grpc.netty.shaded.io.netty.util.ReferenceCountUtil;
4849

4950
import java.io.IOException;
5051
import java.io.InputStream;
@@ -77,7 +78,7 @@ public class ProxyAndTlsProtocolNegotiator implements InternalProtocolNegotiator
7778
*/
7879
private static final int SSL_RECORD_HEADER_LENGTH = 5;
7980

80-
private static SslContext sslContext;
81+
private static volatile SslContext sslContext;
8182

8283
public ProxyAndTlsProtocolNegotiator() {
8384
try {
@@ -113,6 +114,7 @@ public static void loadSslContext() throws CertificateException, IOException {
113114
provider = SslProvider.JDK;
114115
log.info("Using JDK SSL provider");
115116
}
117+
SslContext oldSslContext = sslContext;
116118
if (proxyConfig.isTlsTestModeEnable()) {
117119
SelfSignedCertificate selfSignedCertificate = new SelfSignedCertificate();
118120
sslContext = GrpcSslContexts.forServer(selfSignedCertificate.certificate(), selfSignedCertificate.privateKey())
@@ -136,6 +138,9 @@ public static void loadSslContext() throws CertificateException, IOException {
136138
.build();
137139
}
138140
}
141+
if (oldSslContext != null) {
142+
ReferenceCountUtil.release(oldSslContext);
143+
}
139144
}
140145

141146
private class ProxyAndTlsProtocolHandler extends ByteToMessageDecoder {

remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingServer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@
4545
import io.netty.handler.codec.haproxy.HAProxyMessageDecoder;
4646
import io.netty.handler.codec.haproxy.HAProxyProtocolVersion;
4747
import io.netty.handler.codec.haproxy.HAProxyTLV;
48+
import io.netty.handler.ssl.SslContext;
4849
import io.netty.handler.timeout.IdleState;
4950
import io.netty.handler.timeout.IdleStateEvent;
5051
import io.netty.handler.timeout.IdleStateHandler;
5152
import io.netty.util.AttributeKey;
5253
import io.netty.util.CharsetUtil;
5354
import io.netty.util.HashedWheelTimer;
55+
import io.netty.util.ReferenceCountUtil;
5456
import io.netty.util.Timeout;
5557
import io.netty.util.TimerTask;
5658
import io.netty.util.concurrent.DefaultEventExecutorGroup;
@@ -183,7 +185,11 @@ public void loadSslContext() {
183185

184186
if (tlsMode != TlsMode.DISABLED) {
185187
try {
188+
SslContext oldSslContext = sslContext;
186189
sslContext = TlsHelper.buildSslContext(false);
190+
if (oldSslContext != null) {
191+
ReferenceCountUtil.release(oldSslContext);
192+
}
187193
log.info("SslContext created for server");
188194
} catch (CertificateException | IOException e) {
189195
log.error("Failed to create SslContext for server", e);

0 commit comments

Comments
 (0)