[issue42762] infinite loop resulted by "yield"

Steve Stagg report at bugs.python.org
Tue Dec 29 15:17:09 EST 2020


Steve Stagg <stestagg at gmail.com> added the comment:

(sorry for spam!)

So, this is a retained reference issue.
If I change the script to be this:


---
import gc

DEPTH = 100

def foo():
    global DEPTH
    try:
        yield
    except BaseException as e:
        DEPTH -= 1
        if DEPTH < 1:
            return
        gc.collect()
        yield from foo()


def x():
    for m in foo():
        print(i)

try:
    x()
except:
    pass


ges = [o for o in gc.get_objects() if isinstance(o, GeneratorExit)]
if ges:
    ge, = ges
    print(gc.get_referrers(ge))
---

Then there's a reference to the GeneratorExit being retained (I guess from the exception frames, althought I thought exception frames were cleared up these days?):

[[GeneratorExit()], {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7fac8899ceb0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/home/sstagg/tmp/fuzztest/tx.py', '__cached__': None, 'gc': <module 'gc' (built-in)>, 'DEPTH': 99, 'foo': <function foo at 0x7fac887dc5e0>, 'x': <function x at 0x7fac887dc700>, 'ges': [GeneratorExit()], 'ge': GeneratorExit()}, <frame at 0x7fac8894d400, file '/home/sstagg/tmp/fuzztest/tx.py', line 14, code foo>]
Exception ignored in: <generator object foo at 0x7fac88851740>
RuntimeError: generator ignored GeneratorExit.

Given the infinite loop happens during the finalization of the generator, I think this reference is stopping the loop from going forever.

I tried removing the "as e" from the above script, and no references are retained.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42762>
_______________________________________


More information about the Python-bugs-list mailing list