Skip to content

Commit 76b4d22

Browse files
committed
Free the deep lazy import chain on the main thread in test_lazy_import
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.
1 parent 9cb7a5b commit 76b4d22

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
@@ -318,6 +318,9 @@ def test_deeply_dotted_name(self):
318318
ns = {"__builtins__": {"__lazy_import__": __lazy_import__,
319319
"__import__": lambda *args: m}}
320320
exec("lazy import " + ".".join(["m"] * 10_000) + " as x", ns)
321+
# Free the chain in the main thread: in a thread with a small C stack,
322+
# deallocations deferred by the trashcan are never run.
323+
self.addCleanup(ns.clear)
321324
with contextlib.suppress(RecursionError):
322325
ns["x"].resolve()
323326

0 commit comments

Comments
 (0)