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.

3.9 KB
Raw Blame

Move downloader.download_with_result outside the database session in download_resource (#6560, #6564). Previously, wrapping _download_pdf inside get_user_db_session(...) pinned a checked-out connection from QueuePool across multi-second HTTP transfers, exhausting the pool and starving concurrent FastAPI request handlers under bulk downloads. Pre-download checks now execute in a brief transaction, the HTTP transfer runs outside any session, and short transactions stage the DownloadAttempt and document records only after download completion. The success path now commits document staging and download queue status separately, widening a crash window that can strand a PROCESSING queue row (recoverable via re-run).

Follow-up review fixes:

  • Flush the new DownloadTracker row before expunging it on the pre-download path, so the DownloadAttempt.url_hash foreign key is satisfied on first-time downloads (with FK enforcement the prior code raised FOREIGN KEY constraint failed).
  • Snapshot target_resource.title into a plain local before staging commit so post-commit text production does not lazy-refresh and re-checkout a QueuePool connection immediately before the arXiv network call / CPU extraction.
  • Wrap the new-tracker INSERT in session.begin_nested() so the unconditional pre-block session.commit() releases the QueuePool connection without conflating the pre-check write with any caller-staged row on the shared thread-local session. The savepoint scopes the pre-check's only write so a failure inside it cannot drag the caller's outer transaction with it, and the structural change is pinned by test_pre_check_savepoint_scopes_new_tracker_write.
  • Stop passing the raw exception text through sanitize_error_for_client() to the caller-visible skip_reason. That function scrubs credential shapes only and explicitly does not strip SQL text, target URLs, or dependency internals — the SQLAlchemy IntegrityError on the staging path surfaced INSERT INTO document_collections ... verbatim into the retry-manager and bulk-stream SSE messages. The exception is now mapped to a fixed safe token via _client_safe_download_message() (e.g. database_constraint, network_reset, pdf_parse_failed); the full sanitized text is preserved server-side in DownloadAttempt.error_message and logger.exception for debugging. The same mapping now covers the sibling text-extraction failure paths (_try_arxiv_text_extraction, _try_api_text_extraction, _fallback_pdf_extraction, whose Failed to save text strings flow into download_as_text and the bulk-stream SSE error field) and the HTML downloader's exception path (via the shared security.client_safe_errors table, which both call sites import so the copies cannot drift again); the CWE-209 guard comments in web/routers/library.py now state the fixed-token guarantee instead of claiming sanitize_error_for_client()-scrubbed is safe. FailureClassifier now recognises the token vocabulary (network_timeout → timeout 30-min instead of unknown_error 1h, plus the database/pdf/ filesystem_error:/download_error: shapes), and RetryManager.record_attempt passes single-token skip_reasons through as error_type instead of collapsing them to the literal "str". The HTML downloader's tokenized failure logs the exception class (exc_class=) for operators while still never logging str(e) or the traceback.
  • Added test_first_time_download_persists_tracker_before_attempt_with_fk_on (FK-ON engine) and test_download_sqlalchemy_exception_does_not_leak_sql_in_skip_reason to pin the two follow-up fixes. The FK-ON test is mutation-verified for the combined savepoint+flush invariant (removing both fails with FOREIGN KEY constraint failed); the explicit session.flush() alone is defense-in-depth over the savepoint-exit auto-flush, and the leak test is mutation-verified against restoring sanitize_error_for_client(str(e)).