Skip to content

PDF indexing fails on Windows: NamedTemporaryFile(delete=True) blocks PyMuPDFLoader from opening the temp file #1368

Description

@chirag127

Problem

On Windows, PdfToEntries.extract_text cannot index any PDF because PyMuPDFLoader is passed the path of a still-open NamedTemporaryFile(delete=True), and Windows disallows a second open on that path.

Cite

src/khoj/processor/content/pdf/pdf_to_entries.py:100-105

with tempfile.NamedTemporaryFile(suffix=".pdf", delete=True) as tmpf:
    tmpf.write(pdf_file)
    tmpf.flush()
    loader = PyMuPDFLoader(tmpf.name)
    pdf_entries_per_file = loader.load()

Per Python docs: on Windows the name cannot be used to open the file a second time while the handle is still open.

Steps

  1. Windows 11, pip install khoj (Python 3.10-3.12 per pyproject.toml).
  2. Upload any PDF via /api/content?t=pdf.

Expected

PDF is indexed.

Actual

extract_text catches PermissionError: [WinError 32] The process cannot access the file because it is being used by another process and logs Unable to process file: <bytes>. This file will not be indexed. No entries created.

Environment

OS: Windows 11; Python 3.10-3.12; khoj 2.0.0-beta.28; pymupdf == 1.24.11, langchain-community == 0.3.31 (from pyproject.toml).

Suggested fix

Use delete=False + explicit os.unlink(tmpf.name) in a finally block, or (Python 3.12+) delete_on_close=False.

Thanks for maintaining khoj-ai/khoj!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions