fix(library_client): ensure pending asyncio tasks like generator athrow are executed

This commit is contained in:
Ashwin Bharambe 2025-03-09 16:17:27 -07:00
parent ba917a9c48
commit 70ff226b6a

View file

@ -163,6 +163,9 @@ class LlamaStackAsLibraryClient(LlamaStackClient):
except StopAsyncIteration: except StopAsyncIteration:
pass pass
finally: finally:
pending = asyncio.all_tasks(loop)
if pending:
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
loop.close() loop.close()
return sync_generator() return sync_generator()
@ -383,7 +386,6 @@ class AsyncLlamaStackAsLibraryClient(AsyncLlamaStackClient):
# Wrap the generator to preserve context across iterations # Wrap the generator to preserve context across iterations
wrapped_gen = preserve_headers_context_async_generator(gen()) wrapped_gen = preserve_headers_context_async_generator(gen())
mock_response = httpx.Response( mock_response = httpx.Response(
status_code=httpx.codes.OK, status_code=httpx.codes.OK,
content=wrapped_gen, content=wrapped_gen,