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(db): session-rollback follow-ups (download_resource auto-index + test-helper patch leak) (#4862)

LearningCircuit098bbc849140
diff --git a/changelog.d/4862.bugfix.md b/changelog.d/4862.bugfix.md
new file mode 100644
index 0000000000..ab9d44db8d
--- /dev/null
+++ b/changelog.d/4862.bugfix.md
@@ -0,0 +1 @@
+A rare connection-level failure while auto-indexing a freshly downloaded document no longer leaves the request's database session in a state that fails the next operation. The best-effort auto-index step now recovers the shared session if its lookup fails, continuing the session-rollback hardening from the previous release.
diff --git a/src/local_deep_research/research_library/services/download_service.py b/src/local_deep_research/research_library/services/download_service.py
index cbab04dbfe..a7dd01a79b 100644
--- a/src/local_deep_research/research_library/services/download_service.py
+++ b/src/local_deep_research/research_library/services/download_service.py
@@ -659,6 +659,17 @@ class DownloadService:
self.password,
)
except Exception:
+ # The Document SELECT above runs on the shared session; a
+ # connection-level failure there leaves it needing a
+ # rollback. Auto-indexing is best-effort so we swallow and
+ # return — the with-block exits normally and
+ # get_user_db_session's rollback never fires. Recover the
+ # session here so a later op on it (same request/thread)
+ # doesn't cascade. (No-op for the other failures reachable
+ # here: get_default_library_id self-heals via its own inner
+ # get_user_db_session block, and trigger_auto_index does its
+ # DB work off-thread, so neither dirties this session.)
+ safe_rollback(session, "download_resource auto-index")
logger.exception("Failed to trigger auto-indexing")
return success, skip_reason
diff --git a/tests/research_library/routes/test_rag_routes_coverage.py b/tests/research_library/routes/test_rag_routes_coverage.py
index 212490f8e8..975d374e5e 100644
--- a/tests/research_library/routes/test_rag_routes_coverage.py
+++ b/tests/research_library/routes/test_rag_routes_coverage.py
@@ -203,7 +203,10 @@ def _auth_client(
},
)
finally:
- for p in patches:
+ # Stop in reverse start order so nested patches of the same target
+ # (e.g. extra_patches re-patching get_user_db_session, already patched
+ # by the base list) unwind correctly instead of leaking the base mock.
+ for p in reversed(patches):
p.stop()
diff --git a/tests/research_library/routes/test_rag_routes_deep_coverage.py b/tests/research_library/routes/test_rag_routes_deep_coverage.py
index cb40d39a70..3e59ce23b5 100644
--- a/tests/research_library/routes/test_rag_routes_deep_coverage.py
+++ b/tests/research_library/routes/test_rag_routes_deep_coverage.py
@@ -194,7 +194,11 @@ def _auth_client(
sess["session_id"] = "test-session-id"
yield client, {"db_session": db_session, "settings": mock_sm}
finally:
- for p in patches:
+ # Stop in reverse start order so nested patches of the same target
+ # (e.g. a test's extra_patches re-patching get_user_db_session, which
+ # the base list already patches) unwind correctly. Forward order
+ # restores the base patch's mock instead of the original and leaks it.
+ for p in reversed(patches):
p.stop()
diff --git a/tests/research_library/routes/test_rag_routes_gaps_coverage.py b/tests/research_library/routes/test_rag_routes_gaps_coverage.py
index 8ab41c8a67..e7589c4a4a 100644
--- a/tests/research_library/routes/test_rag_routes_gaps_coverage.py
+++ b/tests/research_library/routes/test_rag_routes_gaps_coverage.py
@@ -169,7 +169,10 @@ def _auth_client(
{"db_session": db_session, "settings": mock_sm},
)
finally:
- for p in patches:
+ # Stop in reverse start order so nested patches of the same target
+ # (e.g. extra_patches re-patching get_user_db_session, already patched
+ # by the base list) unwind correctly instead of leaking the base mock.
+ for p in reversed(patches):
p.stop()
diff --git a/tests/research_library/routes/test_rag_routes_indexing_coverage.py b/tests/research_library/routes/test_rag_routes_indexing_coverage.py
index cc9ec8793d..d022eb6c13 100644
--- a/tests/research_library/routes/test_rag_routes_indexing_coverage.py
+++ b/tests/research_library/routes/test_rag_routes_indexing_coverage.py
@@ -167,7 +167,10 @@ def _auth_client(
sess["session_id"] = "test-session-id"
yield client, {"db_session": db_session, "settings": mock_sm}
finally:
- for p in patches:
+ # Stop in reverse start order so nested patches of the same target
+ # (e.g. extra_patches re-patching get_user_db_session, already patched
+ # by the base list) unwind correctly instead of leaking the base mock.
+ for p in reversed(patches):
p.stop()
diff --git a/tests/research_library/routes/test_rag_routes_upload_coverage.py b/tests/research_library/routes/test_rag_routes_upload_coverage.py
index 94ee73f7ec..d910cf5723 100644
--- a/tests/research_library/routes/test_rag_routes_upload_coverage.py
+++ b/tests/research_library/routes/test_rag_routes_upload_coverage.py
@@ -180,7 +180,10 @@ def _auth_client(
sess["session_id"] = "test-session-id"
yield client, {"db_session": db_session, "settings": mock_sm}
finally:
- for p in patches:
+ # Stop in reverse start order so nested patches of the same target
+ # (e.g. extra_patches re-patching get_user_db_session, already patched
+ # by the base list) unwind correctly instead of leaking the base mock.
+ for p in reversed(patches):
p.stop()