deerflow2/backend/tests
momorebi 0409f8cefd
fix(subagents): cleanup background tasks after completion to prevent memory leak (#1030)
* fix(subagents): cleanup background tasks after completion to prevent memory leak

Added cleanup_background_task() function to remove completed subagent results
from the global _background_tasks dict. Found a small issue: completed tasks
were never removed, causing memory to grow indefinitely with each subagent
execution.

Alternative approaches considered:
- Future + SubagentHandle pattern: Not chosen due to requiring refactoring

Chose the simple cleanup approach for minimal code changes while effectively
resolving the memory leak.

Changes:
- Add cleanup_background_task() in executor.py
- Call cleanup in all task_tool return paths (completed, failed, timed out)

* fix(subagents): prevent race condition in background task cleanup

Address Copilot review feedback on memory leak fix:

- Add terminal state check in cleanup_background_task() to only remove
  tasks that are COMPLETED/FAILED/TIMED_OUT or have completed_at set
- Remove cleanup call from polling safety-timeout branch in task_tool
  since the task may still be running
- Add comprehensive tests for cleanup behavior including:
  - Verification that cleanup is called on terminal states
  - Verification that cleanup is NOT called on polling timeout
  - Tests for terminal state check logic in executor

This prevents KeyError when the background executor tries to update
a task that was prematurely removed from _background_tasks.

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
2026-03-10 07:41:48 +08:00
..
conftest.py feat(subagents): make subagent timeout configurable via config.yaml (#897) 2026-02-25 08:39:29 +08:00
test_channels.py feat(channels): make mobile session settings configurable by channel and user (#1021) 2026-03-08 22:19:40 +08:00
test_checkpointer.py fix(checkpointer): return InMemorySaver instead of None when not configured (#1016) (#1019) 2026-03-09 15:48:27 +08:00
test_checkpointer_none_fix.py fix(checkpointer): return InMemorySaver instead of None when not configured (#1016) (#1019) 2026-03-09 15:48:27 +08:00
test_client.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_client_live.py Support langgraph checkpointer (#1005) 2026-03-07 21:07:21 +08:00
test_custom_agent.py fix(backend): upgrade langgraph-api to 0.7 and stabilize memory path tests (#984) 2026-03-06 09:44:40 +08:00
test_docker_sandbox_mode_detection.py feat(subagents): make subagent timeout configurable via config.yaml (#897) 2026-02-25 08:39:29 +08:00
test_infoquest_client.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_lead_agent_model_resolution.py feat(agent):Supports custom agent and chat experience with refactoring (#957) 2026-03-03 21:32:01 +08:00
test_mcp_client_config.py test(backend): add core logic unit tests for task/title/mcp (#936) 2026-03-01 12:36:09 +08:00
test_mcp_oauth.py feat(mcp): add OAuth support for HTTP/SSE MCP servers (#908) 2026-03-01 22:38:58 +08:00
test_memory_upload_filtering.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_model_factory.py feat: add thinking settings to compatible anthropic api (#1017) 2026-03-08 20:18:21 +08:00
test_present_file_tool_core_logic.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_provisioner_kubeconfig.py feat(subagents): make subagent timeout configurable via config.yaml (#897) 2026-02-25 08:39:29 +08:00
test_readability.py fix(backend): Fix readability extraction crash when Node parser fails (#937) 2026-03-01 22:24:02 +08:00
test_reflection_resolvers.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_skills_loader.py feat(skills): support recursive nested skill loading (#950) 2026-03-02 21:02:03 +08:00
test_subagent_executor.py fix(subagents): cleanup background tasks after completion to prevent memory leak (#1030) 2026-03-10 07:41:48 +08:00
test_subagent_timeout_config.py feat(subagents): make subagent timeout configurable via config.yaml (#897) 2026-02-25 08:39:29 +08:00
test_suggestions_router.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_task_tool_core_logic.py fix(subagents): cleanup background tasks after completion to prevent memory leak (#1030) 2026-03-10 07:41:48 +08:00
test_title_generation.py feat: add AIO sandbox provider and auto title generation (#1) 2026-01-14 23:29:18 +08:00
test_title_middleware_core_logic.py Enhance chat UI and compatible anthropic thinking messages (#1018) 2026-03-08 20:19:31 +08:00
test_uploads_middleware_core_logic.py fix(chat): handle empty uploaded files case and improve artifact selection logic (#979) 2026-03-05 17:45:25 +08:00
test_uploads_router.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00