Skip to content

Conversation

@GininDenis
Copy link
Contributor

@GininDenis GininDenis commented Dec 30, 2025

Fix: Prevent Memory Leak in Task Execution Tracking

Summary

Fixes a memory leak in the EventListener where completed and failed tasks were retained in the execution_spans dictionary indefinitely, preventing proper garbage collection.

Problem

The EventListener class maintains an execution_spans dictionary (line 110) that tracks telemetry spans for running tasks. When tasks completed or failed, the code was setting the task reference to None instead of removing it from the dictionary:

This approach kept the task object as a key in the dictionary, preventing Python's garbage collector from reclaiming the memory.

Cascading Impact: Since tasks hold references to:

  • Agents (task.agent)
  • Crews (task.agent.crew)
  • Memory adapters
  • Tools and other resources

This single leak caused the entire object graph to remain in memory indefinitely. In long-running processes executing many tasks, this caused severe unbounded memory growth as each completed task leaked its entire execution context.

Solution

Changed the cleanup logic to properly delete the dictionary entry using del


Note

Prevents retention of finished tasks and their object graphs by cleaning up execution_spans correctly.

  • In event_listener.py, use self.execution_spans.pop(source, None) in TaskCompletedEvent and TaskFailedEvent handlers and remove reassignment to None
  • Telemetry task_ended still invoked when a span exists; no other behavior changes

Written by Cursor Bugbot for commit 560275b. This will update automatically on new commits. Configure here.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@GininDenis GininDenis force-pushed the fix/execution_spans_memory_leak branch from 9bc95df to c9583fe Compare December 30, 2025 06:01
@GininDenis GininDenis force-pushed the fix/execution_spans_memory_leak branch from c9583fe to a176f25 Compare December 30, 2025 06:39
@GininDenis GininDenis changed the title Unlink task in execution spans [Memory leak]]Unlink task in execution spans Dec 30, 2025
@GininDenis GininDenis changed the title [Memory leak]]Unlink task in execution spans [Memory leak]Unlink task in execution spans Dec 30, 2025
@GininDenis GininDenis changed the title [Memory leak]Unlink task in execution spans [Memory leak] Unlink task in execution spans Dec 30, 2025
@greysonlalonde
Copy link
Contributor

Thank you, @GininDenis !

@greysonlalonde greysonlalonde merged commit 17e3fcb into crewAIInc:main Jan 12, 2026
42 checks passed
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.

2 participants