Skip to content

fix: Windows MCP encoding crash and build abort on empty/corrupted PDFs - #391

Merged
ASuresh0524 merged 2 commits into
StarTrail-org:mainfrom
Wu-Yumin:fix/windows-mcp-encoding-and-empty-pdf
Jul 31, 2026
Merged

fix: Windows MCP encoding crash and build abort on empty/corrupted PDFs#391
ASuresh0524 merged 2 commits into
StarTrail-org:mainfrom
Wu-Yumin:fix/windows-mcp-encoding-and-empty-pdf

Conversation

@Wu-Yumin

Copy link
Copy Markdown
Contributor

What does this PR do?

Two Windows fixes, both reproduced on Windows 11 + Python 3.11 (leann-core 0.3.7):

  • fix(mcp): decode leann CLI output explicitly as UTF-8 (errors="replace").
    Plain text=True decodes subprocess output with the locale encoding (GBK on
    Chinese Windows). Since the leann CLI prints emoji/CJK (UTF-8), the subprocess
    reader thread crashed with UnicodeDecodeError and every MCP tool call returned
    {"text": null}. Fixed by passing encoding="utf-8" explicitly.
  • fix(mcp): actually use the existing _leann_cmd() helper.
    _leann_cmd() (sys.executable -m leann) was defined but never called;
    _run_leann still did a bare ["leann", ...] lookup, which fails with
    WinError 2 when the leann console-script is not on PATH — exactly the
    situation the helper's docstring describes (MCP client wrappers on Windows).
  • fix(cli): skip empty or corrupted PDFs during leann build.
    A 0-byte or damaged PDF raised pymupdf.EmptyFileError (or similar) from
    fitz.open() / pdfplumber.open() and aborted the entire build. Both
    extract_pdf_text_with_pymupdf and extract_pdf_text_with_pdfplumber now
    return "" for empty/unopenable files so the rest of the document set still
    gets indexed.

Note: the related Windows crash in search_documents JSON mode (ctypes.CDLL(None))
was already fixed by #312.

Related Issues

Relates to #14

Checklist

  • Tests pass (uv run pytest) — ran related suites: test_mcp_standalone.py,
    test_mcp_integration.py, test_cli_verbosity.py, test_rebuild_cli.py
    (26 passed); empty-PDF extraction verified manually
  • Code formatted (ruff format and ruff check)
  • Pre-commit hooks pass (pre-commit run --all-files) — not run (pre-commit not installed locally)

Micah added 2 commits July 31, 2026 17:05
Two Windows fixes in the MCP stdio server:

- _run_leann now decodes subprocess output with encoding='utf-8'
  (errors='replace'). text=True alone falls back to the locale
  encoding (e.g. GBK on Chinese Windows), which crashed the
  subprocess reader thread on any emoji/CJK output and made every
  tool call return {"text": null}.
- _run_leann now actually uses the existing _leann_cmd() helper
  (sys.executable -m leann) instead of a bare 'leann' lookup, so the
  CLI is found even when the leann console-script is not on PATH
  (common when leann_mcp is launched by MCP client wrappers).
A 0-byte or corrupted PDF made fitz.open()/pdfplumber.open() raise
(pymupdf.EmptyFileError etc.) and aborted the entire 'leann build'.
Return an empty string for unopenable/empty PDFs so the rest of the
document set still gets indexed.
@ASuresh0524
ASuresh0524 merged commit dc85934 into StarTrail-org:main Jul 31, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants