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(threading): don't discard the parent session when building a worker context (#5137)

Ehsan794447cbe5e8
diff --git a/changelog.d/+thread-context-parent-session-lifecycle.bugfix.md b/changelog.d/+thread-context-parent-session-lifecycle.bugfix.md
new file mode 100644
index 0000000000..c50dce24fc
--- /dev/null
+++ b/changelog.d/+thread-context-parent-session-lifecycle.bugfix.md
@@ -0,0 +1 @@
+Fixed `thread_context()` rolling back and closing the submitting thread's database session when spawning search workers, which could discard the request's uncommitted work. Worker contexts no longer inherit the parent's session; a worker that needs the database acquires its own.
diff --git a/src/local_deep_research/utilities/threading_utils.py b/src/local_deep_research/utilities/threading_utils.py
index 30002ef344..19956657ef 100644
--- a/src/local_deep_research/utilities/threading_utils.py
+++ b/src/local_deep_research/utilities/threading_utils.py
@@ -10,6 +10,13 @@ from loguru import logger
g_thread_local_store = threading.local()
+# Keys in Flask ``g`` that are created and cleaned up by a
+# ``teardown_appcontext`` handler (see ``web/app_factory.cleanup_db_session``).
+# They must never be copied into a worker context: the value is a resource
+# (a SQLAlchemy session) still owned by the submitting thread, and copying it
+# would let a teardown roll back and close the parent thread's session.
+_TEARDOWN_OWNED_G_KEYS = frozenset({"db_session"})
+
def thread_specific_cache(*args: Any, **kwargs: Any) -> Callable:
"""
@@ -74,8 +81,12 @@ def thread_with_app_context(to_wrap: Callable) -> Callable:
def thread_context() -> AppContext | None:
"""
- Pushes a new app context for a thread that is being spawned to handle the
- current request. Will copy all the global data from the current context.
+ Builds (but does not push) a new app context for a thread that is being
+ spawned to handle the current request, pre-populating its ``g`` with the
+ current context's global data. Teardown-owned resources such as
+ ``db_session`` are not copied, so the worker never shares the submitting
+ thread's session. The caller is responsible for entering the returned
+ context in the worker thread (see :func:`thread_with_app_context`).
Returns:
The new context, or None if no context is active.
@@ -100,8 +111,21 @@ def thread_context() -> AppContext | None:
logger.debug("No current app context, not passing to thread.")
return None
- with context:
- for key, value in global_data.items():
- setattr(g, key, value)
+ # Populate the new context's ``g`` directly instead of pushing the context
+ # on the submitting thread. Pushing and popping it here would fire the app's
+ # ``teardown_appcontext`` handlers on this thread, and a handler that owns
+ # ``db_session`` (``web/app_factory.cleanup_db_session``) rolls back and
+ # closes it. Since ``g`` is shallow-copied, that session is the one the
+ # submitting thread still holds, so the parent's uncommitted work would be
+ # discarded. Teardown-owned keys are excluded for the same reason: a worker
+ # that needs the database acquires its own context-local session (see
+ # ``database/session_context.get_user_db_session``) rather than reusing the
+ # parent's. The worker enters ``with context:`` itself (via
+ # ``thread_with_app_context``), so its own teardown cleans up only what it
+ # created.
+ for key, value in global_data.items():
+ if key in _TEARDOWN_OWNED_G_KEYS:
+ continue
+ setattr(context.g, key, value)
return context
diff --git a/tests/utilities/test_thread_context_session_lifecycle.py b/tests/utilities/test_thread_context_session_lifecycle.py
new file mode 100644
index 0000000000..cb3d6ff91a
--- /dev/null
+++ b/tests/utilities/test_thread_context_session_lifecycle.py
@@ -0,0 +1,286 @@
+"""Session-lifecycle guarantees for ``thread_context()``.
+
+``thread_context()`` builds an app context for a worker thread by copying the
+submitting thread's Flask ``g``. It must not disturb the submitting thread's
+resources while doing so. Previously it pushed and popped a temporary app
+context on the submitting thread to populate ``g``; popping fired the app's
+``teardown_appcontext`` handler (``web/app_factory.cleanup_db_session``), which
+rolled back and closed the copied ``g.db_session`` — the very session the
+submitting thread was still using — discarding its uncommitted work.
+
+These tests are driven against the REAL app factory (``create_app``), so the
+real teardown handler is registered. A hand-built ``Flask(__name__)`` registers
+zero teardown handlers and could not exercise this bug at all. Each of the
+eight guarantees the maintainer asked for on PR #5123 is its own test.
+
+Note on scoping: the submitting thread's own app context legitimately owns
+``g.db_session`` and cleans it up when that context exits (normal Flask
+teardown). The bug is ``thread_context()`` destroying it *mid-request*, so every
+parent-session assertion runs while the submitting context is still active.
+"""
+
+from unittest.mock import MagicMock, patch
+
+import pytest
+from flask import g
+from sqlalchemy import Column, Integer, String, create_engine
+from sqlalchemy.orm import declarative_base, sessionmaker
+
+from local_deep_research.utilities.threading_utils import (
+ thread_context,
+ thread_with_app_context,
+)
+
+Base = declarative_base()
+
+
+class _Widget(Base):
+ __tablename__ = "widget"
+ id = Column(Integer, primary_key=True)
+ name = Column(String)
+
+
+def real_app():
+ """The real application, so ``cleanup_db_session`` teardown is registered."""
+ with patch("local_deep_research.web.app_factory.SocketIOService"):
+ from local_deep_research.web.app_factory import create_app
+
+ app, _ = create_app()
+ app.config["TESTING"] = True
+ app.config["WTF_CSRF_ENABLED"] = False
+ # Guard the premise of every test below: without a real teardown handler,
+ # none of these could reproduce the bug (2026-07-17 retro on #5123).
+ assert app.teardown_appcontext_funcs, (
+ "expected the real app to register teardown_appcontext handlers"
+ )
+ return app
+
+
+def _parent_session_with_pending_work():
+ """A real session holding one flushed-but-uncommitted row."""
+ engine = create_engine("sqlite:///:memory:")
+ Base.metadata.create_all(engine)
+ session = sessionmaker(bind=engine)()
+ session.add(_Widget(name="uncommitted-parent-work"))
+ session.flush() # emit INSERT; still inside the open (uncommitted) transaction
+ assert session.query(_Widget).count() == 1
+ return session
+
+
+def test_creating_context_does_not_commit_rollback_or_close_parent_session(
+ real_app,
+):
+ """Guarantee 1: creating ``thread_context()`` does not commit, roll back,
+ close, or otherwise alter the parent thread's active database session."""
+ parent = _parent_session_with_pending_work()
+ parent.commit = MagicMock(wraps=parent.commit)
+ parent.rollback = MagicMock(wraps=parent.rollback)
+ parent.close = MagicMock(wraps=parent.close)
+
+ with real_app.app_context():
+ g.current_user = "alice"
+ g.db_session = parent
+
+ thread_context()
+
+ # Asserted while the submitting context is still active: the factory
+ # itself must not have touched the parent session.
+ parent.commit.assert_not_called()
+ parent.rollback.assert_not_called()
+ parent.close.assert_not_called()
+
+
+def test_parent_pending_work_survives_context_creation_and_worker_exit(
+ real_app,
+):
+ """Guarantee 2: pending uncommitted work in the parent session remains
+ present after context creation and after the worker context exits."""
+ parent = _parent_session_with_pending_work()
+
+ with real_app.app_context():
+ g.current_user = "alice"
+ g.db_session = parent
+
+ child = thread_context()
+ assert parent.query(_Widget).count() == 1 # after context creation
+
+ # The worker enters and exits its own context; the parent must be
+ # untouched by the worker's teardown too.
+ with child:
+ pass
+ assert parent.query(_Widget).count() == 1 # after worker context exit
+
+ # Still uncommitted and still usable.
+ assert (
+ parent.query(_Widget)
+ .filter_by(name="uncommitted-parent-work")
+ .one()
+ )
+
+
+def test_safe_values_available_inside_worker_context(real_app):
+ """Guarantee 3: safe values such as ``current_user`` and
+ ``settings_snapshot`` are available inside the worker context."""
+ seen = {}
+
+ @thread_with_app_context
+ def worker():
+ seen["current_user"] = g.get("current_user")
+ seen["settings_snapshot"] = g.get("settings_snapshot")
+
+ with real_app.app_context():
+ g.current_user = "alice"
+ g.settings_snapshot = {"llm": "gpt"}
+ ctx = thread_context()
+
+ worker(ctx)
+
+ assert seen["current_user"] == "alice"
+ assert seen["settings_snapshot"] == {"llm": "gpt"}
+
+
+def test_teardown_owned_db_session_not_copied_into_worker(real_app):
+ """Guarantee 4: teardown-owned resources such as ``db_session`` are not
+ copied into the worker context."""
+ parent = _parent_session_with_pending_work()
+ seen = {}
+
+ @thread_with_app_context
+ def worker():
+ seen["has_db_session"] = "db_session" in list(g)
+
+ with real_app.app_context():
+ g.current_user = "alice"
+ g.db_session = parent
+ ctx = thread_context()
+
+ # Not present on the context object even before the worker runs.
+ assert "db_session" not in ctx.g.__dict__
+ worker(ctx)
+ assert seen["has_db_session"] is False
+
+
+def test_worker_needing_db_gets_its_own_session_not_the_parents(real_app):
+ """Guarantee 5: a worker that needs database access obtains its own
+ context-local session rather than reusing the parent session."""
+ from local_deep_research.database import session_context
+
+ parent = _parent_session_with_pending_work()
+ worker_session = MagicMock(name="worker-session")
+ obtained = {}
+
+ with (
+ patch.object(
+ session_context.db_manager, "is_user_connected", return_value=True
+ ),
+ patch.object(
+ session_context.db_manager,
+ "get_session",
+ return_value=worker_session,
+ ),
+ ):
+
+ @thread_with_app_context
+ def worker():
+ # No inherited session, so session_context lazily creates one
+ # keyed on g.current_user via db_manager.
+ obtained["session"] = session_context.get_g_db_session()
+
+ with real_app.app_context():
+ g.current_user = "alice"
+ g.db_session = parent
+ ctx = thread_context()
+
+ worker(ctx)
+
+ assert obtained["session"] is worker_session
+ assert obtained["session"] is not parent
+
+
+def test_worker_teardown_closes_only_worker_owned_resources(real_app):
+ """Guarantee 6: worker teardown closes only worker-owned resources and
+ leaves the parent session usable."""
+ parent = _parent_session_with_pending_work()
+ parent.rollback = MagicMock(wraps=parent.rollback)
+ parent.close = MagicMock(wraps=parent.close)
+ worker_owned = MagicMock(name="worker-owned-session")
+
+ with real_app.app_context():
+ g.current_user = "alice"
+ g.db_session = parent
+ child = thread_context()
+
+ # The worker acquires its OWN session inside its context, then exits.
+ with child:
+ g.db_session = worker_owned
+
+ # Worker's own session was cleaned up by the worker's teardown...
+ worker_owned.rollback.assert_called_once()
+ worker_owned.close.assert_called_once()
+ # ...and the parent session was never touched and is still usable.
+ parent.rollback.assert_not_called()
+ parent.close.assert_not_called()
+ assert parent.query(_Widget).count() == 1
+
+
+def test_exception_inside_worker_preserves_ownership_guarantees(real_app):
+ """Guarantee 7: exception paths inside the worker context preserve the same
+ ownership and cleanup guarantees."""
+ parent = _parent_session_with_pending_work()
+ parent.rollback = MagicMock(wraps=parent.rollback)
+ parent.close = MagicMock(wraps=parent.close)
+ worker_owned = MagicMock(name="worker-owned-session")
+
+ with real_app.app_context():
+ g.current_user = "alice"
+ g.db_session = parent
+ child = thread_context()
+
+ with pytest.raises(ValueError):
+ with child:
+ g.db_session = worker_owned
+ raise ValueError("worker blew up")
+
+ # Even on the exception path, worker-owned resources are cleaned up and
+ # the parent session is untouched and still usable.
+ worker_owned.rollback.assert_called_once()
+ worker_owned.close.assert_called_once()
+ parent.rollback.assert_not_called()
+ parent.close.assert_not_called()
+ assert parent.query(_Widget).count() == 1
+
+
+def test_existing_callers_retain_current_behavior(real_app):
+ """Guarantee 8: the existing ``thread_context()`` callers retain their
+ current behavior after the helper change.
+
+ Every caller (``research_service``, ``auth/routes``, and the two
+ strategies via ``run_parallel_searches``) consumes the context the same
+ way: it passes the returned context to a ``thread_with_app_context``
+ worker that runs under ``with context:``. The worker must therefore still
+ see the propagated safe values and run to completion, exactly as before.
+ """
+ result = {}
+
+ @thread_with_app_context
+ def caller_worker(payload):
+ result["user"] = g.get("current_user")
+ result["snapshot"] = g.get("settings_snapshot")
+ return payload
+
+ with real_app.app_context():
+ g.current_user = "alice"
+ g.settings_snapshot = {"k": "v"}
+ ctx = thread_context()
+
+ returned = caller_worker(ctx, "payload-123")
+
+ assert returned == "payload-123"
+ assert result == {"user": "alice", "snapshot": {"k": "v"}}
+
+
+def test_thread_context_returns_none_without_active_app_context():
+ """Sanity: with no active app context, the factory returns None (unchanged
+ behavior; keeps the None-guard covered)."""
+ assert thread_context() is None