trio
trio copied to clipboard
trio finalizes async gen functions without ResourceWarning on pypy - or if a strong reference is kept
import trio
strong_references = set()
async def amain():
print(f"{trio.lowlevel.current_task()=}")
async def agen_fn():
try:
yield
finally:
print(f"{trio.lowlevel.current_task()=}")
strong_references.add(agen := agen_fn())
async for _ in agen:
break
trio.run(amain)
output with python -W error demo.py:
trio.lowlevel.current_task()=<Task '__main__.amain' at 0x7fc7ff9cf2e0>
trio.lowlevel.current_task()=<Task '<init>' at 0x7fc7ff9cf240>
expected output:
trio.lowlevel.current_task()=<Task '__main__.amain' at 0x7fc7ff9cf2e0>
Exception ignored in: <async_generator object amain.<locals>.agen_fn at 0x7f75a13ef740>
Traceback (most recent call last):
File "/home/graingert/projects/trio/trio/_core/_asyncgens.py", line 79, in finalizer
warnings.warn(
ResourceWarning: Async generator '__main__.amain.<locals>.agen_fn' was garbage collected before it had been exhausted. Surround its use in 'async with aclosing(...):' to ensure that it gets cleaned up as soon as you're done using it.
trio.lowlevel.current_task()=<Task '<init>' at 0x7fc7ff9cf240>
the problem is finalize_remaining does not issue a warning. A warning should be issued somewhere here:
https://github.com/python-trio/trio/blob/c7801ae13052ed0aa65f7f6870476f20b0955d94/src/trio/_core/_asyncgens.py#L191-L193
but what to do if there's an exception?