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(download): commit mark_success/mark_failure on caller's session (#6557)

Scaeb314e21ba
diff --git a/changelog.d/6557.bugfix.md b/changelog.d/6557.bugfix.md
new file mode 100644
index 0000000000..3d3aab1627
--- /dev/null
+++ b/changelog.d/6557.bugfix.md
@@ -0,0 +1,9 @@
+Fixed `status_tracker.mark_failure` / `mark_success` leaving writes
+uncommitted on the scheduler's thread-local session, which held the
+SQLite write lock across the multi-minute PDF download loop and
+starved every competing writer (rate-limit tracker, `token_counter`,
+`search_tracker`, backup). They now commit on the passed-in session
+by default, with an opt-out via `commit=False`. Companion PRs #6566
+and #6720 address the rate-limit tracker and the scheduler session
+split respectively; this change is the source-of-truth fix that
+stops the uncommitted writes from being created in the first place.
diff --git a/src/local_deep_research/library/download_management/status_tracker.py b/src/local_deep_research/library/download_management/status_tracker.py
index 12c1da8583..6d17b710c4 100644
--- a/src/local_deep_research/library/download_management/status_tracker.py
+++ b/src/local_deep_research/library/download_management/status_tracker.py
@@ -95,6 +95,7 @@ class ResourceStatusTracker:
resource_id: int,
failure: BaseFailure,
session: Optional[Session] = None,
+ commit: bool = True,
) -> None:
"""
Mark a resource as failed with classification.
@@ -103,9 +104,15 @@ class ResourceStatusTracker:
resource_id: Resource identifier
failure: Classified failure object
session: Optional existing database session to reuse
+ commit: If True (default) and ``session`` is provided, commit
+ after applying the status update. Set to False when the
+ caller manages the transaction (e.g. batching multiple
+ status updates). The internal-session path always commits.
"""
if session is not None:
self._apply_failure(session, resource_id, failure)
+ if commit:
+ session.commit()
return
with self._get_session() as session:
@@ -185,7 +192,10 @@ class ResourceStatusTracker:
)
def mark_success(
- self, resource_id: int, session: Optional[Session] = None
+ self,
+ resource_id: int,
+ session: Optional[Session] = None,
+ commit: bool = True,
) -> None:
"""
Mark a resource as successfully downloaded.
@@ -193,9 +203,15 @@ class ResourceStatusTracker:
Args:
resource_id: Resource identifier
session: Optional existing database session to reuse
+ commit: If True (default) and ``session`` is provided, commit
+ after applying the status update. Set to False when the
+ caller manages the transaction (e.g. batching multiple
+ status updates). The internal-session path always commits.
"""
if session is not None:
self._apply_success(session, resource_id)
+ if commit:
+ session.commit()
return
with self._get_session() as session:
diff --git a/tests/conftest.py b/tests/conftest.py
index 69e5a9a363..d8a7b20769 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -335,6 +335,17 @@ def cleanup_database_connections():
# Dispose auth engine so it will be recreated with correct path
dispose_auth_engine()
+ try:
+ from local_deep_research.utilities.db_utils import (
+ cleanup_cached_user_sessions_current_thread,
+ _get_cached_user_session,
+ )
+
+ cleanup_cached_user_sessions_current_thread()
+ if hasattr(_get_cached_user_session, "cache"):
+ _get_cached_user_session.cache.clear()
+ except (ImportError, AttributeError):
+ pass
yield
# Close all connections after test
@@ -343,6 +354,18 @@ def cleanup_database_connections():
# Dispose auth engine after test
dispose_auth_engine()
+ try:
+ from local_deep_research.utilities.db_utils import (
+ cleanup_cached_user_sessions_current_thread,
+ _get_cached_user_session,
+ )
+
+ cleanup_cached_user_sessions_current_thread()
+ if hasattr(_get_cached_user_session, "cache"):
+ _get_cached_user_session.cache.clear()
+ except (ImportError, AttributeError):
+ pass
+
@pytest.fixture(autouse=True)
def _legacy_bare_username_auth(request):
diff --git a/tests/library/download_management/test_status_tracker_extended.py b/tests/library/download_management/test_status_tracker_extended.py
index 25884705cf..e56464df11 100644
--- a/tests/library/download_management/test_status_tracker_extended.py
+++ b/tests/library/download_management/test_status_tracker_extended.py
@@ -399,12 +399,18 @@ class TestStatusTracking:
mock_session = MagicMock()
mock_session.query.return_value.filter_by.return_value.first.return_value = None
- # Pass session explicitly
- tracker.mark_failure(1, failure, session=mock_session)
+ # Pass session explicitly with commit=False (caller-managed commits).
+ tracker.mark_failure(1, failure, session=mock_session, commit=False)
- # Should not commit or close when session is provided
+ # Should not commit or close when session is provided and commit=False
mock_session.commit.assert_not_called()
+ # By default, mark_failure DOES commit on the external session to
+ # avoid leaving dirty writes that block other writers (regression
+ # coverage for the scheduler-then-metrics deadlock).
+ tracker.mark_failure(1, failure, session=mock_session)
+ mock_session.commit.assert_called_once()
+
@patch("local_deep_research.database.encrypted_db.db_manager")
@patch("local_deep_research.library.download_management.models.Base")
def test_failed_resource_count(self, mock_base, mock_db_manager):
@@ -573,10 +579,16 @@ class TestCleanupOperations:
with patch.object(
tracker, "_get_session", return_value=mock_session
):
- # Session provided - no commit
- tracker.mark_failure(1, failure, session=mock_session)
+ # Session provided with commit=False — no commit
+ tracker.mark_failure(
+ 1, failure, session=mock_session, commit=False
+ )
mock_session.commit.assert_not_called()
+ # Default commit=True commits on the external session
+ tracker.mark_failure(1, failure, session=mock_session)
+ mock_session.commit.assert_called_once()
+
@patch("local_deep_research.database.encrypted_db.db_manager")
@patch("local_deep_research.library.download_management.models.Base")
def test_database_error_handling(self, mock_base, mock_db_manager):
@@ -897,3 +909,203 @@ class TestResourceNotFound:
# No changes should be made
mock_session.add.assert_not_called()
+
+
+class TestMarkFailureCommitsByDefault:
+ """Regression coverage for the scheduler-then-metrics deadlock root cause.
+
+ The original ``mark_failure`` / ``mark_success`` skipped ``session.commit()``
+ when an external session was passed, leaving dirty writes on the caller's
+ thread-local session. The scheduler's long-running ``download_as_text``
+ loop accumulated those dirty writes for minutes, holding a SQLite write
+ transaction and starving the concurrent research-thread metrics writes
+ (``token_counter``, ``search_tracker``) until they timed out on
+ ``busy_timeout``.
+
+ The fix made commit the default. These tests use a real SQLite session
+ to verify the data is actually persisted after ``mark_failure`` /
+ ``mark_success`` return, so a regression to the old "leave it to the
+ caller" semantics would break them.
+ """
+
+ def _make_session(self):
+ """Build a real in-memory SQLite session with the model registered."""
+ from sqlalchemy import create_engine
+ from sqlalchemy.orm import sessionmaker
+
+ from local_deep_research.library.download_management.models import (
+ Base,
+ ResourceDownloadStatus,
+ )
+
+ engine = create_engine("sqlite:///:memory:")
+ Base.metadata.create_all(engine)
+ session = sessionmaker(bind=engine)()
+ return session, engine, ResourceDownloadStatus, sessionmaker
+
+ def test_mark_failure_persists_to_real_db_with_external_session(self):
+ """mark_failure must commit so the row is visible after return."""
+ session, engine, ResourceDownloadStatus, sessionmaker = (
+ self._make_session()
+ )
+
+ # Bypass __init__ to skip the db_manager.open_user_database path;
+ # _apply_failure does not need the tracker to be fully wired up.
+ tracker = ResourceStatusTracker.__new__(ResourceStatusTracker)
+
+ from local_deep_research.library.download_management.failure_classifier import (
+ PermanentFailure,
+ )
+
+ tracker.mark_failure(
+ resource_id=42,
+ failure=PermanentFailure("not_found", "Resource not found"),
+ session=session,
+ )
+
+ # If commit was skipped, the row would be in the transaction but not
+ # visible to a fresh query on the same engine (no autoflush).
+ with sessionmaker(bind=engine)() as probe:
+ row = (
+ probe.query(ResourceDownloadStatus)
+ .filter_by(resource_id=42)
+ .first()
+ )
+ assert row is not None, (
+ "mark_failure must commit when given an external session — "
+ "otherwise the scheduler thread holds a SQLite write lock and "
+ "starves the concurrent research metrics thread."
+ )
+ assert row.status == "permanently_failed"
+ assert row.failure_type == "not_found"
+ assert row.total_retry_count == 1
+
+ def test_mark_success_persists_to_real_db_with_external_session(self):
+ """mark_success must commit so the row is visible after return."""
+ session, engine, ResourceDownloadStatus, sessionmaker = (
+ self._make_session()
+ )
+
+ # Seed a pre-existing temporarily_failed row that mark_success should
+ # promote to "completed".
+ existing = ResourceDownloadStatus(
+ resource_id=7,
+ status="temporarily_failed",
+ failure_type="timeout",
+ failure_message="slow",
+ total_retry_count=2,
+ )
+ session.add(existing)
+ session.commit()
+
+ tracker = ResourceStatusTracker.__new__(ResourceStatusTracker)
+ tracker.mark_success(resource_id=7, session=session)
+
+ with sessionmaker(bind=engine)() as probe:
+ row = (
+ probe.query(ResourceDownloadStatus)
+ .filter_by(resource_id=7)
+ .first()
+ )
+ assert row is not None
+ assert row.status == "completed", (
+ "mark_success must commit when given an external session — the "
+ "status update must persist so subsequent retry eligibility "
+ "checks see it."
+ )
+ assert row.failure_type is None
+ assert row.failure_message is None
+
+ def test_mark_failure_commit_false_does_not_persist(self):
+ """Opt-out path (commit=False) leaves the row in the transaction.
+
+ This is the contract callers rely on when batching multiple status
+ updates in a single transaction. A regression that forced commit=True
+ would silently break those callers.
+ """
+ session, engine, ResourceDownloadStatus, sessionmaker = (
+ self._make_session()
+ )
+
+ tracker = ResourceStatusTracker.__new__(ResourceStatusTracker)
+
+ from local_deep_research.library.download_management.failure_classifier import (
+ TemporaryFailure,
+ )
+
+ tracker.mark_failure(
+ resource_id=99,
+ failure=TemporaryFailure("timeout", "slow", timedelta(minutes=5)),
+ session=session,
+ commit=False,
+ )
+
+ with sessionmaker(bind=engine)() as probe:
+ row = (
+ probe.query(ResourceDownloadStatus)
+ .filter_by(resource_id=99)
+ .first()
+ )
+ assert row is None, (
+ "commit=False must not flush; the row should still be in the "
+ "caller's open transaction."
+ )
+
+ # Explicit caller commit now persists it.
+ session.commit()
+ with sessionmaker(bind=engine)() as probe:
+ row = (
+ probe.query(ResourceDownloadStatus)
+ .filter_by(resource_id=99)
+ .first()
+ )
+ assert row is not None
+ assert row.status == "temporarily_failed"
+
+ def test_mark_success_commit_false_does_not_persist(self):
+ """Opt-out path (commit=False) leaves mark_success in the transaction.
+
+ Verifies that passing commit=False to mark_success does not commit
+ immediately; a second probe session sees the previous state until
+ the caller explicitly commits.
+ """
+ session, engine, ResourceDownloadStatus, sessionmaker = (
+ self._make_session()
+ )
+
+ existing = ResourceDownloadStatus(
+ resource_id=88,
+ status="temporarily_failed",
+ failure_type="timeout",
+ failure_message="slow",
+ total_retry_count=1,
+ )
+ session.add(existing)
+ session.commit()
+
+ tracker = ResourceStatusTracker.__new__(ResourceStatusTracker)
+ tracker.mark_success(resource_id=88, session=session, commit=False)
+
+ with sessionmaker(bind=engine)() as probe:
+ row = (
+ probe.query(ResourceDownloadStatus)
+ .filter_by(resource_id=88)
+ .first()
+ )
+ assert row is not None
+ assert row.status == "temporarily_failed", (
+ "commit=False must not flush; probe should still see the pre-existing state."
+ )
+
+ # Explicit caller commit now persists the success state.
+ session.commit()
+ with sessionmaker(bind=engine)() as probe:
+ row = (
+ probe.query(ResourceDownloadStatus)
+ .filter_by(resource_id=88)
+ .first()
+ )
+ assert row is not None
+ assert row.status == "completed"
+ assert row.failure_type is None
+ assert row.failure_message is None
diff --git a/tests/utilities/test_db_utils.py b/tests/utilities/test_db_utils.py
index 5ee8907182..e5270cb05e 100644
--- a/tests/utilities/test_db_utils.py
+++ b/tests/utilities/test_db_utils.py
@@ -55,7 +55,10 @@ class TestGetDbSession:
) as mock_manager:
mock_manager.get_session.return_value = mock_session
- result = get_db_session(username="testuser")
+ # Unique _namespace to avoid thread_specific_cache hits.
+ result = get_db_session(
+ username="testuser", _namespace="test_username_provided"
+ )
assert result == mock_session
mock_manager.get_session.assert_called_with("testuser")