Files
DeepTutor/tests/tools/test_mineru_models.py
落雨✦ 6f0c7fdb47 fix: Windows MinerU parsing — subprocess UTF-8 decoding & render-thread crash (#844)
* fix: Windows robustness — MinerU UTF-8 decode, render-thread crash & atomic-write PermissionError

- mineru/local.py: pass encoding="utf-8", errors="replace" to subprocess.Popen so MinerU's combined stdout/stderr is decoded as UTF-8 instead of the platform default (GBK on zh-CN Windows), which previously crashed with a UnicodeDecodeError (0x8b).
- mineru/models.py: set MINERU_PDF_RENDER_THREADS=1 in model_env_overrides to avoid MinerU's multi-threaded PDF rendering heap-corruption crash (0xc0000409) on Windows.
- services/file_io.py: retry atomic replace on transient PermissionError (e.g. target locked by antivirus/indexer) with exponential backoff, instead of failing the whole write on Windows.

* fix(mineru): gate the render-thread guard to Windows

`MINERU_PDF_RENDER_THREADS=1` was set unconditionally, but the variable is
honored on Linux and macOS too (upstream default 3-4 render workers), so a
Windows-only heap-corruption workaround was serializing page rendering for
every platform. It also sat in `model_env_overrides`, whose contract is the
model-weight download source — and adding a key there broke that function's
exact-equality test.

Move it to its own `render_env_overrides()` next to the other MinerU env
knowledge, guarded on `sys.platform`, and merge it into the subprocess env at
the local-CLI branch in `backend.py`. Only that branch renders pages in this
process tree; cloud mode never does.

---------

Co-authored-by: Frank <bingxizhao@novix.science>
2026-08-24 16:54:05 +08:00

196 lines
6.6 KiB
Python

from __future__ import annotations
from pathlib import Path
import threading
import time
import pytest
from deeptutor.services.parsing.engines.mineru import models as mineru_models
from deeptutor.services.parsing.engines.mineru.models import (
ModelDownloadManager,
model_env_overrides,
render_env_overrides,
resolve_models_downloader,
)
# ---------------------------------------------------------------------------
# Downloader resolution
# ---------------------------------------------------------------------------
def test_resolver_prefers_configured_cli_sibling(tmp_path: Path) -> None:
bin_dir = tmp_path / "env" / "bin"
bin_dir.mkdir(parents=True)
cli = bin_dir / "mineru"
cli.write_text("#!/bin/sh\n", encoding="utf-8")
cli.chmod(0o755)
downloader = bin_dir / "mineru-models-download"
downloader.write_text("#!/bin/sh\n", encoding="utf-8")
downloader.chmod(0o755)
resolved = resolve_models_downloader(str(cli))
assert resolved == {"found": True, "path": str(downloader)}
def test_resolver_reports_missing_sibling_no_path_fallback(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# Even if PATH has the downloader, a configured CLI without a sibling
# downloader is a miss — the configured env is authoritative.
monkeypatch.setattr(
mineru_models.shutil, "which", lambda cmd: "/usr/bin/mineru-models-download"
)
cli = tmp_path / "mineru"
cli.write_text("#!/bin/sh\n", encoding="utf-8")
cli.chmod(0o755)
resolved = resolve_models_downloader(str(cli))
assert resolved["found"] is False
assert resolved["path"].endswith("mineru-models-download")
def test_resolver_falls_back_to_path(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
mineru_models.shutil, "which", lambda cmd: "/opt/bin/mineru-models-download"
)
assert resolve_models_downloader("") == {
"found": True,
"path": "/opt/bin/mineru-models-download",
}
monkeypatch.setattr(mineru_models.shutil, "which", lambda cmd: None)
assert resolve_models_downloader("")["found"] is False
# ---------------------------------------------------------------------------
# Env overrides
# ---------------------------------------------------------------------------
def test_model_env_overrides_shapes() -> None:
assert model_env_overrides("huggingface") == {"MINERU_MODEL_SOURCE": "huggingface"}
assert model_env_overrides("huggingface", "https://hf-mirror.com/") == {
"MINERU_MODEL_SOURCE": "huggingface",
"HF_ENDPOINT": "https://hf-mirror.com",
}
# Endpoint is an HF-only concept; modelscope ignores it.
assert model_env_overrides("modelscope", "https://hf-mirror.com") == {
"MINERU_MODEL_SOURCE": "modelscope"
}
# Unknown source degrades to the default.
assert model_env_overrides("weird")["MINERU_MODEL_SOURCE"] == "huggingface"
# ---------------------------------------------------------------------------
# Download job manager
# ---------------------------------------------------------------------------
class _FakeProc:
def __init__(self, lines, returncode: int = 0):
self.stdout = iter(lines)
self._returncode = returncode
self.terminated = False
def wait(self) -> int:
return -15 if self.terminated else self._returncode
def poll(self):
return None if not self.terminated else -15
def terminate(self) -> None:
self.terminated = True
def _wait_for_terminal(manager: ModelDownloadManager, timeout: float = 2.0) -> str:
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
state = manager.status()["state"]
if state != "running":
return state
time.sleep(0.01)
return manager.status()["state"]
def test_manager_happy_path_and_cursor(monkeypatch: pytest.MonkeyPatch) -> None:
manager = ModelDownloadManager()
monkeypatch.setattr(
mineru_models.subprocess,
"Popen",
lambda *a, **k: _FakeProc(["downloading layout model...\n"]),
)
result = manager.start(
downloader="/x/mineru-models-download",
model_type="pipeline",
source="huggingface",
)
assert result["ok"] is True
assert _wait_for_terminal(manager) == "done"
status = manager.status(0)
assert status["lines"] == ["downloading layout model..."]
assert status["message"] == "Download finished."
# Cursor protocol: re-polling from next_cursor yields nothing new.
assert manager.status(status["next_cursor"])["lines"] == []
def test_manager_nonzero_exit_is_failed(monkeypatch: pytest.MonkeyPatch) -> None:
manager = ModelDownloadManager()
monkeypatch.setattr(
mineru_models.subprocess,
"Popen",
lambda *a, **k: _FakeProc(["boom\n"], returncode=3),
)
assert manager.start(downloader="/x/dl", model_type="pipeline", source="huggingface")["ok"]
assert _wait_for_terminal(manager) == "failed"
assert "code 3" in manager.status()["message"]
def test_manager_rejects_concurrent_start_and_cancels(
monkeypatch: pytest.MonkeyPatch,
) -> None:
release = threading.Event()
def blocking_lines():
yield "starting\n"
release.wait(timeout=5)
proc = _FakeProc(blocking_lines())
monkeypatch.setattr(mineru_models.subprocess, "Popen", lambda *a, **k: proc)
manager = ModelDownloadManager()
assert manager.start(downloader="/x/dl", model_type="all", source="modelscope")["ok"]
# Busy: a second start is rejected while the first is running.
busy = manager.start(downloader="/x/dl", model_type="pipeline", source="huggingface")
assert busy["ok"] is False
cancelled = manager.cancel()
assert cancelled["ok"] is True
release.set()
assert _wait_for_terminal(manager) == "cancelled"
def test_manager_cancel_without_job() -> None:
assert ModelDownloadManager().cancel()["ok"] is False
def test_render_env_overrides_is_windows_only(monkeypatch) -> None:
"""The render-thread guard must not slow Linux/macOS.
``MINERU_PDF_RENDER_THREADS`` is honored on every platform (upstream
default 3-4 workers), so serializing it unconditionally would cost every
non-Windows user parse throughput for a Windows-only heap-corruption crash.
"""
import deeptutor.services.parsing.engines.mineru.models as models
monkeypatch.setattr(models.sys, "platform", "win32")
assert render_env_overrides() == {"MINERU_PDF_RENDER_THREADS": "1"}
monkeypatch.setattr(models.sys, "platform", "linux")
assert render_env_overrides() == {}
monkeypatch.setattr(models.sys, "platform", "darwin")
assert render_env_overrides() == {}