Skip to content

fix(plugins): unwind already-run plugins on CancelledError in SequentialPluginManager - #1525

Open
xgbtxy wants to merge 1 commit into
a2aproject:mainfrom
xgbtxy:fix/plugins-manager-cancelled-error-unwind
Open

xgbtxy wants to merge 1 commit into
a2aproject:mainfrom
xgbtxy:fix/plugins-manager-cancelled-error-unwind

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Description

Unwinds already-executed plugins in SequentialPluginManager when a subsequent plugin's before_* hook raises asyncio.CancelledError, delivering the CancelledError to each already-run plugin's matching after_* hook before propagating cancellation.

Root Cause

In src/a2a/plugins/manager.py, SequentialPluginManager.before_handler_run, before_client_request, and before_execution track plugins that have already completed their before_* hook in ran and unwind them in reverse order if a later plugin fails. However, all three methods only caught except Exception as e::

Changes

  • src/a2a/plugins/manager.py: Catch except (Exception, asyncio.CancelledError) as e: in SequentialPluginManager.before_handler_run, before_client_request, and before_execution so already-run plugins receive their matching after_* hook with the CancelledError before cancellation is re-raised.
  • tests/plugins/test_manager.py:
    • Added test_cancellation_unwinds_plugins_that_ran (parametrized across before_handler_run, before_client_request, and before_execution): cancels a real asyncio.Task blocked inside plugin B's before_* hook and verifies plugin A receives after_* with asyncio.CancelledError, plugin C never runs, and the task finishes cancelled.
    • Added test_nested_manager_cancellation_unwinds_all_ran_plugins: verifies cancellation inside a nested SequentialPluginManager unwinds both inner and outer already-run plugins in reverse order with asyncio.CancelledError.
    • Verified all 4 new test cases fail on main (b5597a23) prior to the fix (missing after_* 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.py 94% 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 unused type: ignore warnings in client_factory.py and transports/__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

  • Follow the CONTRIBUTING Guide.
  • Make your Pull Request title in the https://www.conventionalcommits.org/ specification.
  • Ensure the tests and linter pass (ruff check, ruff format --check, ty check, pytest).
  • Appropriate docs were updated (no documentation changes needed).

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.

…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.
@xgbtxy
xgbtxy requested a review from a team as a code owner November 25, 2026 16:48
Copilot AI balanced review requested due to automatic review settings November 25, 2026 16:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

Copy link
Copy Markdown

🧪 Code Coverage (vs main)

⬇️ Download Full Report

No coverage changes.

Generated by coverage-comment.yml

@mykytanetipa
mykytanetipa self-requested a review November 26, 2026 10:20
@mykytanetipa mykytanetipa self-assigned this Nov 26, 2026
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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants