fix(agent): prune completed tasks from Agent._tasks to prevent unbounded growth #9225

Merged
HAL9000 merged 3 commits from fix/agent-task-list-memory-leak into master 2026-06-03 05:10:54 +00:00

3 Commits

Author SHA1 Message Date
HAL9000 c86db5afa9 style: ruff format fix for agent_task_memory_leak_fix_steps.py
CI / lint (pull_request) Successful in 41s
CI / helm (pull_request) Successful in 35s
CI / build (pull_request) Successful in 40s
CI / typecheck (pull_request) Successful in 1m0s
CI / quality (pull_request) Successful in 1m6s
CI / security (pull_request) Successful in 1m17s
CI / push-validation (pull_request) Successful in 24s
CI / unit_tests (pull_request) Successful in 4m53s
CI / docker (pull_request) Successful in 1m46s
CI / integration_tests (pull_request) Successful in 8m23s
CI / coverage (pull_request) Successful in 10m17s
CI / status-check (pull_request) Successful in 2s
Add blank line after docstring in step_basic_agent_setup to satisfy
ruff format check.
2026-06-03 00:53:20 -04:00
HAL9000 bb3ce1586b fix(agent): fix BDD test coordination and add CONTRIBUTORS entry
Fix the failing unit tests in agent_task_memory_leak_fix.feature by
replacing the broken event loop management with a persistent background
asyncio event loop running in a daemon thread. The original implementation
called asyncio.create_task() from synchronous Behave step code, which
requires a running event loop — causing RuntimeError: no running event loop.

The fix introduces a _BackgroundLoop class that keeps a dedicated asyncio
event loop alive in a background thread. All agent instantiation and
message sending now happens via asyncio.run_coroutine_threadsafe(), ensuring
the event loop is always running when asyncio.create_task() is called.

Also adds the missing step definition for 'I send {count:d} messages to
the agent' (without 'in rapid succession') to match the feature file.

Updates CONTRIBUTORS.md with the agent task memory leak fix contribution.

ISSUES CLOSED: #9044
2026-06-03 00:53:19 -04:00
HAL9000 9dd2ebb3f2 fix(agent): prune completed tasks from Agent._tasks to prevent unbounded growth
This fix addresses issue #9044 by adding a done callback to each asyncio.Task
created in the Agent._setup_processing_pipeline method. The callback removes
the task from the _tasks set upon completion, preventing unbounded memory
growth in long-lived agent instances.

The fix uses task.add_done_callback(self._tasks.discard) to ensure that
completed tasks are promptly removed from the set, allowing them to be
garbage collected. Using set.discard is safe as it never raises ValueError
on double-removal.

ISSUES CLOSED: #9044
2026-06-03 00:53:19 -04:00