Repository navigation
Conversation
…ialPluginManager In SequentialPluginManager.before_handler_run, before_client_request, and before_execution, catching only `Exception` skipped unwinding plugins that had already run when a later plugin raised `asyncio.CancelledError` (a BaseException in Python 3.8+). Catch `(Exception, asyncio.CancelledError)` in all three before_* hooks so already-run plugins receive their matching after_* hook with the CancelledError before cancellation propagates, matching the cancellation semantics in server_plugin_hooks and PluginAgentExecutor.
🧪 Code Coverage (vs
|
mykytanetipa
self-requested a review
November 26, 2026 10:20
Iwaniukooo11
pushed a commit
that referenced
this pull request
Nov 27, 2026
## What changes A client plugin can miss the terminal notification when the caller cancels a unary request. When the caller cancels an in-flight stream read, the terminal notification currently has `error=None`, the same value as a deliberate `aclose()`. This can leave plugin request tracking unfinished or report an interrupted stream as successful. Report the original `asyncio.CancelledError` through the existing final `after_client_request` hook and re-raise it to the caller. Explicitly closing the stream still produces a successful final notification. The earlier before-hook stage is covered separately in #1525. ## Validation - The repository tests feed `ConnectionError` and `asyncio.CancelledError` through the same transport mock in `get_task`, `send_message` and `subscribe`. All three cancellation cases fail on the base, and the explicit-close control still checks `error=None`. - The [Python 3.10 fork presubmit](https://github.com/GoldArowana/a2a-python/actions/runs/45876426932) passed lint, type checks and the full pytest suite for `0cb5bda9`. It passed 2,722 tests with 94.24% coverage. --------- Co-authored-by: GoldArowana <27399349+GoldArowana@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Unwinds already-executed plugins in
SequentialPluginManagerwhen a subsequent plugin'sbefore_*hook raisesasyncio.CancelledError, delivering theCancelledErrorto each already-run plugin's matchingafter_*hook before propagating cancellation.Root Cause
In
src/a2a/plugins/manager.py,SequentialPluginManager.before_handler_run,before_client_request, andbefore_executiontrack plugins that have already completed theirbefore_*hook inranand unwind them in reverse order if a later plugin fails. However, all three methods only caughtexcept Exception as e::asyncio.CancelledErrorinherits fromBaseException, notException.Nis cancelled while awaiting insidebefore_handler_run,before_client_request, orbefore_execution,CancelledErrorbypasses the unwind block and skips the matchingafter_*cleanup hooks for plugins0..N-1(e.g., leaving OpenTelemetry spans/context tokens or per-request resources unclosed).ServerResponse.error,ClientResponse.error, andExecutionPlugin.after_execution(..., error)already acceptBaseException | None, and bothserver_plugin_hooks(fix(server): run after_handler_run when a handler call is cancelled #1427) andPluginAgentExecutor(fix(server): report asyncio.CancelledError to ExecutionPlugin.after_execution #1514) already catch(Exception, asyncio.CancelledError).Changes
src/a2a/plugins/manager.py: Catchexcept (Exception, asyncio.CancelledError) as e:inSequentialPluginManager.before_handler_run,before_client_request, andbefore_executionso already-run plugins receive their matchingafter_*hook with theCancelledErrorbefore cancellation is re-raised.tests/plugins/test_manager.py:test_cancellation_unwinds_plugins_that_ran(parametrized acrossbefore_handler_run,before_client_request, andbefore_execution): cancels a realasyncio.Taskblocked inside plugin B'sbefore_*hook and verifies plugin A receivesafter_*withasyncio.CancelledError, plugin C never runs, and the task finishes cancelled.test_nested_manager_cancellation_unwinds_all_ran_plugins: verifies cancellation inside a nestedSequentialPluginManagerunwinds both inner and outer already-run plugins in reverse order withasyncio.CancelledError.main(b5597a23) prior to the fix (missingafter_*calls) and pass with the fix.Verification
uv run pytest tests/plugins/test_manager.py -v --cov=a2a.plugins.manager --cov-report=term-missing: 28 passed (src/a2a/plugins/manager.py94% coverage, 100% of modified lines covered).uv run pytest tests/plugins/ tests/server/agent_execution/test_plugin_agent_executor.py tests/server/request_handlers/test_default_request_handler_v2_plugins.py tests/client/test_base_client_plugins.py -v: 78 passed.uv run ruff check src tests: All checks passed.uv run ruff format --check src tests: 274 files already formatted.uv run ty check: 0 new diagnostics (only the 3 pre-existing unusedtype: ignorewarnings inclient_factory.pyandtransports/__init__.py).uv run pytest --ignore=tests/integration/cross_version/client_server/test_client_server.py --ignore=tests/integration/push_notifications/test_default_push_notification_support.py --cov=a2a -q: 2713 passed, 215 skipped, 4 xfailed, 1 xpassed (DB tests without Postgres/MySQL DSNs skipped; the two live subprocess integration test files excluded locally).Checklist
CONTRIBUTINGGuide.ruff check,ruff format --check,ty check,pytest).AI Assistance
Produced with AI assistance and verified locally by reproducing the missing
after_*unwind across all three hooks and nested managers before applying the fix and running the repository checks above.