Skip to content

Commit 491ccaf

Browse files
[3.15] gh-159538: Fix deep chain leak in test_deeply_dotted_name (GH-159576) (#159609)
gh-159538: Fix deep chain leak in test_deeply_dotted_name (GH-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. (cherry picked from commit c84d4b5) Co-authored-by: Brittany Reynoso <breynoso@meta.com>
1 parent dc4adb0 commit 491ccaf

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

‎Lib/test/test_lazy_import/__init__.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ def test_deeply_dotted_name(self):
325325
m.m = m
326326
ns = {"__builtins__": {"__lazy_import__": __lazy_import__,
327327
"__import__": lambda *args: m}}
328+
# Free the chain on the main thread: a thread with a small C stack
329+
# postpones deep deallocations and never gets back to them.
330+
self.addCleanup(ns.clear)
328331
exec("lazy import " + ".".join(["m"] * 10_000) + " as x", ns)
329332
with contextlib.suppress(RecursionError):
330333
ns["x"].resolve()

0 commit comments

Comments
 (0)