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()