Skip to content

Weakly reference engines from the OpenSSL engine map (#17199) - #17205

Merged
normanmaurer merged 2 commits into
netty:4.1from
chrisvest:4.1-openssl-reap
Aug 4, 2026
Merged

normanmaurer merged 2 commits into
netty:4.1from
chrisvest:4.1-openssl-reap

Conversation

@chrisvest

Copy link
Copy Markdown
Member

Motivation:

ReferenceCountedOpenSslContext strongly references every engine through its engines map (the SSL* -> engine reverse lookup used by the OpenSSL callbacks). As an SslContext is typically a long-lived singleton, a leaked engine stays reachable for the context's whole lifetime, so OpenSslEngine.finalize() - the backstop meant for that case - never runs and its native memory is freed only when the context is destroyed.

Modification:

Reintroduce OpenSslEngineMap as a class holding the engines as WeakReferences. A live engine is always strongly reachable via its SslHandler, so only a leaked one becomes collectable. The abstraction removed in #15444 was a strong wrapper that rightly added no value; weak values give it a reason to exist.

Add OpenSslEngineTest.leakedEngineIsReclaimedWhileContextAlive.

Result:

A leaked OPENSSL engine is reclaimed per-engine independently of its context. For OPENSSL_REFCNT a leaked engine now also becomes collectable, so the ResourceLeakDetector reports it instead of it being pinned silently.

(cherry picked from commit 26255b1)

This also backports #15444 which is harmless because neither of those types are public.

Motivation:

ReferenceCountedOpenSslContext strongly references every engine through
its `engines` map (the SSL* -> engine reverse lookup used by the OpenSSL
callbacks). As an SslContext is typically a long-lived singleton, a
leaked engine stays reachable for the context's whole lifetime, so
OpenSslEngine.finalize() - the backstop meant for that case - never runs
and its native memory is freed only when the context is destroyed.

Modification:

Reintroduce OpenSslEngineMap as a class holding the engines as
WeakReferences. A live engine is always strongly reachable via its
SslHandler, so only a leaked one becomes collectable. The abstraction
removed in netty#15444 was a strong wrapper that rightly added no value; weak
values give it a reason to exist.

Add OpenSslEngineTest.leakedEngineIsReclaimedWhileContextAlive.

Result:

A leaked OPENSSL engine is reclaimed per-engine independently of its
context. For OPENSSL_REFCNT a leaked engine now also becomes
collectable, so the ResourceLeakDetector reports it instead of it being
pinned silently.

(cherry picked from commit 26255b1)
@normanmaurer normanmaurer added this to the 4.1.137.Final milestone Aug 4, 2026
@normanmaurer
normanmaurer merged commit b75a281 into netty:4.1 Aug 4, 2026
17 of 18 checks passed
@chrisvest
chrisvest deleted the 4.1-openssl-reap branch August 4, 2026 17:03
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…etty#17205)

Motivation:

ReferenceCountedOpenSslContext strongly references every engine through
its `engines` map (the SSL* -> engine reverse lookup used by the OpenSSL
callbacks). As an SslContext is typically a long-lived singleton, a
leaked engine stays reachable for the context's whole lifetime, so
OpenSslEngine.finalize() - the backstop meant for that case - never runs
and its native memory is freed only when the context is destroyed.

Modification:

Reintroduce OpenSslEngineMap as a class holding the engines as
WeakReferences. A live engine is always strongly reachable via its
SslHandler, so only a leaked one becomes collectable. The abstraction
removed in netty#15444 was a strong wrapper that rightly added no value; weak
values give it a reason to exist.

Add OpenSslEngineTest.leakedEngineIsReclaimedWhileContextAlive.

Result:

A leaked OPENSSL engine is reclaimed per-engine independently of its
context. For OPENSSL_REFCNT a leaked engine now also becomes
collectable, so the ResourceLeakDetector reports it instead of it being
pinned silently.

(cherry picked from commit 26255b1)

This also backports netty#15444 which is
harmless because neither of those types are public.

---------

Co-authored-by: Bryce Anderson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants