Skip to content

Python: fix(a2a): initialize _close_http_client on the user-supplied-client path - #8095

Open
Yufeng He (he-yufeng) wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/a2a-close-http-client
Open

Python: fix(a2a): initialize _close_http_client on the user-supplied-client path#8095
Yufeng He (he-yufeng) wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/a2a-close-http-client

Conversation

@he-yufeng

Copy link
Copy Markdown
Contributor

Motivation & Context

A2AAgent(url=..., http_client=my_client) is the documented way to share an httpx client across agents, but that construction path never sets _close_http_client: the attribute is only assigned in the client is not None early return and in the branch that builds a client itself. Exiting async with A2AAgent(...) on the third path therefore raises AttributeError: 'A2AAgent' object has no attribute '_close_http_client' (_agent.py:371), and the caller's client is never treated according to any ownership rule at all.

Maintainer direction confirmed in the issue ("Please proceed").

Description & Review Guide

  • One-line default: _close_http_client = False is now set before the branch split, so every construction path has the attribute by the time __aexit__ can run. The two self-created paths still flip it to True, so ownership semantics are unchanged.
  • Impact: the user-supplied-client path exits cleanly and does not close the caller's client, matching the documented ownership intent.
  • Review focus: the early-return path (client is not None) keeps its existing _close_http_client = True; I did not touch that, though it is worth a separate look if the intent there differs.

Related Issue

Fixes #7950

Contribution Checklist

  • The code follows the code style of this project.
  • Tests added for the fix: one covers that a caller-supplied client exits cleanly and is not closed, one covers that a self-created client is still closed on exit.
  • All new and existing tests passed: pytest tests/test_a2a_agent.py tests/test_a2a_executor.py tests/test_utils.py — 161 passed. (test_a2a_group_chat.py needs the agent-framework-orchestrations extra, not installed locally; CI covers it.)
  • The contribution is licensed under the MIT License.
  • The code is my own original work, or I have properly attributed it.
  • Documentation updated — not needed; no public API or documented behavior changes.
  • Breaking changes: none.

Verified before the fix, the new test fails with the exact AttributeError from the issue; after the fix both tests pass and ruff is clean.

…client path

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 <40085740+he-yufeng@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused fix resolves the reported failure while preserving existing ownership behavior with adequate regression coverage.

Pull request overview

Fixes A2AAgent cleanup when callers provide their own HTTP client.

Changes:

  • Initializes HTTP-client ownership consistently across constructor paths.
  • Adds tests for caller-owned and internally created client cleanup.
File summaries
File Description
python/packages/a2a/agent_framework_a2a/_agent.py Defaults caller-provided clients to non-owned.
python/packages/a2a/tests/test_a2a_agent.py Verifies both cleanup ownership paths.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: A2AAgent.__aexit__ raises AttributeError when a custom http_client is passed without client=

2 participants