diff --git a/Lib/test/test_lazy_import/__init__.py b/Lib/test/test_lazy_import/__init__.py index a4adf50899a760..59ff1dee53ba50 100644 --- a/Lib/test/test_lazy_import/__init__.py +++ b/Lib/test/test_lazy_import/__init__.py @@ -325,6 +325,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()