Skip to content

armgit / LEANN

Public Imported from GitHub
0

[MLsys2026 Best Paper]: https://arxiv.org/abs/2506.08276. RAG on Everything with LEANN. Enjoy 97% storage savings while running a fast, accurate, and 100% private RAG application on your personal device.

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

Wu-Yumindc85934f318c
diff --git a/packages/leann-core/src/leann/cli.py b/packages/leann-core/src/leann/cli.py
index 7b6adf3..4cacdee 100644
--- a/packages/leann-core/src/leann/cli.py
+++ b/packages/leann-core/src/leann/cli.py
@@ -175,6 +175,11 @@ def suppress_cpp_output(suppress: bool = True):
def extract_pdf_text_with_pymupdf(file_path: str) -> str | None:
"""Extract text from PDF using PyMuPDF for better quality."""
try:
+ if os.path.getsize(file_path) == 0:
+ # Empty file: nothing to extract, skip instead of letting
+ # fitz.EmptyFileError abort the whole build.
+ return ""
+
import fitz # PyMuPDF
doc = fitz.open(file_path)
@@ -186,11 +191,18 @@ def extract_pdf_text_with_pymupdf(file_path: str) -> str | None:
except ImportError:
# Fallback to default reader
return None
+ except Exception:
+ # Skip corrupted PDFs instead of aborting the whole build
+ return ""
def extract_pdf_text_with_pdfplumber(file_path: str) -> str | None:
"""Extract text from PDF using pdfplumber for better quality."""
try:
+ if os.path.getsize(file_path) == 0:
+ # Empty file: nothing to extract, skip instead of aborting the build.
+ return ""
+
import pdfplumber
text = ""
@@ -201,6 +213,9 @@ def extract_pdf_text_with_pdfplumber(file_path: str) -> str | None:
except ImportError:
# Fallback to default reader
return None
+ except Exception:
+ # Skip corrupted PDFs instead of aborting the whole build
+ return ""
class LeannCLI:
diff --git a/packages/leann-core/src/leann/mcp.py b/packages/leann-core/src/leann/mcp.py
index 458cf71..7713ec7 100755
--- a/packages/leann-core/src/leann/mcp.py
+++ b/packages/leann-core/src/leann/mcp.py
@@ -21,9 +21,14 @@ def _leann_cmd() -> list[str]:
def _run_leann(*args, timeout=120):
"""Run a leann CLI command and return (returncode, stdout, stderr)."""
result = subprocess.run(
- ["leann", *args],
+ [*_leann_cmd(), *args],
capture_output=True,
text=True,
+ # The leann CLI prints UTF-8 (emoji, CJK, ...). Decode explicitly:
+ # text=True alone falls back to the locale encoding (e.g. GBK on
+ # Chinese Windows) and crashes the subprocess reader thread.
+ encoding="utf-8",
+ errors="replace",
timeout=timeout,
)
return result.returncode, result.stdout, result.stderr