import asyncio async def divide_by(x): await asyncio.sleep(.1) return 1/x async def agen(): try: while True: yield 1 finally: # Awaiting a failing coroutine is fine try: await divide_by(0) except ZeroDivisionError: pass # Awaiting a finished task is fine try: task = asyncio.create_task(divide_by(0)) await asyncio.sleep(.2) await task except ZeroDivisionError: pass # Awaiting an successful unfinished task is fine try: task = asyncio.create_task(divide_by(1)) await task except ZeroDivisionError: pass # Awaiting a failing unfinished task triggers the bug try: task = asyncio.create_task(divide_by(0)) await task except ZeroDivisionError: pass # The bug can also be reproduced with a future try: future = asyncio.Future() loop = asyncio.get_event_loop() loop.call_soon(future.set_exception, ZeroDivisionError()) await future except ZeroDivisionError: pass async def main(): g = agen() async for x in g: assert x == 1 break await g.aclose() if __name__ == '__main__': asyncio.run(main())