DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(rag): run document parsing off the event loop (KB indexing stalls all requests) (#847)
Wong026ebc585a72
diff --git a/deeptutor/services/rag/pipelines/llamaindex/document_loader.py b/deeptutor/services/rag/pipelines/llamaindex/document_loader.pyindex b3175d56e..471d8f514 100644--- a/deeptutor/services/rag/pipelines/llamaindex/document_loader.py+++ b/deeptutor/services/rag/pipelines/llamaindex/document_loader.py@@ -11,6 +11,7 @@ into the multimodal ``ImageNode`` path below.from __future__ import annotations+import asyncioimport base64from dataclasses import dataclassimport logging@@ -67,7 +68,13 @@ class LlamaIndexDocumentLoader:for file_path_str in classification.parser_files:file_path = Path(file_path_str)self.logger.info(f"Parsing document: {file_path.name}")- text, extracted_images = self._parse_document(file_path)+ # MinerU cloud parsing blocks end to end (upload + 300s polling ++ # archive download) on a synchronous httpx.Client — running it on+ # the event loop stalls every other request for the whole PDF+ # (same class of bug as upstream #761/#777). Hand it to a thread.+ text, extracted_images = await asyncio.to_thread(+ self._parse_document, file_path+ )self._append_if_nonempty(documents, file_path, text)image_sources.extend(extracted_images)diff --git a/tests/services/rag/test_llamaindex_document_loader.py b/tests/services/rag/test_llamaindex_document_loader.pyindex 92cf9c2ca..3a1c7bbcf 100644--- a/tests/services/rag/test_llamaindex_document_loader.py+++ b/tests/services/rag/test_llamaindex_document_loader.py@@ -12,6 +12,7 @@ from __future__ import annotationsimport asynciofrom pathlib import Path+import threadingimport pytest@@ -68,6 +69,48 @@ def test_loader_routes_parser_files_through_active_parse_engine(assert "Block two" in by_name["paper.pdf"]+def test_loader_keeps_event_loop_responsive_while_parser_blocks(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ pytest.importorskip("llama_index.core")+ import deeptutor.services.parsing as parsing+ from deeptutor.services.parsing.types import ParsedDocument+ from deeptutor.services.rag.pipelines.llamaindex.document_loader import (+ LlamaIndexDocumentLoader,+ )++ pdf_path = tmp_path / "slow.pdf"+ pdf_path.write_bytes(b"stub")+ parse_started = threading.Event()+ allow_parse_to_finish = threading.Event()++ class _BlockingService:+ def parse(self, _source_path, **_kwargs):+ parse_started.set()+ assert allow_parse_to_finish.wait(timeout=2)+ return ParsedDocument(markdown="Parsed without blocking the loop")++ monkeypatch.setattr(parsing, "get_parse_service", lambda: _BlockingService())++ async def _exercise() -> list[object]:+ load_task = asyncio.create_task(LlamaIndexDocumentLoader().load([str(pdf_path)]))+ deadline = asyncio.get_running_loop().time() + 1+ while not parse_started.is_set():+ assert asyncio.get_running_loop().time() < deadline+ await asyncio.sleep(0.001)++ # Reaching this line while parse() is still waiting proves that the+ # parser is not occupying the event-loop thread.+ assert not load_task.done()+ allow_parse_to_finish.set()+ return await asyncio.wait_for(load_task, timeout=1)++ documents = asyncio.run(_exercise())+ assert [document.text for document in documents] == [+ "Parsed without blocking the loop"+ ]++def test_loader_skips_document_when_active_engine_cannot_parse(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None: