fix: close pypdfium2 page handle on error + cover classifier/OCR edge cases

Follow-up to the tiered document processor (#858), landing the round-4 review
nits the reviewer approved without:

- pypdfium2_fast: free the page handle in an outer finally so a corrupt page
  that makes get_textpage() raise can't orphan it.
- test: classify_from_text junk-text-layer path (non-zero chars, low quality,
  high ocr_frac) flags bad_text_layer -- the hot-path coverage gap.
- test: build_ocr_backend raises ValueError when the gateway M2M client_id is
  set without its token_url/secret.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-06-05 03:21:10 +02:00
co-authored by Claude Opus 4.8
parent ba3cb575fe
commit 9ffe0645b8
3 changed files with 37 additions and 3 deletions
@@ -40,11 +40,15 @@ def _extract(content: bytes) -> tuple[str, dict[str, Any]]:
page_texts: list[str] = []
for i in range(len(pdf)):
page = pdf[i]
textpage = page.get_textpage()
try:
page_texts.append(textpage.get_text_bounded() or "")
textpage = page.get_textpage()
try:
page_texts.append(textpage.get_text_bounded() or "")
finally:
textpage.close()
finally:
textpage.close()
# Outer finally so the page handle is freed even if
# get_textpage() raises on a corrupt page.
page.close()
doc_meta = pdf.get_metadata_dict() or {}
finally: