fix(agents): await cancelled tasks in pre-3.11 ParallelAgent merge

`_merge_agent_run_pre_3_11()` cancelled its background tasks but did
not await them, so a sibling task could still be mid-`async for` when
the caller ran `aclose()` on the same generator and hit `RuntimeError:
aclose(): asynchronous generator is already running`. That cleanup
race could mask the original sub-agent failure.

Close #5297

Co-authored-by: George Weale <gweale@google.com>
PiperOrigin-RevId: 955369097
This commit is contained in:
George Weale
2026-07-28 11:23:08 -07:00
committed by Copybara-Service
parent 425dda1904
commit bc550991b9
+4 -1
View File
@@ -158,7 +158,10 @@ async def _merge_agent_run_pre_3_11(
finally:
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
if tasks:
# Await cancellation so siblings are no longer mid-iteration when the
# caller `aclose()`s them (else "generator is already running").
await asyncio.gather(*tasks, return_exceptions=True)
@deprecated(