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 handling of per-thread cached data.

Daniel Petti12f955241d7c
diff --git a/src/local_deep_research/utilities/db_utils.py b/src/local_deep_research/utilities/db_utils.py
index 95737623ab..45b99d6c49 100644
--- a/src/local_deep_research/utilities/db_utils.py
+++ b/src/local_deep_research/utilities/db_utils.py
@@ -16,11 +16,12 @@ DATA_DIR = os.path.abspath(
DB_PATH = os.path.join(DATA_DIR, "ldr.db")
-@thread_specific_cache(cache=LRUCache(maxsize=1))
+@thread_specific_cache(cache=LRUCache(maxsize=10))
def get_db_session() -> Session:
"""
Returns:
- The singleton DB session.
+ The singleton DB session for each thread.
+
"""
engine = create_engine(f"sqlite:///{DB_PATH}")
session_class = sessionmaker(bind=engine)
diff --git a/src/local_deep_research/utilities/threading_utils.py b/src/local_deep_research/utilities/threading_utils.py
index b8968a833e..af97b3c220 100644
--- a/src/local_deep_research/utilities/threading_utils.py
+++ b/src/local_deep_research/utilities/threading_utils.py
@@ -1,13 +1,17 @@
import threading
from functools import wraps
-from typing import Any, Callable, Tuple
-from loguru import logger
+from typing import Any, Callable, Tuple, Hashable
+import uuid
+from loguru import logger
from cachetools import cached, keys
from flask import current_app, g
from flask.ctx import AppContext
+g_thread_local_store = threading.local()
+
+
def thread_specific_cache(*args: Any, **kwargs: Any) -> Callable:
"""
A version of `cached()` that is local to a single thread. In other words,
@@ -22,9 +26,18 @@ def thread_specific_cache(*args: Any, **kwargs: Any) -> Callable:
"""
- def _key_func(*args_: Any, **kwargs_: Any) -> Tuple[int, ...]:
+ def _key_func(*args_: Any, **kwargs_: Any) -> Tuple[Hashable, ...]:
base_hash = keys.hashkey(*args_, **kwargs_)
- return (threading.get_ident(),) + base_hash
+
+ if hasattr(g_thread_local_store, "thread_id"):
+ # We already gave this thread a unique ID. Use that.
+ thread_id = g_thread_local_store.thread_id
+ else:
+ # Give this thread a new unique ID.
+ thread_id = uuid.uuid4().hex
+ g_thread_local_store.thread_id = thread_id
+
+ return (thread_id,) + base_hash
return cached(*args, **kwargs, key=_key_func)