I have a situation where I want to break from an async for loop. I managed to reduce the issue to the application below. I expect to enter the 'finally' section of the context manager when breaking from the loop in main. In other words, the expected result is
try
456
finally
done
but what I get is
try
456
done
finally
and then an exception when the application closes.
Here is the code
import asyncio
from contextlib import asynccontextmanager
@asynccontextmanager
async def receiving():
try:
print('try')
yield 123
finally:
print('finally')
async def request_all():
async with receiving():
yield 456
async def main():
async for r in request_all():
print(r)
break
print('done')
asyncio.run(main())
I found this bug report that seems similar, but as far as I can tell it has been resolved before 3.8. I tested my issue on 3.8.2 and 3.9.6