"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(swarm): close per-task ChatLLM to stop pooled connection leak
cgycorey129cb25e86ac
diff --git a/agent/src/providers/chat.py b/agent/src/providers/chat.pyindex dd6356810..377695374 100644--- a/agent/src/providers/chat.py+++ b/agent/src/providers/chat.py@@ -296,6 +296,27 @@ class ChatLLM:reasoning_effort=runtime_cfg.langchain_reasoning_effort.strip().lower(),)+ def close(self) -> None:+ """Best-effort release of the underlying provider HTTP client.++ The LangChain adapter (ChatOpenAI and its OpenAI-compatible+ subclasses) owns a pooled ``httpx.Client`` that is not guaranteed to+ be refcount-collected promptly — cyclic references can defer it to a+ GC pass. Long-running callers (swarm workers build one ChatLLM per+ task) must call this when the instance is done with, or sockets+ accumulate in CLOSE-WAIT. Safe to call multiple times; providers+ without a closeable client are a no-op.+ """+ llm = self._llm+ for attr in ("root_client", "root_async_client", "client"):+ client = getattr(llm, attr, None)+ close_fn = getattr(client, "close", None)+ if callable(close_fn):+ try:+ close_fn()+ except Exception:+ logger.debug("ChatLLM.close: failed to close %s", attr, exc_info=True)+def chat(self, messages: List[Dict[str, Any]], tools: Optional[List[Dict[str, Any]]] = None, timeout: Optional[int] = None) -> LLMResponse:"""Call the LLM synchronously.diff --git a/agent/src/swarm/worker.py b/agent/src/swarm/worker.pyindex 8b58cf49f..29deb03f1 100644--- a/agent/src/swarm/worker.py+++ b/agent/src/swarm/worker.py@@ -382,17 +382,75 @@ def run_worker(include_shell_tools: bool = False,grounding_block: str = "",agent_config: AgentConfig | None = None,+) -> WorkerResult:+ """Run one worker task, releasing the per-task LLM client on exit.++ Builds a fresh :class:`ChatLLM` for the task and guarantees it is closed+ even on early returns (timeout, token limit, tool error). The underlying+ LangChain adapter owns a pooled ``httpx.Client`` that is not promptly+ refcount-collected; without this, long-running swarm deployments+ accumulate one CLOSE-WAIT socket per task (#1141).++ Args:+ agent_spec: Agent role specification with tools/skills/model config.+ task: The task to execute, including prompt template.+ upstream_summaries: Summaries from upstream tasks keyed by input_from keys.+ user_vars: User-provided variables for template rendering.+ run_dir: Path to .swarm/runs/{run_id}/ directory.+ event_callback: Optional callback for swarm events.+ include_shell_tools: Whether this worker may register shell tools.+ grounding_block: Optional pre-rendered "Ground Truth" markdown that+ anchors the worker on real recent prices for symbols mentioned in+ ``user_vars``. Forwarded verbatim to :func:`build_worker_prompt`.+ agent_config: Optional resolved agent config carrying remote MCP+ server definitions. Threaded from :class:`SwarmRuntime` and+ consumed by :func:`build_swarm_registry` to merge remote MCP+ tools with the local-tool pool before applying the agent's+ whitelist. ``None`` preserves the prior local-only behavior.++ Returns:+ WorkerResult with status, summary, artifacts, and iteration count.+ """+ llm = ChatLLM(model_name=agent_spec.model_name)+ try:+ return _run_worker_impl(+ agent_spec=agent_spec,+ task=task,+ upstream_summaries=upstream_summaries,+ user_vars=user_vars,+ run_dir=run_dir,+ llm=llm,+ event_callback=event_callback,+ include_shell_tools=include_shell_tools,+ grounding_block=grounding_block,+ agent_config=agent_config,+ )+ finally:+ llm.close()+++def _run_worker_impl(+ agent_spec: SwarmAgentSpec,+ task: SwarmTask,+ upstream_summaries: dict[str, str],+ user_vars: dict[str, str],+ run_dir: Path,+ event_callback: Callable[[SwarmEvent], None] | None = None,+ include_shell_tools: bool = False,+ grounding_block: str = "",+ agent_config: AgentConfig | None = None,+ *,+ llm: ChatLLM,) -> WorkerResult:"""Execute a single worker task using a lightweight ReAct loop.Steps:1. Build filtered ToolRegistry from agent_spec.tools- 2. Create ChatLLM with agent_spec.model_name- 3. Build system prompt with role + upstream summaries + filtered skills- 4. Resolve task.prompt_template with user_vars- 5. Run ReAct loop (for iteration in range(max_iterations))- 6. Write summary to artifacts/{agent_id}/summary.md- 7. Return WorkerResult+ 2. Build system prompt with role + upstream summaries + filtered skills+ 3. Resolve task.prompt_template with user_vars+ 4. Run ReAct loop (for iteration in range(max_iterations))+ 5. Write summary to artifacts/{agent_id}/summary.md+ 6. Return WorkerResultArgs:agent_spec: Agent role specification with tools/skills/model config.@@ -400,6 +458,8 @@ def run_worker(upstream_summaries: Summaries from upstream tasks keyed by input_from keys.user_vars: User-provided variables for template rendering.run_dir: Path to .swarm/runs/{run_id}/ directory.+ llm: Pre-built ChatLLM; ownership stays with the caller+ (:func:`run_worker` closes it in a ``finally``).event_callback: Optional callback for swarm events.include_shell_tools: Whether this worker may register shell tools.grounding_block: Optional pre-rendered "Ground Truth" markdown that@@ -429,10 +489,7 @@ def run_worker(include_shell_tools=include_shell_tools,)- # 2. Create LLM- llm = ChatLLM(model_name=agent_spec.model_name)-- # 3. Build system prompt with filtered skills+ # 2. Build system prompt with filtered skillsskills_loader = SkillsLoader()skill_desc = _filter_skill_descriptions(skills_loader, agent_spec.skills)system_prompt = build_worker_prompt(diff --git a/agent/tests/test_swarm_m4_e2e.py b/agent/tests/test_swarm_m4_e2e.pyindex b703e9a02..076f4db7d 100644--- a/agent/tests/test_swarm_m4_e2e.py+++ b/agent/tests/test_swarm_m4_e2e.py@@ -143,6 +143,9 @@ class _StubChatLLM:def chat(self, messages, tools=None, timeout=None): # pragma: no cover — fallback pathreturn self.stream_chat(messages, tools=tools)+ def close(self) -> None:+ """No-op: the stub owns no HTTP client."""+def _stub_llm_factory(responses: list[LLMResponse]):"""Build a callable that reads as ``ChatLLM(model_name=...)`` would.diff --git a/agent/tests/test_swarm_m5_trust_model.py b/agent/tests/test_swarm_m5_trust_model.pyindex 634489dbc..382501002 100644--- a/agent/tests/test_swarm_m5_trust_model.py+++ b/agent/tests/test_swarm_m5_trust_model.py@@ -297,6 +297,9 @@ class _StubChatLLM:def chat(self, messages, tools=None, timeout=None): # pragma: no coverreturn self.stream_chat(messages, tools=tools)+ def close(self) -> None:+ """No-op: the stub owns no HTTP client."""+def _stub_llm_factory(responses: list[LLMResponse]):stub = _StubChatLLM(responses)diff --git a/agent/tests/test_swarm_output_contract.py b/agent/tests/test_swarm_output_contract.pyindex bbb56d1d2..0e3a26f71 100644--- a/agent/tests/test_swarm_output_contract.py+++ b/agent/tests/test_swarm_output_contract.py@@ -346,6 +346,9 @@ class _ScriptedLLM:def stream_chat(self, messages, tools=None, timeout=None, on_text_chunk=None):return self._responses.pop(0)+ def close(self) -> None:+ """No-op: the scripted stub owns no HTTP client."""+@pytest.mark.parametrize(("result", "raises", "expected_event_status", "expected_worker_status"),@@ -428,3 +431,98 @@ def test_collect_artifacts_rejects_symlink_escape(tmp_path: Path) -> None:pytest.skip("symlinks are not available on this platform")assert _collect_artifacts(artifact_dir) == []+++# ---------------------------------------------------------------------------+# ChatLLM lifecycle: run_worker must close the per-task LLM (regression #1141)+# ---------------------------------------------------------------------------+++def test_run_worker_closes_llm_on_success(monkeypatch, tmp_path: Path) -> None:+ """A successful run must close the per-task ChatLLM exactly once."""+ import src.swarm.worker as worker_mod++ registry = ToolRegistry()+ registry.register(_ResultTool("done", raises=False))+ monkeypatch.setattr(+ worker_mod, "build_swarm_registry", lambda *args, **kwargs: registry+ )+ closed = []++ class _TrackingLLM(_ScriptedLLM):+ def close(self) -> None:+ closed.append(1)++ monkeypatch.setattr(+ worker_mod, "ChatLLM", lambda *args, **kwargs: _TrackingLLM()+ )++ worker_result = run_worker(+ agent_spec=SwarmAgentSpec(+ id="analyst",+ role="Analyst",+ system_prompt="Analyse the result.",+ tools=["market_probe"],+ max_iterations=3,+ ),+ task=SwarmTask(+ id="task", agent_id="analyst", prompt_template="Probe the market."+ ),+ upstream_summaries={},+ user_vars={},+ run_dir=tmp_path,+ )+ assert worker_result.status.value == "completed"+ assert len(closed) == 1, f"close must be called exactly once, got {len(closed)}"+++def test_run_worker_closes_llm_on_tool_exception(monkeypatch, tmp_path: Path) -> None:+ """close() must fire even when the run dies on a tool exception."""+ import src.swarm.worker as worker_mod++ registry = ToolRegistry()+ registry.register(_ResultTool("boom", raises=True))+ monkeypatch.setattr(+ worker_mod, "build_swarm_registry", lambda *args, **kwargs: registry+ )+ closed = []++ class _TrackingLLM(_ScriptedLLM):+ def close(self) -> None:+ closed.append(1)++ monkeypatch.setattr(+ worker_mod, "ChatLLM", lambda *args, **kwargs: _TrackingLLM()+ )++ worker_result = run_worker(+ agent_spec=SwarmAgentSpec(+ id="analyst",+ role="Analyst",+ system_prompt="Analyse the result.",+ tools=["market_probe"],+ max_iterations=3,+ ),+ task=SwarmTask(+ id="task", agent_id="analyst", prompt_template="Probe the market."+ ),+ upstream_summaries={},+ user_vars={},+ run_dir=tmp_path,+ )+ assert worker_result.status.value == "incomplete"+ assert len(closed) == 1, f"close must fire on failure path, got {len(closed)}"+++def test_chatllm_close_is_best_effort_noop_without_client() -> None:+ """ChatLLM.close() must not raise when the provider adapter has no+ closeable client (e.g. scripted/native adapters without root_client)."""+ from src.providers.chat import ChatLLM++ class _BareLLM:+ def bind_tools(self, tools):+ return self++ llm = ChatLLM.__new__(ChatLLM)+ llm._llm = _BareLLM() # no root_client / client attributes+ llm.close() # must not raisediff --git a/agent/tests/test_swarm_worker_content_filter.py b/agent/tests/test_swarm_worker_content_filter.pyindex ff67eefbd..fbbf93d71 100644--- a/agent/tests/test_swarm_worker_content_filter.py+++ b/agent/tests/test_swarm_worker_content_filter.py@@ -48,6 +48,9 @@ class _ScriptedChatLLM:def __call__(self, *args, **kwargs) -> "_ScriptedChatLLM":return self+ def close(self) -> None:+ """No-op: the scripted stub owns no HTTP client."""+def stream_chat(self, messages, tools=None, on_text_chunk=None, timeout=None) -> LLMResponse:diff --git a/agent/tests/test_swarm_worker_stream_retry.py b/agent/tests/test_swarm_worker_stream_retry.pyindex 2be91fdc0..96250899a 100644--- a/agent/tests/test_swarm_worker_stream_retry.py+++ b/agent/tests/test_swarm_worker_stream_retry.py@@ -63,6 +63,10 @@ class _FlakyChatLLM:"""return self+ def close(self) -> None:+ """No-op: the stub owns no HTTP client."""+ return self+def stream_chat(self, messages, tools=None, on_text_chunk=None, timeout=None) -> LLMResponse:"""Raise the next queued error or return the final response.diff --git a/agent/tests/test_swarm_worker_tool_result_truncation.py b/agent/tests/test_swarm_worker_tool_result_truncation.pyindex 6d190883e..f883d95a9 100644--- a/agent/tests/test_swarm_worker_tool_result_truncation.py+++ b/agent/tests/test_swarm_worker_tool_result_truncation.py@@ -60,6 +60,9 @@ class _ScriptedChatLLM:def __call__(self, *args, **kwargs) -> "_ScriptedChatLLM":return self+ def close(self) -> None:+ """No-op: the scripted stub owns no HTTP client."""+def stream_chat(self, messages, tools=None, on_text_chunk=None, timeout=None) -> LLMResponse: