Skip to content

fix(a2a): clean up cancellation tasks and Redis pubsub - #6765

Open
adityat210 wants to merge 1 commit into
crewAIInc:mainfrom
adityat210:agent/a2a-cancellation-cleanup
Open

fix(a2a): clean up cancellation tasks and Redis pubsub#6765
adityat210 wants to merge 1 commit into
crewAIInc:mainfrom
adityat210:agent/a2a-cancellation-cleanup

Conversation

@adityat210

Copy link
Copy Markdown

Summary

  • close the Redis pubsub used by the A2A cancellation watcher
  • cancel and await both child tasks during every exit path
  • add regression coverage for normal completion and caller-driven cancellation

Problem

The cancellable decorator spawned an execution task and a cancellation watcher, but its cleanup only deleted the cancellation flag. On normal completion it cancelled the watcher without awaiting it, and when the wrapper itself was cancelled it left both child tasks running. The Redis watcher also created a dedicated pubsub connection without closing it.

That could retain one watcher task and one pubsub connection after a normal Redis-backed execution, or leave the execution task, watcher, and pubsub alive after caller cancellation.

Fix

The wrapper now owns the complete lifecycle of both child tasks in a finally block: unfinished tasks are cancelled and both are awaited with asyncio.gather(..., return_exceptions=True) before the cancellation flag is removed. The Redis watcher closes pubsub in its own finally block and logs connection-level close failures without masking the task result.

This follows Python's asyncio cancellation guidance to perform cleanup in finally, and redis-py's explicit async resource cleanup guidance.

No public API or cancellation semantics change.

Impact

Eliminates up to three residual async resources per caller-cancelled Redis-backed execution (the execution task, watcher task, and pubsub connection), and two after normal Redis-backed completion (watcher plus pubsub).

Tests

  • pytest -n 0 lib/crewai/tests/a2a/utils/test_task.py -q — 18 passed
  • pytest -n 0 lib/crewai/tests/a2a/utils -q — 39 passed
  • ruff format --check — passed
  • ruff check — passed
  • targeted mypy — passed
  • git diff --check — passed

@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6a421497-1727-473a-b07f-9716a4c77464

📥 Commits

Reviewing files that changed from the base of the PR and between c8f441c and 8659424.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/a2a/utils/task.py
  • lib/crewai/tests/a2a/utils/test_task.py

📝 Walkthrough

Walkthrough

The cancellation wrapper now closes Redis pub/sub connections reliably. It also cancels and gathers both execution and watcher tasks. Tests cover successful cleanup, wrapper cancellation, and continued CancelledError behavior.

Changes

Cancellation cleanup

Layer / File(s) Summary
Redis watcher resource cleanup
lib/crewai/src/crewai/a2a/utils/task.py, lib/crewai/tests/a2a/utils/test_task.py
The Redis watcher closes its pub/sub connection in a finally block and logs OSError or ConnectionError failures. Tests verify cleanup after successful execution.
Coordinated task cancellation
lib/crewai/src/crewai/a2a/utils/task.py, lib/crewai/tests/a2a/utils/test_task.py
Cancellation now stops unfinished execution and watcher tasks, then gathers both with exception collection. Tests verify wrapped coroutine cleanup and continued CancelledError behavior.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the cleanup of A2A cancellation tasks and Redis pubsub resources.
Description check ✅ Passed The description directly explains the cancellation cleanup changes, Redis pubsub cleanup, regression tests, and validation results.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@adityat210
adityat210 marked this pull request as ready for review August 1, 2026 20:32
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.

1 participant