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: prevent context leaks in decorator and subscription scheduler

LearningCircuitdf6d9a08f46b
diff --git a/src/local_deep_research/config/thread_settings.py b/src/local_deep_research/config/thread_settings.py
index 5adc1ea36f..0cc467a27f 100644
--- a/src/local_deep_research/config/thread_settings.py
+++ b/src/local_deep_research/config/thread_settings.py
@@ -26,6 +26,16 @@ def set_settings_context(settings_context):
_thread_local.settings_context = settings_context
+def clear_settings_context():
+ """Clear the settings context for the current thread.
+
+ Should be called in a finally block after set_settings_context() to prevent
+ context from leaking to subsequent tasks when threads are reused in a pool.
+ """
+ if hasattr(_thread_local, "settings_context"):
+ del _thread_local.settings_context
+
+
def get_settings_context():
"""Get the settings context for the current thread."""
if hasattr(_thread_local, "settings_context"):
diff --git a/src/local_deep_research/news/subscription_manager/scheduler.py b/src/local_deep_research/news/subscription_manager/scheduler.py
index 7a5cfe4ecd..5fed1f733e 100644
--- a/src/local_deep_research/news/subscription_manager/scheduler.py
+++ b/src/local_deep_research/news/subscription_manager/scheduler.py
@@ -1291,7 +1291,10 @@ class NewsScheduler:
# Use programmatic API with settings context
from ...api.research_functions import quick_summary
- from ...config.thread_settings import set_settings_context
+ from ...config.thread_settings import (
+ clear_settings_context,
+ set_settings_context,
+ )
# Create and set settings context for this thread
class SettingsContext:
@@ -1350,6 +1353,8 @@ class NewsScheduler:
logger.exception(
f"Error triggering research for subscription {subscription['id']}: {e}"
)
+ finally:
+ clear_settings_context()
def _store_research_result(
self,
diff --git a/src/local_deep_research/utilities/thread_context.py b/src/local_deep_research/utilities/thread_context.py
index 44dabca6ee..9952fdd283 100644
--- a/src/local_deep_research/utilities/thread_context.py
+++ b/src/local_deep_research/utilities/thread_context.py
@@ -30,12 +30,24 @@ def set_search_context(context: Dict[str, Any]) -> None:
"""
global _g_thread_data
if hasattr(_g_thread_data, "context"):
- logger.warning(
+ logger.debug(
"Context already set for this thread. It will be overwritten."
)
_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.
@@ -94,6 +106,10 @@ def preserve_research_context(func: Callable) -> Callable:
if context is not None:
set_search_context(context)
- return func(*args, **kwargs)
+ try:
+ return func(*args, **kwargs)
+ finally:
+ if context is not None:
+ clear_search_context()
return wrapper