~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(langgraph-agent): clean up subagent worker sessions (#5336)
LearningCircuitfd5dc745e24e
diff --git a/changelog.d/5336.bugfix.md b/changelog.d/5336.bugfix.mdnew file mode 100644index 0000000000..507a5ff610--- /dev/null+++ b/changelog.d/5336.bugfix.md@@ -0,0 +1 @@+LangGraph subagent timeout accounting now includes worker cleanup, so stalled cleanup can no longer hang research past the configured deadline.diff --git a/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py b/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.pyindex bbb3c669d5..ba924fcb6c 100644--- a/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py@@ -26,6 +26,7 @@ from ...citation_handler import CitationHandlerfrom ...security.egress import EngineClassification, classify_enginefrom ...security import sanitize_error_for_clientfrom ...utilities.thread_context import get_search_context, search_context+from ...database.thread_local_session import thread_cleanupfrom ..tools.fetch import FETCH_MODES, build_fetch_tool, make_library_resolverfrom .base_strategy import (BaseSearchStrategy,@@ -759,19 +760,22 @@ def _make_research_subtopic_tool(task_state_changed.notify_all()try:- # threading.local is NOT inherited by ThreadPoolExecutor- # workers, so re-arm the PEP-578 audit-hook backstop for the- # subagent's lifetime.- from ...security.egress.audit_hook import active_egress_context-- with active_egress_context(egress_context):- # search_context sets the password ContextVar for this- # worker and clears it on exit, preventing pool reuse from- # leaking credentials between tasks.- if captured_search_context is not None:- with search_context(captured_search_context):- return run_subagent(topic)- return run_subagent(topic)+ with thread_cleanup():+ # threading.local is NOT inherited by ThreadPoolExecutor+ # workers, so re-arm the PEP-578 audit-hook backstop for the+ # subagent's lifetime.+ from ...security.egress.audit_hook import (+ active_egress_context,+ )++ with active_egress_context(egress_context):+ # search_context sets the password ContextVar for this+ # worker and clears it on exit, preventing pool reuse from+ # leaking credentials between tasks.+ if captured_search_context is not None:+ with search_context(captured_search_context):+ return run_subagent(topic)+ return run_subagent(topic)finally:with task_state_changed:task_end_times[task_id] = time.monotonic()diff --git a/tests/strategies/test_langgraph_subagent_cleanup.py b/tests/strategies/test_langgraph_subagent_cleanup.pynew file mode 100644index 0000000000..ef75737633--- /dev/null+++ b/tests/strategies/test_langgraph_subagent_cleanup.py@@ -0,0 +1,273 @@+import threading+from unittest.mock import MagicMock, patch++import local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy as mod+import local_deep_research.database.thread_local_session as thread_local_session+from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (+ SearchResultsCollector,+ _make_research_subtopic_tool,+)+from sqlalchemy import create_engine, text+from sqlalchemy.orm import Session+from sqlalchemy.pool import QueuePool+++TEST_TIMEOUT = 0.2+ASSERTION_TIMEOUT = 5.0+++def _build_cleanup_tool():+ collector = SearchResultsCollector([])+ tool = _make_research_subtopic_tool(+ search_engine_name="duckduckgo",+ model=MagicMock(),+ settings_snapshot={"search.tool": {"value": "duckduckgo"}},+ collector=collector,+ max_sub_iterations=8,+ max_subagent_workers=1,+ )+ return tool, MagicMock()+++class TestResearchSubtopicWorkerCleanup:+ def test_successful_worker_runs_canonical_cleanup_on_worker_thread(self):+ # Given: one successful worker and a spy at thread_cleanup's lookup seam.+ caller_thread_id = threading.get_ident()+ worker_thread_ids: list[int] = []+ cleanup_thread_ids: list[int] = []+ tool, agent_mock = _build_cleanup_tool()++ def _invoke(+ _payload: dict[str, list[dict[str, str]]],+ _config: dict[str, int] | None = None,+ ) -> dict[str, list[MagicMock]]:+ worker_thread_ids.append(threading.get_ident())+ return {"messages": [MagicMock(content="worker finding")]}++ def _cleanup() -> None:+ cleanup_thread_ids.append(threading.get_ident())++ agent_mock.invoke.side_effect = _invoke+ with patch.object(+ mod, "_make_web_search_tool", return_value=MagicMock()+ ):+ with patch.object(mod, "build_fetch_tool", return_value=None):+ with patch(+ "langchain.agents.create_agent", return_value=agent_mock+ ):+ with patch(+ "local_deep_research.database.thread_local_session.cleanup_current_thread",+ side_effect=_cleanup,+ ):+ # When: the caller runs a normally completing subagent.+ result = tool.invoke({"subtopics": ["cleanup topic"]})++ # Then: canonical cleanup ran exactly once on that worker, not the caller.+ assert "worker finding" in result+ assert len(worker_thread_ids) == 1+ assert worker_thread_ids[0] != caller_thread_id+ assert cleanup_thread_ids == worker_thread_ids++ def test_timed_out_worker_cleans_up_after_callable_is_released(self):+ # Given: a running worker blocked until the test releases it.+ caller_thread_id = threading.get_ident()+ worker_started = threading.Event()+ release_worker = threading.Event()+ callable_completed = threading.Event()+ cleanup_completed = threading.Event()+ worker_thread_ids: list[int] = []+ cleanup_thread_ids: list[int] = []+ events: list[tuple[str, int]] = []+ tool, agent_mock = _build_cleanup_tool()++ def _invoke(+ _payload: dict[str, list[dict[str, str]]],+ _config: dict[str, int] | None = None,+ ) -> dict[str, list[MagicMock]]:+ worker_thread_ids.append(threading.get_ident())+ worker_started.set()+ _ = release_worker.wait()+ events.append(("callable_completed", threading.get_ident()))+ callable_completed.set()+ return {"messages": [MagicMock(content="late worker finding")]}++ def _cleanup() -> None:+ cleanup_thread_ids.append(threading.get_ident())+ events.append(("cleanup", threading.get_ident()))+ cleanup_completed.set()++ agent_mock.invoke.side_effect = _invoke+ with patch.object(+ mod, "_make_web_search_tool", return_value=MagicMock()+ ):+ with patch.object(mod, "build_fetch_tool", return_value=None):+ with patch(+ "langchain.agents.create_agent", return_value=agent_mock+ ):+ with patch.object(+ mod, "SUBAGENT_TIMEOUT_SECONDS", TEST_TIMEOUT+ ):+ with patch(+ "local_deep_research.database.thread_local_session.cleanup_current_thread",+ side_effect=_cleanup,+ ):+ try:+ # When: the caller times out while the worker remains running.+ result = tool.invoke(+ {"subtopics": ["blocked topic"]}+ )++ # Then: it returns before cleanup; release triggers ordered cleanup.+ assert worker_started.is_set()+ assert worker_thread_ids[0] != caller_thread_id+ assert (+ "Research on 'blocked topic' timed out after"+ in result+ )+ assert cleanup_thread_ids == []+ release_worker.set()+ assert callable_completed.wait(+ ASSERTION_TIMEOUT+ )+ assert cleanup_completed.wait(ASSERTION_TIMEOUT)+ assert cleanup_thread_ids == [+ worker_thread_ids[0]+ ]+ assert events == [+ (+ "callable_completed",+ worker_thread_ids[0],+ ),+ ("cleanup", worker_thread_ids[0]),+ ]+ finally:+ release_worker.set()++ def test_blocked_cleanup_does_not_hold_caller_past_subagent_deadline(self):+ # Given: a completed subagent whose worker cleanup remains blocked.+ cleanup_entered = threading.Event()+ release_cleanup = threading.Event()+ cleanup_completed = threading.Event()+ caller_returned = threading.Event()+ caller_results: list[str] = []+ tool, agent_mock = _build_cleanup_tool()++ def _invoke(+ _payload: dict[str, list[dict[str, str]]],+ _config: dict[str, int] | None = None,+ ) -> dict[str, list[MagicMock]]:+ return {"messages": [MagicMock(content="worker finding")]}++ def _cleanup() -> None:+ cleanup_entered.set()+ _ = release_cleanup.wait()+ cleanup_completed.set()++ def _invoke_tool() -> None:+ caller_results.append(tool.invoke({"subtopics": ["cleanup topic"]}))+ caller_returned.set()++ agent_mock.invoke.side_effect = _invoke+ with patch.object(+ mod, "_make_web_search_tool", return_value=MagicMock()+ ):+ with patch.object(mod, "build_fetch_tool", return_value=None):+ with patch(+ "langchain.agents.create_agent", return_value=agent_mock+ ):+ with patch.object(+ mod, "SUBAGENT_TIMEOUT_SECONDS", TEST_TIMEOUT+ ):+ with patch(+ "local_deep_research.database.thread_local_session.cleanup_current_thread",+ side_effect=_cleanup,+ ):+ # When: a caller invokes the tool while cleanup blocks.+ caller_thread = threading.Thread(+ target=_invoke_tool+ )+ caller_thread.start()+ try:+ assert cleanup_entered.wait(ASSERTION_TIMEOUT)++ # Then: the caller receives the deadline result.+ assert caller_returned.wait(ASSERTION_TIMEOUT)+ assert len(caller_results) == 1+ assert (+ "Research on 'cleanup topic' timed out after"+ in caller_results[0]+ )+ finally:+ release_cleanup.set()+ assert cleanup_completed.wait(ASSERTION_TIMEOUT)+ caller_thread.join(ASSERTION_TIMEOUT)+ assert not caller_thread.is_alive()++ def test_worker_cleanup_closes_retained_pooled_session(self):+ # Given: a one-connection pool and a fresh real thread-local manager.+ engine = create_engine(+ "sqlite://",+ poolclass=QueuePool,+ pool_size=1,+ max_overflow=0,+ connect_args={"check_same_thread": False},+ )+ pool = engine.pool+ assert isinstance(pool, QueuePool)+ manager = thread_local_session.ThreadLocalSessionManager()+ retained_sessions: list[Session] = []+ tool, agent_mock = _build_cleanup_tool()++ def _create_session(_username: str, _password: str) -> Session:+ return Session(engine)++ def _invoke(+ _payload: dict[str, list[dict[str, str]]],+ _config: dict[str, int] | None = None,+ ) -> dict[str, list[MagicMock]]:+ session = thread_local_session.get_metrics_session(+ "cleanup-user", "cleanup-password"+ )+ assert session is not None+ session.execute(text("SELECT 1"))+ retained_sessions.append(session)+ return {"messages": [MagicMock(content="pooled worker finding")]}++ agent_mock.invoke.side_effect = _invoke+ try:+ with patch.object(+ thread_local_session, "thread_session_manager", manager+ ):+ with patch.object(+ thread_local_session.db_manager,+ "open_user_database",+ return_value=engine,+ ):+ with patch.object(+ thread_local_session.db_manager,+ "create_thread_safe_session_for_metrics",+ side_effect=_create_session,+ ):+ with patch.object(+ mod,+ "_make_web_search_tool",+ return_value=MagicMock(),+ ):+ with patch.object(+ mod, "build_fetch_tool", return_value=None+ ):+ with patch(+ "langchain.agents.create_agent",+ return_value=agent_mock,+ ):+ # When: a worker obtains and retains a real pooled Session.+ result = tool.invoke(+ {"subtopics": ["pooled cleanup topic"]}+ )++ # Then: cleanup closed the retained Session's connection before return.+ assert "pooled worker finding" in result+ assert retained_sessions+ assert pool.checkedout() == 0+ finally:+ engine.dispose()