Skip to content

~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.

fix: thread context leak between research tasks (#2105)

LearningCircuit73a83544e981
diff --git a/src/local_deep_research/api/research_functions.py b/src/local_deep_research/api/research_functions.py
index 406c195ab4..e5958ff729 100644
--- a/src/local_deep_research/api/research_functions.py
+++ b/src/local_deep_research/api/research_functions.py
@@ -15,7 +15,7 @@ from ..config.thread_settings import get_setting_from_snapshot
from ..report_generator import IntegratedReportGenerator
from ..search_system import AdvancedSearchSystem
from ..utilities.db_utils import no_db_settings
-from ..utilities.thread_context import set_search_context
+from ..utilities.thread_context import clear_search_context, set_search_context
from ..utilities.search_utilities import remove_think_tags
from .settings_utils import create_settings_snapshot
@@ -276,33 +276,36 @@ def quick_summary(
}
set_search_context(search_context)
- # Remove research_mode from kwargs before passing to _init_search_system
- init_kwargs = {k: v for k, v in kwargs.items() if k != "research_mode"}
- # Make sure username is passed to the system
- init_kwargs["username"] = username
- init_kwargs["research_id"] = research_id
- init_kwargs["research_context"] = search_context
- init_kwargs["search_original_query"] = search_original_query
- system = _init_search_system(llms=llms, **init_kwargs)
-
- # Perform the search and analysis
- results = system.analyze_topic(query)
-
- # Extract the summary from the current knowledge
- if results and "current_knowledge" in results:
- summary = results["current_knowledge"]
- else:
- summary = "Unable to generate summary for the query."
+ try:
+ # Remove research_mode from kwargs before passing to _init_search_system
+ init_kwargs = {k: v for k, v in kwargs.items() if k != "research_mode"}
+ # Make sure username is passed to the system
+ init_kwargs["username"] = username
+ init_kwargs["research_id"] = research_id
+ init_kwargs["research_context"] = search_context
+ init_kwargs["search_original_query"] = search_original_query
+ system = _init_search_system(llms=llms, **init_kwargs)
+
+ # Perform the search and analysis
+ results = system.analyze_topic(query)
+
+ # Extract the summary from the current knowledge
+ if results and "current_knowledge" in results:
+ summary = results["current_knowledge"]
+ else:
+ summary = "Unable to generate summary for the query."
- # Prepare the return value
- return {
- "summary": summary,
- "findings": results.get("findings", []),
- "iterations": results.get("iterations", 0),
- "questions": results.get("questions", {}),
- "formatted_findings": results.get("formatted_findings", ""),
- "sources": results.get("all_links_of_system", []),
- }
+ # Prepare the return value
+ return {
+ "summary": summary,
+ "findings": results.get("findings", []),
+ "iterations": results.get("iterations", 0),
+ "questions": results.get("questions", {}),
+ "formatted_findings": results.get("formatted_findings", ""),
+ "sources": results.get("all_links_of_system", []),
+ }
+ finally:
+ clear_search_context()
@no_db_settings
@@ -501,29 +504,32 @@ def detailed_research(
}
set_search_context(search_context)
- # Initialize system
- system = _init_search_system(retrievers=retrievers, llms=llms, **kwargs)
+ try:
+ # Initialize system
+ system = _init_search_system(retrievers=retrievers, llms=llms, **kwargs)
- # Perform detailed research
- results = system.analyze_topic(query)
+ # Perform detailed research
+ results = system.analyze_topic(query)
- # Return comprehensive results
- return {
- "query": query,
- "research_id": research_id,
- "summary": results.get("current_knowledge", ""),
- "findings": results.get("findings", []),
- "iterations": results.get("iterations", 0),
- "questions": results.get("questions", {}),
- "formatted_findings": results.get("formatted_findings", ""),
- "sources": results.get("all_links_of_system", []),
- "metadata": {
- "timestamp": datetime.now(UTC).isoformat(),
- "search_tool": kwargs.get("search_tool", "auto"),
- "iterations_requested": kwargs.get("iterations", 1),
- "strategy": kwargs.get("search_strategy", "source_based"),
- },
- }
+ # Return comprehensive results
+ return {
+ "query": query,
+ "research_id": research_id,
+ "summary": results.get("current_knowledge", ""),
+ "findings": results.get("findings", []),
+ "iterations": results.get("iterations", 0),
+ "questions": results.get("questions", {}),
+ "formatted_findings": results.get("formatted_findings", ""),
+ "sources": results.get("all_links_of_system", []),
+ "metadata": {
+ "timestamp": datetime.now(UTC).isoformat(),
+ "search_tool": kwargs.get("search_tool", "auto"),
+ "iterations_requested": kwargs.get("iterations", 1),
+ "strategy": kwargs.get("search_strategy", "source_based"),
+ },
+ }
+ finally:
+ clear_search_context()
@no_db_settings
diff --git a/src/local_deep_research/utilities/thread_context.py b/src/local_deep_research/utilities/thread_context.py
index 20e1cade32..c99caf0ea3 100644
--- a/src/local_deep_research/utilities/thread_context.py
+++ b/src/local_deep_research/utilities/thread_context.py
@@ -36,6 +36,18 @@ def set_search_context(context: Dict[str, Any]) -> None:
_g_thread_data.context = context.copy()
+def clear_search_context() -> None:
+ """
+ Clears the research context for this thread.
+
+ Should be called in a finally block after set_search_context() to prevent
+ context from leaking to subsequent tasks when threads are reused in a pool.
+ """
+ global _g_thread_data
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+
def get_search_context() -> Dict[str, Any] | None:
"""
Gets the current research context for this thread.