Skip to content

Commit 25a3dcb

Browse files
authored
Python: fix(a2a): initialize _close_http_client on the user-supplied-client path (microsoft#8095)
A2AAgent(url=..., http_client=mine) never set _close_http_client, so exiting the async context manager raised AttributeError instead of leaving the caller's client alone. Default the flag to False up front; the two paths that create the client still flip it to True. Fixes microsoft#7950 Signed-off-by: Yufeng He <[email protected]>
1 parent 843a7b5 commit 25a3dcb

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎python/packages/a2a/agent_framework_a2a/_agent.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ def __init__(
263263

264264
super().__init__(id=id, name=name, description=description, **kwargs)
265265
self._http_client: httpx.AsyncClient | None = http_client
266+
# every construction path must set this before __aexit__ can run
267+
self._close_http_client = False
266268
self._timeout_config = self._create_timeout_config(timeout)
267269
bindings = supported_protocol_bindings if supported_protocol_bindings is not None else ["JSONRPC"]
268270
if client is not None:

‎python/packages/a2a/tests/test_a2a_agent.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,3 +2536,37 @@ async def test_input_required_sets_task_id_instead_of_reference(mock_a2a_client:
25362536

25372537

25382538
# endregion
2539+
2540+
2541+
async def test_context_manager_with_user_supplied_http_client() -> None:
2542+
"""A2AAgent(url=..., http_client=mine) must exit cleanly and must not close it.
2543+
2544+
The third construction path (no client, caller-provided http_client) used to
2545+
leave _close_http_client unset, so __aexit__ raised AttributeError.
2546+
"""
2547+
mock_http_client = MagicMock()
2548+
mock_http_client.aclose = AsyncMock()
2549+
2550+
agent = A2AAgent(url="http://agent.example", http_client=mock_http_client)
2551+
2552+
async with agent:
2553+
pass
2554+
2555+
# the client belongs to the caller; we must not close it
2556+
mock_http_client.aclose.assert_not_called()
2557+
2558+
2559+
async def test_context_manager_closes_self_created_http_client() -> None:
2560+
"""A2AAgent(url=...) builds its own client and closes it on exit."""
2561+
2562+
with patch("agent_framework_a2a._agent.httpx.AsyncClient") as cls:
2563+
mock_http_client = MagicMock()
2564+
mock_http_client.aclose = AsyncMock()
2565+
cls.return_value = mock_http_client
2566+
2567+
agent = A2AAgent(url="http://agent.example")
2568+
2569+
async with agent:
2570+
pass
2571+
2572+
mock_http_client.aclose.assert_called_once()

0 commit comments

Comments
 (0)