fix(tasks): clean up execution_spans on async CancelledError - #7361
devtechedge wants to merge 1 commit into
Conversation
Emit TaskFailedEvent when _aexecute_core is cancelled so EventListener can pop the span entry and release retained task graphs. Adds regression coverage for issue crewAIInc#7351.
|
Quick note for maintainers: I do not have permission to apply labels on this repo. Please add |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughThe task execution path now handles ChangesAsync cancellation handling
Suggested reviewers: Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to Cancelled async tasks now emit a terminal failure event before cancellation propagates, allowing telemetry spans and related task objects to be cleaned up. The targeted cleanup and event behavior are covered by regression tests, with no remaining merge-blocking risk identified. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Summary
Fixes #7351.
When an async task is cancelled after
TaskStartedEvent,asyncio.CancelledErrorwas skipping theexcept Exceptionpath in_aexecute_core(). That meant no terminal event was emitted, soEventListener.execution_spanskept a strong reference to theTask(and through it the Agent / Crew graph).This change catches
CancelledErrorexplicitly, emitsTaskFailedEventso the existing listener can pop the span entry and close telemetry the same way ordinary failures do, then re-raises to preserve cancellation semantics.Changes
lib/crewai/src/crewai/task.py: handleasyncio.CancelledErrorbeforeexcept Exceptionin_aexecute_core()lib/crewai/tests/telemetry/test_task_cancellation_span_cleanup.py: regression tests for span cleanup andTaskFailedEventemission on cancelWhy TaskFailedEvent
Reusing
TaskFailedEventis the smallest behavior change. The listener already popsexecution_spansand routes failures throughtask_failed. A dedicated cancellation event would be nicer for telemetry consumers later, but it is not required to close the leak.Test plan
CancelledErroremitsTaskFailedEventuv run pytest lib/crewai/tests/telemetry/test_task_cancellation_span_cleanup.py -x -qNotes for maintainers
Happy to adjust if you would rather introduce a dedicated cancellation event instead of reusing
TaskFailedEvent.I do not have permission to apply labels here - please add
llm-generatedwhen you can (required by the contributing guide).Thanks for reviewing.