core: in astream_events v2 always await task even if already finished (#22916)

- this ensures exceptions propagate to the caller
This commit is contained in:
Nuno Campos 2024-06-14 12:54:20 -07:00 committed by GitHub
parent 513e491ce9
commit 338180f383
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -940,9 +940,10 @@ async def _astream_events_implementation_v2(
yield event
finally:
# Wait for the runnable to finish, if not cancelled (eg. by break)
if task.cancel():
try:
await task
except asyncio.CancelledError:
pass
# Cancel the task if it's still running
task.cancel()
# Await it anyway, to run any cleanup code, and propagate any exceptions
try:
await task
except asyncio.CancelledError:
pass