gh-151728: Clear the typing caches at interpreter shutdown - #154858
Open
wjakob wants to merge 2 commits into
Open
gh-151728: Clear the typing caches at interpreter shutdown#154858wjakob wants to merge 2 commits into
wjakob wants to merge 2 commits into
Conversation
wjakob
requested review from
a team,
AlexWaygood,
JelleZijlstra,
ZeroIntensity and
encukou
as code owners
July 29, 2026 07:21
Documentation build overview
295 files changed ·
|
The typing module caches every subscripted type. When an extension module leaks a reference to typing, these caches also keep types owned by other (correct) extension modules alive past interpreter shutdown, where nothing can free them anymore. The cache_clear callables are already collected in typing._cleanups, so registering them with atexit is enough to avoid this.
wjakob
force-pushed
the
gh-151728-typing-clear-caches
branch
from
July 29, 2026 07:38
a191941 to
acbb783
Compare
Member
|
Looks reasonable. |
Contributor
Author
|
Done. |
Member
|
Quick thought: Should we perhaps add this to If not, this looks good to me anyway. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
typingcaches every subscripted type in module-levellru_cachees. Those caches are only released when thetypingmodule itself is torn down, which does not happen when some extension module leaks a reference to it. Sincetypingis nearly universal, a single such leak (e.g. viatorch) keeps the cached types alive past interpreter shutdown, including types owned by unrelated, correctly written extensions. Those extensions then look like they are leaking:valgrindreports them, and nanobind prints warnings about types, functions, and instances that were never freed.typing._cleanupsalready holds callables to clean caches but does nothing with them. This PR registers an atexit callback that triggers them during Python finalization so that the process is leak-free upon termination. This complements gh-98253, which broke a cycle inside_tp_cachebut left the retention itself in place.The PR adds no tests because reproducing the problem requires an extension module that deliberately leaks a reference (i.e. one with a missing
tp_traverse). A minimal one is at https://github.com/wjakob/typing-leak.typingcaches keep extension types alive past shutdown #151728