Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
fix: stop PDF render workers when parent exits
jinzhenj66025719539a
diff --git a/docs/plans/2026-07-22-pdf-render-parent-exit-design.md b/docs/plans/2026-07-22-pdf-render-parent-exit-design.mdnew file mode 100644index 00000000..5acd5088--- /dev/null+++ b/docs/plans/2026-07-22-pdf-render-parent-exit-design.md@@ -0,0 +1,13 @@+# PDF render worker parent-exit cleanup++PDF rendering uses a persistent spawn-based `ProcessPoolExecutor`. If its owning parse-server exits without running executor shutdown,+the worker can remain alive with `PPID=1`; the multiprocessing resource tracker then also remains alive because the worker still holds+the tracker pipe.++Each PDF render worker installs a daemon watcher through the executor initializer. The watcher waits on+`multiprocessing.parent_process()` and calls `os._exit(1)` when the parent process sentinel becomes ready. This uses Python's+cross-platform multiprocessing process sentinel and does not require PID polling or a separate MinerU lifecycle pipe.++Normal executor shutdown is unchanged. On abnormal parent exit, terminating the worker closes its resource-tracker descriptor, allowing+the standard-library resource tracker to observe EOF, clean registered resources, and exit. A worker permanently holding the GIL may+still prevent its watcher thread from running; that extreme case is outside this change.diff --git a/mineru/utils/pdf_image_tools.py b/mineru/utils/pdf_image_tools.pyindex f285ca64..372dc98d 100644--- a/mineru/utils/pdf_image_tools.py+++ b/mineru/utils/pdf_image_tools.py@@ -161,9 +161,26 @@ def _get_pdf_render_pool_capacity(cpu_count: int | None = None) -> int:)+def _exit_pdf_render_worker_when_parent_exits() -> None:+ parent = multiprocessing.parent_process()+ if parent is None:+ return+ parent.join()+ os._exit(1)+++def _install_pdf_render_parent_exit_watcher() -> None:+ watcher = threading.Thread(+ target=_exit_pdf_render_worker_when_parent_exits,+ name="mineru-pdf-render-parent-exit-watcher",+ daemon=True,+ )+ watcher.start()++def _create_pdf_render_executor(max_workers: int) -> ProcessPoolExecutor:if is_windows_environment():- return ProcessPoolExecutor(max_workers=max_workers)+ return ProcessPoolExecutor(max_workers=max_workers, initializer=_install_pdf_render_parent_exit_watcher)start_method = multiprocessing.get_start_method()if start_method != "spawn":@@ -171,9 +188,10 @@ def _create_pdf_render_executor(max_workers: int) -> ProcessPoolExecutor:return ProcessPoolExecutor(max_workers=max_workers,mp_context=multiprocessing.get_context("spawn"),+ initializer=_install_pdf_render_parent_exit_watcher,)- return ProcessPoolExecutor(max_workers=max_workers)+ return ProcessPoolExecutor(max_workers=max_workers, initializer=_install_pdf_render_parent_exit_watcher)def _is_pdf_render_pool_still_spawning_workers(executor: ProcessPoolExecutor) -> bool:diff --git a/tests/unittest/test_pdf_render_diagnostics.py b/tests/unittest/test_pdf_render_diagnostics.pyindex 294cbd58..11f0107f 100644--- a/tests/unittest/test_pdf_render_diagnostics.py+++ b/tests/unittest/test_pdf_render_diagnostics.py@@ -30,6 +30,59 @@ def _capture_log_messages() -> tuple[list[str], int]:return messages, handler_id+def test_pdf_render_worker_exits_after_parent_process(monkeypatch: pytest.MonkeyPatch) -> None:+ events: list[object] = []++ class _ParentProcess:+ def join(self) -> None:+ events.append("parent.join")++ monkeypatch.setattr(pdf_image_tools.multiprocessing, "parent_process", _ParentProcess)+ monkeypatch.setattr(pdf_image_tools.os, "_exit", lambda code: events.append(("exit", code)))++ pdf_image_tools._exit_pdf_render_worker_when_parent_exits()++ assert events == ["parent.join", ("exit", 1)]+++def test_pdf_render_worker_without_multiprocessing_parent_does_not_exit(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(pdf_image_tools.multiprocessing, "parent_process", lambda: None)+ monkeypatch.setattr(pdf_image_tools.os, "_exit", lambda code: pytest.fail(f"unexpected exit {code}"))++ pdf_image_tools._exit_pdf_render_worker_when_parent_exits()+++def test_pdf_render_executor_installs_parent_exit_watcher() -> None:+ executor = pdf_image_tools._create_pdf_render_executor(max_workers=1)+ try:+ assert executor._initializer is pdf_image_tools._install_pdf_render_parent_exit_watcher+ finally:+ executor.shutdown(wait=True, cancel_futures=True)+++def test_pdf_render_parent_exit_watcher_is_daemon(monkeypatch: pytest.MonkeyPatch) -> None:+ events: list[object] = []++ class _Thread:+ def __init__(self, *, target: Any, name: str, daemon: bool) -> None:+ events.append((target, name, daemon))++ def start(self) -> None:+ events.append("start")++ monkeypatch.setattr(pdf_image_tools.threading, "Thread", _Thread)+ pdf_image_tools._install_pdf_render_parent_exit_watcher()++ assert events == [+ (+ pdf_image_tools._exit_pdf_render_worker_when_parent_exits,+ "mineru-pdf-render-parent-exit-watcher",+ True,+ ),+ "start",+ ]++def test_pdf_render_future_states_cover_terminal_and_active_states() -> None:pending: Future[Any] = Future()running: Future[Any] = Future()