~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(library): rollback session in RAG service exception handlers (#3980)
LearningCircuit7db58eb47e90
diff --git a/src/local_deep_research/research_library/services/library_rag_service.py b/src/local_deep_research/research_library/services/library_rag_service.pyindex 769d889e4b..40169873f5 100644--- a/src/local_deep_research/research_library/services/library_rag_service.py+++ b/src/local_deep_research/research_library/services/library_rag_service.py@@ -26,7 +26,7 @@ from ...database.models.library import (RagDocumentStatus,EmbeddingProvider,)-from ...database.session_context import get_user_db_session+from ...database.session_context import get_user_db_session, safe_rollbackfrom ...utilities.type_utils import to_boolfrom ..utils import ensure_in_collectionfrom ...embeddings.splitters import get_text_splitter@@ -769,6 +769,13 @@ class LibraryRAGService:}except Exception as e:+ # The session is shared (thread-local) with the caller.+ # If session.flush() or session.commit() raised, the session+ # is in PendingRollbackError state until rolled back —+ # leaving subsequent operations to cascade. Roll back BEFORE+ # returning the error dict so the caller sees a clean+ # session. (Same pattern as the #3827 fix.)+ safe_rollback(session, "library_rag_service.index_document")logger.exception(f"Error indexing document {document_id} for collection {collection_id}")@@ -916,6 +923,12 @@ class LibraryRAGService:return {"status": "success", "deleted_count": deleted_count}except Exception as e:+ # session.commit() above can raise; without rollback the+ # shared thread-local session stays poisoned for the+ # caller's next operation (issue #3827 pattern).+ safe_rollback(+ session, "library_rag_service.remove_document_from_rag"+ )logger.exception(f"Error removing document {document_id} from collection {collection_id}")diff --git a/tests/research_library/services/test_library_rag_service_rollback.py b/tests/research_library/services/test_library_rag_service_rollback.pynew file mode 100644index 0000000000..35bc20d034--- /dev/null+++ b/tests/research_library/services/test_library_rag_service_rollback.py@@ -0,0 +1,134 @@+"""Regression tests for session-rollback hygiene in LibraryRAGService.++Follow-up to #3827: the service's `index_document` and+`remove_document_from_rag` methods both open a thread-local session,+perform writes (`session.flush()` / `session.commit()`), and catch+exceptions in an outer ``except`` block. Without an explicit rollback+that catch leaves the shared session in ``PendingRollbackError`` state —+every subsequent ORM operation on the same thread cascades.++These tests force an exception inside the try-block and assert that+``session.rollback`` was called before the helper returned its error+dict.+"""++from unittest.mock import MagicMock, patch+++_MOD = "local_deep_research.research_library.services.library_rag_service"+++def _make_service(**overrides):+ """Instantiate LibraryRAGService with all heavy deps mocked out."""+ with (+ patch(f"{_MOD}.LocalEmbeddingManager") as _lem,+ patch(f"{_MOD}.get_user_db_session"),+ patch(f"{_MOD}.FileIntegrityManager"),+ patch(f"{_MOD}.get_text_splitter"),+ ):+ _lem.return_value.embeddings = MagicMock()+ from local_deep_research.research_library.services.library_rag_service import (+ LibraryRAGService,+ )++ defaults = dict(username="testuser", db_password="pw")+ defaults.update(overrides)+ return LibraryRAGService(**defaults)+++def _ctx_yielding(session):+ """Context-manager mock that yields *session*."""+ ctx = MagicMock()+ ctx.__enter__ = MagicMock(return_value=session)+ ctx.__exit__ = MagicMock(return_value=False)+ return ctx+++def _make_doc(text="Long text content for indexing test."):+ doc = MagicMock()+ doc.id = "doc-1"+ doc.text_content = text+ doc.title = "Test"+ doc.filename = None+ doc.original_url = "http://example.com/d"+ doc.authors = None+ doc.published_date = None+ doc.doi = None+ doc.arxiv_id = None+ doc.pmid = None+ doc.pmcid = None+ doc.extraction_method = None+ doc.word_count = None+ return doc+++# ---------------------------------------------------------------------------+# index_document: rollback fires when the inner work raises.+# ---------------------------------------------------------------------------+++@patch(f"{_MOD}.ensure_in_collection")+@patch(f"{_MOD}.get_user_db_session")+def test_index_document_rolls_back_session_on_exception(+ mock_get_session, mock_ensure+):+ """If text-splitting or any inner step raises, the outer except must+ call session.rollback() before returning the error dict."""+ svc = _make_service()++ session = MagicMock()+ mock_get_session.return_value = _ctx_yielding(session)++ session.query.return_value.filter_by.return_value.first.return_value = (+ _make_doc()+ )+ mock_ensure.return_value = MagicMock(indexed=False, chunk_count=0)++ # Force the inner work to raise — text_splitter is patched in+ # _make_service, but make it explicit here.+ svc.text_splitter = MagicMock()+ svc.text_splitter.split_documents.side_effect = RuntimeError(+ "simulated splitter failure"+ )++ result = svc.index_document("doc-1", "coll-1")++ assert result["status"] == "error"+ session.rollback.assert_called_once()+++# ---------------------------------------------------------------------------+# remove_document_from_rag: rollback fires when commit (or anything before+# it) raises.+# ---------------------------------------------------------------------------+++@patch(f"{_MOD}.get_user_db_session")+def test_remove_document_from_rag_rolls_back_session_on_exception(+ mock_get_session,+):+ """If session.commit() (or earlier work) raises, the outer except+ must call session.rollback()."""+ svc = _make_service()++ session = MagicMock()+ mock_get_session.return_value = _ctx_yielding(session)++ # First filter_by chain returns a non-None DocumentCollection so we+ # enter the try block.+ doc_collection = MagicMock(indexed=True, chunk_count=3)+ session.query.return_value.filter_by.return_value.first.return_value = (+ doc_collection+ )++ # Make the embedding-manager's chunk deletion raise inside the try+ # block (mirrors a real DB / FAISS layer failure).+ svc.embedding_manager = MagicMock()+ svc.embedding_manager._delete_chunks_from_db.side_effect = RuntimeError(+ "simulated chunk-delete failure"+ )++ result = svc.remove_document_from_rag("doc-1", "coll-1")++ assert result["status"] == "error"+ session.rollback.assert_called_once()