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: guard flask_session access with has_request_context() to prevent crash in background threads (#2410)

LearningCircuit7e4bd6599e70
diff --git a/src/local_deep_research/database/session_context.py b/src/local_deep_research/database/session_context.py
index 92837f6f13..582a34c72f 100644
--- a/src/local_deep_research/database/session_context.py
+++ b/src/local_deep_research/database/session_context.py
@@ -7,7 +7,12 @@ import functools
from contextlib import contextmanager
from typing import Callable, Optional
-from flask import g, has_app_context, session as flask_session
+from flask import (
+ g,
+ has_app_context,
+ has_request_context,
+ session as flask_session,
+)
from loguru import logger
from sqlalchemy.orm import Session
@@ -52,7 +57,7 @@ def get_user_db_session(
try:
# Get username from Flask session if not provided (only in Flask context)
- if not username and has_app_context():
+ if not username and has_request_context():
username = flask_session.get("username")
if not username:
@@ -66,15 +71,17 @@ def get_user_db_session(
else:
# Get password if not provided
if not password and has_app_context():
- # Try to get from g
+ # Try to get from g (works with app context)
if hasattr(g, "user_password"):
password = g.user_password
logger.debug(
f"Got password from g.user_password for {username}"
)
- # Try session password store
- elif flask_session.get("session_id"):
- session_id = flask_session.get("session_id")
+
+ # Try session password store (requires request context for flask_session)
+ if not password and has_request_context():
+ session_id = flask_session.get("session_id")
+ if session_id:
logger.debug(
f"Trying session password store for {username}"
)
diff --git a/src/local_deep_research/utilities/db_utils.py b/src/local_deep_research/utilities/db_utils.py
index d9b5597d28..4538516951 100644
--- a/src/local_deep_research/utilities/db_utils.py
+++ b/src/local_deep_research/utilities/db_utils.py
@@ -2,7 +2,12 @@ import functools
from typing import Any, Callable, Dict
from cachetools import LRUCache
-from flask import g, has_app_context, session as flask_session
+from flask import (
+ g,
+ has_app_context,
+ has_request_context,
+ session as flask_session,
+)
from loguru import logger
from sqlalchemy.orm import Session
@@ -96,7 +101,7 @@ def get_settings_manager(
The appropriate settings manager instance.
"""
# If db_session not provided, try to get one
- if db_session is None and username is None and has_app_context():
+ if db_session is None and username is None and has_request_context():
username = flask_session.get("username")
if db_session is None:
diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.py
index ce2c8f55e6..6425db629d 100644
--- a/src/local_deep_research/web/services/research_service.py
+++ b/src/local_deep_research/web/services/research_service.py
@@ -4,7 +4,6 @@ import threading
from datetime import datetime, UTC
from pathlib import Path
-from flask import g, session
from loguru import logger
from ...config.llm_config import get_llm
@@ -1447,10 +1446,7 @@ def run_research_process(
# Save enhanced error report to encrypted database
try:
- # Get username from the research context
- username = getattr(g, "username", None) or session.get(
- "username"
- )
+ # username already available from function scope (line 281)
if username:
from ...storage import get_report_storage
@@ -1492,10 +1488,7 @@ def run_research_process(
# Get existing metadata from database first
existing_metadata = {}
try:
- # Get username from the research context
- username = getattr(g, "username", None) or session.get(
- "username"
- )
+ # username already available from function scope (line 281)
if username:
with get_user_db_session(username) as db_session:
research = (