https://github.com/python/cpython/commit/c84d4b5953f18871ef37b2a95c93de84c66950ef
commit: c84d4b5953f18871ef37b2a95c93de84c66950ef
branch: main
author: Brittany Reynoso <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-17T02:05:23+02:00
summary:

gh-159538: Fix deep chain leak in test_deeply_dotted_name (#159576)

test_deeply_dotted_name() builds a 10,000-deep chain of lazy import
placeholders in a thread with a 256 KiB C stack.  Freeing the chain
there defers most deallocations to the trashcan, which only drains them
once the stack margin recovers.  With a stack that small the margin
never does, so the deferred objects were leaked when the thread exited,
which -R reports as a reference leak on platforms with larger frames
such as PPC64LE.

Clear the namespace in a cleanup, which runs on the main thread.

files:
M Lib/test/test_lazy_import/__init__.py

diff --git a/Lib/test/test_lazy_import/__init__.py 
b/Lib/test/test_lazy_import/__init__.py
index 31006f804fd6d51..fd74ce6827fcca1 100644
--- a/Lib/test/test_lazy_import/__init__.py
+++ b/Lib/test/test_lazy_import/__init__.py
@@ -317,6 +317,9 @@ def test_deeply_dotted_name(self):
         m.m = m
         ns = {"__builtins__": {"__lazy_import__": __lazy_import__,
                                "__import__": lambda *args: m}}
+        # Free the chain on the main thread: a thread with a small C stack
+        # postpones deep deallocations and never gets back to them.
+        self.addCleanup(ns.clear)
         exec("lazy import " + ".".join(["m"] * 10_000) + " as x", ns)
         with contextlib.suppress(RecursionError):
             ns["x"].resolve()

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to