Skip to content

Commit a157528

Browse files
committed
Ensure we only add OpenSslEngine to the OpenSslEngineMap when handshake is started
Motivation: We need to ensure we only add the OpenSslEngine to the OpenSslEngineMap when the handshake is started as otherwise we may produce a memory leak when the OpenSslEngine is created but not actually used. This can for example happen if we encounter a connection refused from the remote peer. In this case we will never remove the OpenSslEngine from the OpenSslEngineMap and so it will never be collected (as we hold a reference). This has as affect that the finalizer will never be run as well. Modifications: - Lazy add the OpenSslEngine to the OpenSslEngineMap to elimate possible leak. - Call OpenSslEngine.shutdown() when SslHandler is removed from the ChannelPipeline to free memory asap in all cases. Result: No more memory leak with OpenSslEngine if connection is refused.
1 parent 4cdbe39 commit a157528

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

handler/src/main/java/io/netty/handler/ssl/OpenSslContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,8 @@ public final boolean isClient() {
295295

296296
@Override
297297
public final SSLEngine newEngine(ByteBufAllocator alloc, String peerHost, int peerPort) {
298-
final OpenSslEngine engine = new OpenSslEngine(ctx, alloc, isClient(), sessionContext(), apn, engineMap,
298+
return new OpenSslEngine(ctx, alloc, isClient(), sessionContext(), apn, engineMap,
299299
rejectRemoteInitiatedRenegotiation, peerHost, peerPort, keyCertChain, clientAuth);
300-
engineMap.add(engine);
301-
return engine;
302300
}
303301

304302
/**

handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private enum HandshakeState {
168168
private final OpenSslApplicationProtocolNegotiator apn;
169169
private final boolean rejectRemoteInitiatedRenegation;
170170
private final OpenSslSession session;
171-
private final java.security.cert.Certificate[] localCerts;
171+
private final Certificate[] localCerts;
172172
private final ByteBuffer[] singleSrcBuffer = new ByteBuffer[1];
173173
private final ByteBuffer[] singleDstBuffer = new ByteBuffer[1];
174174

@@ -1185,6 +1185,9 @@ private HandshakeStatus handshake() throws SSLException {
11851185
throw exception;
11861186
}
11871187

1188+
// Adding the OpenSslEngine to the OpenSslEngineMap so it can be used in the AbstractCertificateVerifier.
1189+
engineMap.add(this);
1190+
11881191
int code = SSL.doHandshake(ssl);
11891192
if (code <= 0) {
11901193
// Check if we have a pending exception that was created during the handshake and if so throw it after

handler/src/main/java/io/netty/handler/ssl/SslHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ public void handlerRemoved0(ChannelHandlerContext ctx) throws Exception {
429429
// Check if queue is not empty first because create a new ChannelException is expensive
430430
pendingUnencryptedWrites.removeAndFailAll(new ChannelException("Pending write on removal of SslHandler"));
431431
}
432+
if (engine instanceof OpenSslEngine) {
433+
// Call shutdown so we ensure all the native memory is released asap
434+
((OpenSslEngine) engine).shutdown();
435+
}
432436
}
433437

434438
@Override

0 commit comments

Comments
 (0)