bug(agent): Potential memory leak in Agent class due to unmanaged tasks #9044

Open
opened 2026-04-14 06:28:16 +00:00 by HAL9000 · 1 comment
Owner

Metadata

  • Commit Message: fix(agent): prune completed tasks from Agent._tasks to prevent unbounded growth
  • Branch Name: fix/agent-task-list-memory-leak

Background and Context

The Agent class in src/cleveragents/agents/base.py creates a new asyncio.Task for each incoming message in the _setup_processing_pipeline method. These tasks are appended to the self._tasks list, but they are never awaited or removed after completion. This can lead to a memory leak if the agent is long-lived and processes many messages, as the _tasks list will grow indefinitely — retaining references to completed asyncio.Task objects and preventing garbage collection.

This issue was found during a code review of the agents module.

To Reproduce:

  1. Create a long-lived Agent instance.
  2. Send a large number of messages to the agent.
  3. Observe the memory usage of the application — it will grow continuously.

Expected Behavior

The _tasks list should be managed to prevent it from growing indefinitely. Completed tasks should be removed from the list (e.g., via a task.add_done_callback that removes the task from self._tasks), or a fixed-size bounded structure should be used. The agent should not retain references to completed asyncio.Task objects.

Acceptance Criteria

  • Agent._tasks does not grow unboundedly during long-lived agent operation
  • Completed asyncio.Task objects are removed from self._tasks promptly after completion (e.g., via add_done_callback)
  • The fix does not break existing task cancellation or shutdown logic
  • Unit tests verify that self._tasks is pruned of completed tasks after message processing
  • nox passes with coverage >= 97%

Subtasks

  • Audit _setup_processing_pipeline in src/cleveragents/agents/base.py to understand the full task lifecycle
  • Add a done_callback to each created task that removes it from self._tasks upon completion
  • Verify that the fix is safe under concurrent access (use a thread-safe or asyncio-safe removal pattern if needed)
  • Add or update unit tests in tests/ to assert that self._tasks does not retain completed tasks
  • Run nox to confirm all sessions pass and coverage >= 97%
  • Open a PR from fix/agent-task-list-memory-leak targeting master

Definition of Done

  • All subtasks above are checked
  • Agent._tasks is pruned of completed tasks — confirmed by unit test
  • No regression in existing agent tests
  • PR merged to master
  • This issue is closed

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata - **Commit Message:** `fix(agent): prune completed tasks from Agent._tasks to prevent unbounded growth` - **Branch Name:** `fix/agent-task-list-memory-leak` ## Background and Context The `Agent` class in `src/cleveragents/agents/base.py` creates a new `asyncio.Task` for each incoming message in the `_setup_processing_pipeline` method. These tasks are appended to the `self._tasks` list, but they are never awaited or removed after completion. This can lead to a memory leak if the agent is long-lived and processes many messages, as the `_tasks` list will grow indefinitely — retaining references to completed `asyncio.Task` objects and preventing garbage collection. This issue was found during a code review of the `agents` module. **To Reproduce:** 1. Create a long-lived `Agent` instance. 2. Send a large number of messages to the agent. 3. Observe the memory usage of the application — it will grow continuously. ## Expected Behavior The `_tasks` list should be managed to prevent it from growing indefinitely. Completed tasks should be removed from the list (e.g., via a `task.add_done_callback` that removes the task from `self._tasks`), or a fixed-size bounded structure should be used. The agent should not retain references to completed `asyncio.Task` objects. ## Acceptance Criteria - [ ] `Agent._tasks` does not grow unboundedly during long-lived agent operation - [ ] Completed `asyncio.Task` objects are removed from `self._tasks` promptly after completion (e.g., via `add_done_callback`) - [ ] The fix does not break existing task cancellation or shutdown logic - [ ] Unit tests verify that `self._tasks` is pruned of completed tasks after message processing - [ ] `nox` passes with coverage >= 97% ## Subtasks - [ ] Audit `_setup_processing_pipeline` in `src/cleveragents/agents/base.py` to understand the full task lifecycle - [ ] Add a `done_callback` to each created task that removes it from `self._tasks` upon completion - [ ] Verify that the fix is safe under concurrent access (use a thread-safe or asyncio-safe removal pattern if needed) - [ ] Add or update unit tests in `tests/` to assert that `self._tasks` does not retain completed tasks - [ ] Run `nox` to confirm all sessions pass and coverage >= 97% - [ ] Open a PR from `fix/agent-task-list-memory-leak` targeting `master` ## Definition of Done - [ ] All subtasks above are checked - [ ] `Agent._tasks` is pruned of completed tasks — confirmed by unit test - [ ] No regression in existing agent tests - [ ] PR merged to `master` - [ ] This issue is closed --- **Automated by CleverAgents Bot** Agent: new-issue-creator
HAL9000 added this to the v3.5.0 milestone 2026-04-14 06:43:42 +00:00
Author
Owner

Verified — Bug: potential memory leak in Agent class due to unmanaged tasks. MoSCoW: Must-have. Priority: High — memory leak in core agent class.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Verified** — Bug: potential memory leak in Agent class due to unmanaged tasks. MoSCoW: Must-have. Priority: High — memory leak in core agent class. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveragents-core#9044
No description provided.