~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): prevent duplicate delete requests and handle concurrent deletions (#5337) (#5338)
S34796c28d8a7
diff --git a/changelog.d/5337.bugfix.md b/changelog.d/5337.bugfix.mdnew file mode 100644index 0000000000..a90a9f000d--- /dev/null+++ b/changelog.d/5337.bugfix.md@@ -0,0 +1 @@+Prevent library delete confirmations from issuing duplicate requests, and make concurrent duplicate document deletions return not found without repeating success logs or RAG cleanup.diff --git a/src/local_deep_research/research_library/deletion/services/document_deletion.py b/src/local_deep_research/research_library/deletion/services/document_deletion.pyindex d3a56c6eac..eb10d2e80e 100644--- a/src/local_deep_research/research_library/deletion/services/document_deletion.py+++ b/src/local_deep_research/research_library/deletion/services/document_deletion.py@@ -7,7 +7,9 @@ Handles:- Remove from collection (unlink or delete if orphaned)"""-from typing import Dict, List, Any+import threading+from contextlib import contextmanager+from typing import Any, Dict, Iterator, List, Tuplefrom loguru import logger@@ -25,6 +27,47 @@ from ....database.session_passwords import capture_request_db_passwordfrom ..utils.cascade_helper import CascadeHelper+# Document deletion is a read-then-delete workflow with filesystem and FAISS+# side effects around the database transaction. Serialize it per user/document+# across service instances so duplicate browser requests cannot both pass the+# initial existence check. The registry is ref-counted so completed document+# IDs do not accumulate for the lifetime of the process.+_document_delete_locks: Dict[Tuple[str, str], threading.Lock] = {}+_document_delete_lock_refs: Dict[Tuple[str, str], int] = {}+_document_delete_locks_guard = threading.Lock()+++@contextmanager+def _hold_document_delete_lock(+ username: str, document_id: str+) -> Iterator[None]:+ """Hold the process-local lock for one user's document deletion."""+ key = (username, document_id)+ with _document_delete_locks_guard:+ lock = _document_delete_locks.get(key)+ if lock is None:+ lock = threading.Lock()+ _document_delete_locks[key] = lock+ _document_delete_lock_refs[key] = (+ _document_delete_lock_refs.get(key, 0) + 1+ )++ try:+ with lock:+ yield+ finally:+ with _document_delete_locks_guard:+ remaining = _document_delete_lock_refs[key] - 1+ if remaining:+ _document_delete_lock_refs[key] = remaining+ else:+ _document_delete_lock_refs.pop(key, None)+ # Identity check protects against future registry maintenance+ # replacing the lock while this holder is winding down.+ if _document_delete_locks.get(key) is lock:+ _document_delete_locks.pop(key, None)++def _is_note_document(session, document) -> bool:"""Return True if the document represents a note (source_type='note').@@ -53,14 +96,32 @@ class DocumentDeletionService:self.username = usernamedef delete_document(self, document_id: str) -> Dict[str, Any]:- """- Delete a document and ALL related data.+ """Delete one document, serializing duplicate in-process requests."""+ with _hold_document_delete_lock(self.username, document_id):+ result, rag_collection_ids = self._delete_document_locked(+ document_id+ )++ # Post-commit FAISS + chunk-row cleanup. Runs after the DB transaction+ # has committed AND the keyed lock is released, allowing concurrent+ # requests to immediately observe "Document not found" without waiting+ # for slow vector-store IO.+ if result.get("deleted"):+ self._purge_document_rag(+ document_id, rag_collection_ids, full_delete=True+ )- This method ensures complete cleanup:+ return result++ def _delete_document_locked(+ self, document_id: str+ ) -> Tuple[Dict[str, Any], List[str]]:+ """Delete a document and all related data while its keyed lock is held.++ This method ensures complete DB/file cleanup:- DocumentChunks (no FK constraint, manual cleanup required)- DocumentBlob (CASCADE handles, but we track for stats)- Filesystem files- - FAISS index entries- DownloadTracker update- DocumentCollection links (CASCADE)- RagDocumentStatus (CASCADE)@@ -69,17 +130,7 @@ class DocumentDeletionService:document_id: ID of the document to deleteReturns:- Dict with deletion details:- {- "deleted": True/False,- "document_id": str,- "title": str,- "blob_deleted": bool,- "blob_size": int,- "chunks_deleted": int,- "collections_unlinked": int,- "error": str (if failed)- }+ Tuple of (result_dict, rag_collection_ids) for post-commit purge."""result: Dict[str, Any] = {"deleted": False,@@ -102,7 +153,7 @@ class DocumentDeletionService:"deleted": False,"document_id": document_id,"error": "Document not found",- }+ }, []# Notes are not deletable via the generic document API.# The Notes-collection deletion guard only blocks the@@ -121,7 +172,7 @@ class DocumentDeletionService:"Use DELETE /api/notes/<id> instead."),"is_note": True,- }+ }, []title = document.title or document.filename or "Untitled"result["title"] = title@@ -195,7 +246,25 @@ class DocumentDeletionService:# 6. Delete the document and all related records (leaves the# DocumentChunk rows for the post-commit cleanup phase).- CascadeHelper.delete_document_completely(session, document_id)+ # The helper's row count is authoritative: a concurrent+ # cross-process request can still win after our initial read+ # because the keyed lock is intentionally process-local.+ # Never report success or purge RAG state when this DELETE+ # affected no document row.+ actually_deleted = CascadeHelper.delete_document_completely(+ session, document_id+ )+ if not actually_deleted:+ logger.debug(+ f"Document {document_id} was not deleted by CascadeHelper (already removed by concurrent request)"+ )+ session.rollback()+ return {+ "deleted": False,+ "document_id": document_id,+ "error": "Document not found",+ }, []+session.commit()result["deleted"] = True@@ -204,6 +273,7 @@ class DocumentDeletionService:f"{result['chunks_deleted']} chunks, "f"{result['blob_size']} bytes blob")+ return result, rag_collection_idsexcept Exception:logger.exception(f"Failed to delete document {document_id}")@@ -212,17 +282,7 @@ class DocumentDeletionService:"deleted": False,"document_id": document_id,"error": "Failed to delete document",- }-- # Post-commit FAISS + chunk-row cleanup. Runs after the deletion- # transaction has committed — releasing SQLite's single write lock —- # so the slow vector-store IO never blocks another writer.- if result["deleted"]:- self._purge_document_rag(- document_id, rag_collection_ids, full_delete=True- )-- return result+ }, []def _purge_document_rag(self,diff --git a/src/local_deep_research/web/static/js/deletion/confirmation_modal.js b/src/local_deep_research/web/static/js/deletion/confirmation_modal.jsindex edddfd7df7..e9c9135e8a 100644--- a/src/local_deep_research/web/static/js/deletion/confirmation_modal.js+++ b/src/local_deep_research/web/static/js/deletion/confirmation_modal.js@@ -74,8 +74,15 @@ function initDeleteConfirmModal() {const confirmBtn = document.getElementById('deleteConfirmBtn');if (confirmBtn) {confirmBtn.addEventListener('click', function() {- if (currentConfirmCallback) {- currentConfirmCallback();+ // Consume the callback before invoking it. This prevents a rapid+ // double-click from confirming the same destructive action twice,+ // and clearing the cancel callback prevents the modal's subsequent+ // hidden event from reporting a successful confirmation as a cancel.+ const callback = currentConfirmCallback;+ currentConfirmCallback = null;+ currentCancelCallback = null;+ if (callback) {+ callback();}hideDeleteModal();});@@ -239,13 +246,22 @@ async function confirmAndRun(options, action) {const confirmationsEnabled = await areConfirmationsEnabled();if (confirmationsEnabled) {- const confirmed = await showDeleteConfirmation(options);+ // showDeleteConfirmation supports direct callers that provide an+ // options.onConfirm callback. DeleteManager also passes that same+ // callback as `action`, so forwarding it here would execute once in+ // the modal and again after the promise resolves. Suppress only the+ // modal-side confirmation callback and run the supplied action exactly+ // once after a positive result. Keep onCancel for direct cancellation+ // handling.+ const confirmationOptions = { ...options, onConfirm: undefined };+ const confirmed = await showDeleteConfirmation(confirmationOptions);if (confirmed) {- action();+ return await action();}- } else {- action();+ return null;}++ return await action();}// Initialize when DOM is readydiff --git a/tests/js/deletion/delete-modules.test.js b/tests/js/deletion/delete-modules.test.jsindex 925c3a4a39..968cd07f25 100644--- a/tests/js/deletion/delete-modules.test.js+++ b/tests/js/deletion/delete-modules.test.js@@ -11,6 +11,10 @@import '@js/utils/format-bytes.js';import '@js/deletion/confirmation_modal.js';+// Preserve the real implementation before the DeleteManager tests replace the+// exported function with an auto-confirm stub.+const realConfirmAndRun = window.DeleteConfirmation.confirmAndRun;+describe('DeleteConfirmation tooltip lookup', () => {it('returns the configured tooltip for a known action', () => {// We test that lookup works without asserting exact tooltip text@@ -40,6 +44,157 @@ describe('DeleteConfirmation tooltip lookup', () => {});});+describe('DeleteConfirmation execution', () => {+ function setupModalDom(options = {}) {+ const autoEmitHidden = options.autoEmitHidden ?? true;+ const originalChildren = Array.from(document.body.childNodes);+ const originalBootstrapDescriptor = Object.getOwnPropertyDescriptor(+ globalThis,+ 'bootstrap',+ );+ document.body.replaceChildren();++ const modal = document.createElement('div');+ modal.id = 'deleteConfirmModal';+ for (const [tag, id] of [+ ['h2', 'deleteModalTitle'],+ ['p', 'deleteModalMessage'],+ ['div', 'deleteModalDetails'],+ ['ul', 'deleteDetailsList'],+ ['div', 'deleteModalWarning'],+ ['span', 'deleteWarningText'],+ ]) {+ const element = document.createElement(tag);+ element.id = id;+ modal.appendChild(element);+ }+ const confirmButton = document.createElement('button');+ confirmButton.id = 'deleteConfirmBtn';+ const confirmButtonText = document.createElement('span');+ confirmButtonText.id = 'deleteConfirmBtnText';+ confirmButton.appendChild(confirmButtonText);+ modal.appendChild(confirmButton);+ document.body.appendChild(modal);++ const modalInstance = {+ show: vi.fn(),+ hide: vi.fn(() => {+ if (autoEmitHidden) {+ modal.dispatchEvent(new Event('hidden.bs.modal'));+ }+ }),+ };+ function Modal() {+ return modalInstance;+ }+ Modal.getInstance = vi.fn(() => modalInstance);+ Object.defineProperty(globalThis, 'bootstrap', {+ configurable: true,+ writable: true,+ value: { Modal },+ });++ document.dispatchEvent(new Event('DOMContentLoaded'));++ const cleanup = () => {+ if (originalBootstrapDescriptor) {+ Object.defineProperty(+ globalThis,+ 'bootstrap',+ originalBootstrapDescriptor,+ );+ } else {+ Reflect.deleteProperty(globalThis, 'bootstrap');+ }+ document.body.replaceChildren(...originalChildren);+ };++ return { modal, confirmButton, modalInstance, cleanup };+ }++ it('runs a confirmed destructive action exactly once', async () => {+ const { confirmButton, cleanup } = setupModalDom();+ const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({+ ok: true,+ json: async () => ({ value: true }),+ });++ try {+ const action = vi.fn(async () => {});+ // DeleteManager passes the same callback both in options.onConfirm+ // and as the explicit action argument.+ const confirmation = realConfirmAndRun(+ { action: 'deleteDocument', onConfirm: action },+ action,+ );+ await new Promise(resolve => setTimeout(resolve, 0));+ confirmButton.click();+ await confirmation;++ expect(action).toHaveBeenCalledTimes(1);+ } finally {+ fetchSpy.mockRestore();+ cleanup();+ }+ });++ it('consumes confirm callback on rapid double-click and clears cancel callback before modal hides', async () => {+ const { modal, confirmButton, cleanup } = setupModalDom({ autoEmitHidden: false });++ try {+ const onConfirm = vi.fn();+ const onCancel = vi.fn();++ const promise = window.DeleteConfirmation.show({+ action: 'deleteDocument',+ onConfirm,+ onCancel,+ });++ // Rapid double click on confirm button+ confirmButton.click();+ confirmButton.click();++ // Simulate Bootstrap emitting hidden.bs.modal after transition+ modal.dispatchEvent(new Event('hidden.bs.modal'));++ const result = await promise;++ expect(result).toBe(true);+ expect(onConfirm).toHaveBeenCalledTimes(1);+ expect(onCancel).not.toHaveBeenCalled();+ } finally {+ cleanup();+ }+ });++ it('invokes cancel callback and resolves false when modal is hidden without confirmation', async () => {+ const { modal, cleanup } = setupModalDom({ autoEmitHidden: false });++ try {+ const onConfirm = vi.fn();+ const onCancel = vi.fn();++ const promise = window.DeleteConfirmation.show({+ action: 'deleteDocument',+ onConfirm,+ onCancel,+ });++ // Simulate closing the modal without confirming (e.g. backdrop/close click)+ modal.dispatchEvent(new Event('hidden.bs.modal'));++ const result = await promise;++ expect(result).toBe(false);+ expect(onConfirm).not.toHaveBeenCalled();+ expect(onCancel).toHaveBeenCalledTimes(1);+ } finally {+ cleanup();+ }+ });+});+// Now load delete_manager and stub its dependencies before importinglet DM;diff --git a/tests/research_library/deletion/services/test_document_deletion.py b/tests/research_library/deletion/services/test_document_deletion.pyindex 184ff32b67..6c59f81964 100644--- a/tests/research_library/deletion/services/test_document_deletion.py+++ b/tests/research_library/deletion/services/test_document_deletion.py@@ -1,5 +1,7 @@"""Tests for DocumentDeletionService."""+import threading+from concurrent.futures import ThreadPoolExecutorfrom unittest.mock import MagicMock, Mock, patch@@ -90,6 +92,139 @@ class TestDocumentDeletionServiceDeleteDocument:mock_purge.assert_called_once()assert mock_purge.call_args.kwargs["full_delete"] is True+ def test_zero_row_delete_returns_not_found_without_commit_or_rag_purge(+ self,+ ):+ """A raced DELETE that affects no document row must not report success."""+ service = DocumentDeletionService(username="testuser")+ mock_session = MagicMock()+ mock_cm = MagicMock()+ mock_cm.__enter__ = Mock(return_value=mock_session)+ mock_cm.__exit__ = Mock(return_value=None)++ mock_doc = MagicMock()+ mock_doc.title = "Raced Document"+ mock_doc.filename = "raced.txt"+ mock_doc.storage_mode = "database"+ mock_doc.file_path = None+ mock_session.get.return_value = mock_doc+ mock_session.query.return_value.filter.return_value.count.return_value = 0++ with (+ patch(+ "local_deep_research.research_library.deletion.services.document_deletion.get_user_db_session",+ return_value=mock_cm,+ ),+ patch(+ "local_deep_research.research_library.deletion.services.document_deletion.CascadeHelper"+ ) as mock_helper,+ patch.object(service, "_purge_document_rag") as mock_purge,+ patch(+ "local_deep_research.research_library.deletion.services.document_deletion.logger.info"+ ) as mock_success_log,+ ):+ mock_helper.get_document_collections.return_value = ["col-1"]+ mock_helper.get_document_blob_size.return_value = 0+ mock_helper.delete_document_completely.return_value = False++ result = service.delete_document("doc-raced")++ assert result == {+ "deleted": False,+ "document_id": "doc-raced",+ "error": "Document not found",+ }+ mock_session.rollback.assert_called_once()+ mock_session.commit.assert_not_called()+ mock_purge.assert_not_called()+ mock_success_log.assert_not_called()++ def test_serializes_same_document_deletes_across_service_instances(self):+ """The second request must wait until the first deletion fully exits."""+ first_service = DocumentDeletionService(username="testuser")+ second_service = DocumentDeletionService(username="testuser")+ first_entered = threading.Event()+ release_first = threading.Event()+ second_call_started = threading.Event()+ second_entered = threading.Event()+ call_count = 0+ call_count_lock = threading.Lock()++ def fake_locked_delete(_service, document_id):+ nonlocal call_count+ with call_count_lock:+ call_count += 1+ call_number = call_count++ if call_number == 1:+ first_entered.set()+ assert release_first.wait(timeout=2)+ return {"deleted": True, "document_id": document_id}, []++ second_entered.set()+ return {+ "deleted": False,+ "document_id": document_id,+ "error": "Document not found",+ }, []++ def run_second_delete():+ second_call_started.set()+ return second_service.delete_document("doc-shared")++ with patch.object(+ DocumentDeletionService,+ "_delete_document_locked",+ fake_locked_delete,+ ):+ with ThreadPoolExecutor(max_workers=2) as pool:+ first = pool.submit(first_service.delete_document, "doc-shared")+ assert first_entered.wait(timeout=1)+ second = pool.submit(run_second_delete)+ assert second_call_started.wait(timeout=1)+ try:+ assert not second_entered.wait(timeout=0.1)+ finally:+ release_first.set()++ assert first.result(timeout=2)["deleted"] is True+ assert second.result(timeout=2)["deleted"] is False++ assert second_entered.is_set()+ assert call_count == 2++ def test_rag_purge_runs_outside_keyed_lock(self):+ """Verify post-commit _purge_document_rag runs after releasing the keyed lock."""+ from local_deep_research.research_library.deletion.services.document_deletion import (+ _document_delete_locks,+ )++ service = DocumentDeletionService(username="testuser")+ lock_held_during_purge = None++ def fake_locked_delete(_document_id):+ return {"deleted": True, "document_id": _document_id}, ["coll1"]++ def fake_purge(_document_id, _collection_ids, *, full_delete):+ nonlocal lock_held_during_purge+ lock_key = ("testuser", "doc-123")+ lock_held_during_purge = lock_key in _document_delete_locks++ with (+ patch.object(+ service,+ "_delete_document_locked",+ side_effect=fake_locked_delete,+ ),+ patch.object(+ service, "_purge_document_rag", side_effect=fake_purge+ ),+ ):+ res = service.delete_document("doc-123")++ assert res["deleted"] is True+ assert lock_held_during_purge is False+def test_handles_exception_gracefully(self):"""Should handle exceptions and rollback."""service = DocumentDeletionService(username="testuser")