Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Python: Fix A2AAgent AttributeError on exit with a caller-supplied ht…
…tp_client

`A2AAgent.__init__` assigned `_close_http_client` on only two of its three
constructor paths. Passing `http_client=` without `client=` left the attribute
unset, so `__aexit__` raised `AttributeError` when the agent was used as an
async context manager - the exact usage the `http_client` docstring invites.

Default `_close_http_client` to False before the branching logic. Every path
now has a safe value, and a caller-supplied client is correctly left open for
its owner to close.

Fixes #7950
  • Loading branch information
atty57 committed Aug 29, 2026
commit 0bde45a2e7e036ddb46c05c013021cf0cfad49b2
2 changes: 2 additions & 0 deletions python/packages/a2a/agent_framework_a2a/_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ def __init__(

super().__init__(id=id, name=name, description=description, **kwargs)
self._http_client: httpx.AsyncClient | None = http_client
# Only close the httpx client if we created it; a caller-supplied one stays the caller's.
Comment thread
atty57 marked this conversation as resolved.
Resolved
self._close_http_client = False
self._timeout_config = self._create_timeout_config(timeout)
bindings = supported_protocol_bindings if supported_protocol_bindings is not None else ["JSONRPC"]
if client is not None:
Expand Down
10 changes: 10 additions & 0 deletions python/packages/a2a/tests/test_a2a_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ async def test_context_manager_no_cleanup_when_no_http_client() -> None:
pass


async def test_context_manager_does_not_close_caller_supplied_http_client() -> None:
"""A caller-supplied http_client (without client=) must survive __aexit__. See issue #7950."""
mock_http_client = AsyncMock()

async with A2AAgent(url="http://localhost:9999/", http_client=cast(Any, mock_http_client)):
pass

mock_http_client.aclose.assert_not_called()


def test_prepare_message_for_a2a_with_multiple_contents() -> None:
"""Test conversion of Message with multiple contents."""

Expand Down
Loading