Skip to content

Commit aecfead

Browse files
committed
Attempt to fix flakiness
1 parent 5e02bcb commit aecfead

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/a2a/server/request_handlers/request_handler.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,17 @@ async def async_gen_wrapper(
241241
) -> Any:
242242
if params is not None:
243243
validate_proto_required_fields(params)
244-
async for item in method(self, params, context, *args, **kwargs):
245-
yield item
244+
# Explicitly close the inner async generator in a finally block
245+
# so that its cleanup (except/finally) runs deterministically
246+
# during aclose(). On Python < 3.13, async-for does NOT call
247+
# aclose() on the iterator when an exception (e.g. GeneratorExit)
248+
# propagates through the loop body, leaving cleanup to the GC.
249+
inner = method(self, params, context, *args, **kwargs)
250+
try:
251+
async for item in inner:
252+
yield item
253+
finally:
254+
await inner.aclose()
246255

247256
return async_gen_wrapper
248257

0 commit comments

Comments
 (0)