You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generator .close does not release resources #118272
Sending a generator .close() in Python 3.12 does not release resources used by local variables as it used to do in earlier versions of Python. Wrapping the generator content in a try: ... except GeneratorExit: pass releases the resources as expected.
The following shows the difference in space usage between wrapping the code with try-except and not.
defgenerator_using_a_lot_of_space():
a_big_set=set(range(10_000_000))
yield42g=generator_using_a_lot_of_space()
input('A <press enter>') # 13.4 MBprint(next(g))
input('B <press enter>') # 576.6 MBg.close()
input('C <press enter>') # 576.6 MB <== space usage used to drop to 13-14 MBdefgenerator_using_a_lot_of_space():
a_big_set=set(range(10_000_000))
try:
yield42exceptGeneratorExit:
passg=generator_using_a_lot_of_space()
input('D <press enter>') # 14.6 MBprint(next(g))
input('E <press enter>') # 575.6 MBg.close()
input('F <press enter>') # 14.6 MB <== drop in space usage as expected
The above space measurements were done with
Python 3.12.3 (tags/v3.12.3:f6650f9, Apr 9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)] on win32
and looking at the space usage reported by the Windows 11 Task Manager.
Activity
graingert commentedon Apr 25, 2024
bisected to eb4774d using:
outputs:
should output:
passes on 3.11, fails on 3.12 and 3.13a6
pythongh-118272: Clear generator frame's locals when the generator is…
iritkatriel commentedon Apr 25, 2024
Thank you, @gsbrodal for the report, and @graingert for the investigation.
gh-118272: Clear generator frame's locals when the generator is closed (
pythongh-118272: Clear generator frame's locals when the generator is…
pythongh-118272: set stacktop to 0 before freeing contents, to avoid …
gh-118272: set stacktop to 0 before freeing contents, to avoid access…
[3.12] gh-118272: Clear generator frame's locals when the generator i…
pythongh-118272: Clear generator frame's locals when the generator is…
pythongh-118272: set stacktop to 0 before freeing contents, to avoid …
Revert "[3.12] pythongh-118272: Clear generator frame's locals when t…