Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix: keep live server process during startup timeout

jinzhenja5a24a53771f
diff --git a/mineru/cli/commands/server.py b/mineru/cli/commands/server.py
index 98b444dc..e8b9fa12 100644
--- a/mineru/cli/commands/server.py
+++ b/mineru/cli/commands/server.py
@@ -8,6 +8,7 @@ import sys
import time
import uuid
from collections.abc import Iterator
+from dataclasses import dataclass
from typing import Any
import typer
@@ -16,6 +17,7 @@ from rich.table import Table
from rich.text import Text
from ...config import config
+from ...doclib.endpoint import read_endpoint_file
from ...doclib.instance_lock import DoclibLockUnavailable, build_doclib_home_owned_message, doclib_home_lock
from ...doclib.types import ServerStatusResponse, TCPServerStatus
from ...errors import MineruError
@@ -29,6 +31,12 @@ app = typer.Typer(help="Server lifecycle management", no_args_is_help=True)
SERVER_START_TIMEOUT_SEC = 30.0
+@dataclass(frozen=True)
+class _ServerStartingStatus:
+ status: str = "starting"
+ pid: int | None = None
+
+
def _socket_path() -> str:
return config.doclib.uds.path
@@ -240,7 +248,7 @@ def _start() -> str:
if not _wait_for_started_server(proc):
if proc.poll() is None:
- proc.kill()
+ return f"Server is still starting (PID {proc.pid}).\nCheck status: mineru server status"
raise MineruError(
"service_unavailable",
f"Server failed to start within {int(SERVER_START_TIMEOUT_SEC)} seconds. "
@@ -307,10 +315,11 @@ def status(json_mode: bool = typer.Option(False, "--json", help="JSON output"))
run_cli(ctx, _server_status, render=_render_server_status)
-def _server_status() -> ServerStatusResponse:
+def _server_status() -> ServerStatusResponse | _ServerStartingStatus:
if not _server_running():
if not _doclib_lock_available():
- raise _home_owner_unavailable_error()
+ endpoint = read_endpoint_file(_endpoint_path())
+ return _ServerStartingStatus(pid=endpoint.pid if endpoint is not None else None)
return _not_running_status()
from ...doclib.client import DoclibClient
@@ -318,7 +327,14 @@ def _server_status() -> ServerStatusResponse:
return c.get_server_status()
-def _render_server_status(data: ServerStatusResponse) -> Iterator[RenderableObject]:
+def _render_server_status(data: ServerStatusResponse | _ServerStartingStatus) -> Iterator[RenderableObject]:
+ if isinstance(data, _ServerStartingStatus):
+ if data.pid is None:
+ yield "Server is still starting."
+ else:
+ yield f"Server is still starting (PID {data.pid})."
+ yield "Check again: mineru server status"
+ return
if not _get(data, "running"):
yield "Server is not running."
return
diff --git a/tests/unittest/test_cli_next_command_design.py b/tests/unittest/test_cli_next_command_design.py
index 23e4ee58..daa5d8bb 100644
--- a/tests/unittest/test_cli_next_command_design.py
+++ b/tests/unittest/test_cli_next_command_design.py
@@ -2085,8 +2085,8 @@ def test_server_start_failure_points_to_log_and_does_not_discard_child_stderr(mo
class _Proc:
pid = 12345
- def poll(self) -> None:
- return None
+ def poll(self) -> int:
+ return 1
def kill(self) -> None:
popen_calls.append({"kill": True})
@@ -2124,7 +2124,32 @@ def test_server_start_failure_points_to_log_and_does_not_discard_child_stderr(mo
assert "child failed\n" not in log_path.read_text(encoding="utf-8")
assert "child stdout\n" in stdout_log_path.read_text(encoding="utf-8")
assert "child failed\n" in stderr_log_path.read_text(encoding="utf-8")
- assert popen_calls[-1] == {"kill": True}
+ assert {"kill": True} not in popen_calls
+
+
+def test_server_start_reports_starting_process_without_killing_it(monkeypatch: Any, tmp_path: Path) -> None:
+ class _Proc:
+ pid = 12345
+
+ def poll(self) -> None:
+ return None
+
+ def _popen(*args: Any, **kwargs: Any) -> _Proc:
+ return _Proc()
+
+ monkeypatch.setattr(server, "_server_running", lambda: False)
+ monkeypatch.setattr(server, "_doclib_lock_available", lambda: True)
+ monkeypatch.setattr(server, "_wait_for_started_server", lambda proc: False)
+ monkeypatch.setattr(server, "_server_log_path", lambda: str(tmp_path / "doclib.log"))
+ monkeypatch.setattr(server, "_server_stdout_log_path", lambda: str(tmp_path / "doclib.stdout.log"))
+ monkeypatch.setattr(server, "_server_stderr_log_path", lambda: str(tmp_path / "doclib.stderr.log"))
+ monkeypatch.setattr(server.subprocess, "Popen", _popen)
+
+ result = runner.invoke(app, ["server", "start"])
+
+ assert result.exit_code == 0
+ assert "Server is still starting (PID 12345)." in result.output
+ assert "Check status: mineru server status" in result.output
def test_server_start_lock_blocks_concurrent_start(monkeypatch: Any, tmp_path: Path) -> None:
@@ -2807,18 +2832,26 @@ def test_server_status_json_not_running_returns_state_json(monkeypatch: Any) ->
assert "Server is not running." not in result.output
-def test_server_status_json_reports_unresponsive_home_owner(monkeypatch: Any, tmp_path: Path) -> None:
+def test_server_status_json_reports_starting_without_endpoint_pid(monkeypatch: Any) -> None:
monkeypatch.setattr(server, "_server_running", lambda: False)
monkeypatch.setattr(server, "_doclib_lock_available", lambda: False)
- monkeypatch.setattr("mineru.doclib.instance_lock._mineru_home", lambda: str(tmp_path))
- monkeypatch.setattr("mineru.doclib.instance_lock.read_endpoint_file", lambda path: None)
+ monkeypatch.setattr(server, "read_endpoint_file", lambda path: None)
result = runner.invoke(app, ["server", "status", "--json"])
- assert result.exit_code == 1
+ assert result.exit_code == 0
payload = json.loads(result.output)
- assert payload["error"]["code"] == "service_unavailable"
- assert payload["error"]["message"] == (
- f"MinerU home [{tmp_path}] is currently owned by another doclib server process."
- )
- assert "doclib.lock" not in payload["error"]["message"]
+ assert payload == {"status": "starting", "pid": None}
+
+
+def test_server_status_reports_starting_with_endpoint_pid(monkeypatch: Any) -> None:
+ endpoint = type("Endpoint", (), {"pid": 12345})()
+ monkeypatch.setattr(server, "_server_running", lambda: False)
+ monkeypatch.setattr(server, "_doclib_lock_available", lambda: False)
+ monkeypatch.setattr(server, "read_endpoint_file", lambda path: endpoint)
+
+ result = runner.invoke(app, ["server", "status"])
+
+ assert result.exit_code == 0
+ assert "Server is still starting (PID 12345)." in result.output
+ assert "Check again: mineru server status" in result.output