diff --git a/nextcloud_mcp_server/document_processors/pymupdf.py b/nextcloud_mcp_server/document_processors/pymupdf.py index 722fad40..d1262b02 100644 --- a/nextcloud_mcp_server/document_processors/pymupdf.py +++ b/nextcloud_mcp_server/document_processors/pymupdf.py @@ -107,14 +107,19 @@ class PyMuPDFProcessor(DocumentProcessor): if progress_callback: await progress_callback(0, 100, "Opening PDF document") - # Open document and extract metadata in thread + # Open document only to read metadata + page count, then close it + # immediately (try/finally so a failure in _extract_metadata can't + # leak it). The heavy extraction below works from ``content`` bytes + # in the isolated worker, so ``doc`` is not needed past this point. doc = await anyio.to_thread.run_sync( # type: ignore[attr-defined] lambda: pymupdf.open("pdf", content) ) - - metadata = self._extract_metadata(doc, filename) - metadata["file_size"] = len(content) - page_count = doc.page_count + try: + metadata = self._extract_metadata(doc, filename) + metadata["file_size"] = len(content) + page_count = doc.page_count + finally: + doc.close() if progress_callback: await progress_callback(10, 100, f"Extracting {page_count} pages") @@ -143,7 +148,6 @@ class PyMuPDFProcessor(DocumentProcessor): mem_limit_mb=settings.document_parse_mem_limit_mb, ) except PdfParseFailed as exc: - doc.close() logger.warning( "Isolated PDF parse failed for %s (reason=%s): %s", filename or "", @@ -198,9 +202,6 @@ class PyMuPDFProcessor(DocumentProcessor): metadata["image_paths"] = image_paths metadata["page_boundaries"] = page_boundaries - # Close document - doc.close() - if progress_callback: await progress_callback(100, 100, "Processing complete") diff --git a/nextcloud_mcp_server/vector/processor.py b/nextcloud_mcp_server/vector/processor.py index cd2b329b..47138344 100644 --- a/nextcloud_mcp_server/vector/processor.py +++ b/nextcloud_mcp_server/vector/processor.py @@ -237,7 +237,15 @@ async def process_document( for attempt in range(max_retries): try: - await _index_document(doc_task, nc_client, qdrant_client) + indexed = await _index_document(doc_task, nc_client, qdrant_client) + + # A permanent parse failure returns False: it was already + # recorded (document_parse_failed_total + the registry's + # document_parse_total{error}) and the placeholder marked + # "failed". It is not an indexing event and not retryable, so + # don't count it as a successful upsert/indexed document. + if indexed is False: + return # Record successful processing metrics duration = time.time() - start_time @@ -534,8 +542,8 @@ async def _index_document( # on a pathological PDF) returns success=False rather than # raising -- there is nothing to index and retrying would just # fail again. Mark the placeholder "failed" so the scanner stops - # re-queuing it (until the file changes) and return without - # indexing empty content. + # re-queuing it (until the file changes) and return False so the + # caller skips the success metrics (it was not indexed). if not result.success: reason = result.metadata.get("parse_failed_reason", "error") record_document_parse_failed(reason) @@ -561,7 +569,7 @@ async def _index_document( doc_task.doc_id, exc_info=True, ) - return + return False content = result.text file_metadata = result.metadata