Skip to content

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

feat: implement timeout handling for result downloads and refactor response processing

myhloli3cf57bea8fd1
diff --git a/mineru/cli/api_client.py b/mineru/cli/api_client.py
index 3f1576ff..24806a17 100644
--- a/mineru/cli/api_client.py
+++ b/mineru/cli/api_client.py
@@ -616,6 +616,15 @@ def build_http_timeout() -> httpx.Timeout:
return httpx.Timeout(connect=10, read=60, write=300, pool=30)
+def build_result_download_timeout() -> httpx.Timeout:
+ return httpx.Timeout(
+ connect=10,
+ read=TASK_RESULT_TIMEOUT_SECONDS,
+ write=300,
+ pool=30,
+ )
+
+
def find_free_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(("127.0.0.1", 0))
@@ -951,23 +960,44 @@ async def download_result_zip(
submit_response: SubmitResponse,
task_label: str,
) -> Path:
- response = await client.get(submit_response.result_url)
- if response.status_code != 200:
- raise click.ClickException(
- f"Failed to download result ZIP for task {submit_response.task_id}: "
- f"{response.status_code} {response_detail(response)}"
- )
- content_type = response.headers.get("content-type", "")
- if "application/zip" not in content_type:
- raise click.ClickException(
- f"Expected a ZIP result for {task_label}, "
- f"got content-type={content_type or 'unknown'}"
- )
-
zip_fd, zip_path = tempfile.mkstemp(suffix=".zip", prefix="mineru_cli_result_")
os.close(zip_fd)
- Path(zip_path).write_bytes(response.content)
- return Path(zip_path)
+ zip_file_path = Path(zip_path)
+ try:
+ async with client.stream(
+ "GET",
+ submit_response.result_url,
+ timeout=build_result_download_timeout(),
+ ) as response:
+ if response.status_code != 200:
+ await response.aread()
+ raise click.ClickException(
+ f"Failed to download result ZIP for task {submit_response.task_id}: "
+ f"{response.status_code} {response_detail(response)}"
+ )
+ content_type = response.headers.get("content-type", "")
+ if "application/zip" not in content_type:
+ raise click.ClickException(
+ f"Expected a ZIP result for {task_label}, "
+ f"got content-type={content_type or 'unknown'}"
+ )
+
+ with open(zip_file_path, "wb") as handle:
+ async for chunk in response.aiter_bytes():
+ handle.write(chunk)
+ except click.ClickException:
+ zip_file_path.unlink(missing_ok=True)
+ raise
+ except httpx.TimeoutException as exc:
+ zip_file_path.unlink(missing_ok=True)
+ raise click.ClickException(
+ f"Timed out downloading result ZIP for task {submit_response.task_id} "
+ f"for {task_label}"
+ ) from exc
+ except Exception:
+ zip_file_path.unlink(missing_ok=True)
+ raise
+ return zip_file_path
def safe_extract_zip(zip_path: Path, output_dir: Path) -> None:
diff --git a/mineru/cli/fast_api.py b/mineru/cli/fast_api.py
index 5d9de889..87b8edf4 100644
--- a/mineru/cli/fast_api.py
+++ b/mineru/cli/fast_api.py
@@ -641,7 +641,7 @@ def create_result_zip(
return zip_path
-def build_result_response(
+async def build_result_response(
background_tasks: BackgroundTasks,
status_code: int,
output_dir: str,
@@ -658,7 +658,8 @@ def build_result_response(
zip_filename: str = "results.zip",
) -> Response:
if response_format_zip:
- zip_path = create_result_zip(
+ zip_path = await asyncio.to_thread(
+ create_result_zip,
output_dir=output_dir,
pdf_file_names=pdf_file_names,
backend=backend,
@@ -678,7 +679,8 @@ def build_result_response(
status_code=status_code,
)
- result_dict = build_result_dict(
+ result_dict = await asyncio.to_thread(
+ build_result_dict,
output_dir=output_dir,
pdf_file_names=pdf_file_names,
backend=backend,
@@ -709,14 +711,14 @@ def build_task_submission_response(
return JSONResponse(status_code=202, content=payload)
-def build_sync_file_parse_response(
+async def build_sync_file_parse_response(
background_tasks: BackgroundTasks,
task: AsyncParseTask,
request: Request,
) -> Response:
task_payload = task.to_status_payload(request)
if task.response_format_zip:
- response = build_result_response(
+ response = await build_result_response(
background_tasks=background_tasks,
status_code=200,
output_dir=task.output_dir,
@@ -738,7 +740,8 @@ def build_sync_file_parse_response(
response.headers[FILE_PARSE_TASK_RESULT_URL_HEADER] = task_payload["result_url"]
return response
- result_dict = build_result_dict(
+ result_dict = await asyncio.to_thread(
+ build_result_dict,
output_dir=task.output_dir,
pdf_file_names=task.file_names,
backend=task.backend,
@@ -1413,7 +1416,7 @@ async def parse_pdf(
},
)
- return build_sync_file_parse_response(
+ return await build_sync_file_parse_response(
background_tasks=background_tasks,
task=task,
request=http_request,
@@ -1478,7 +1481,7 @@ async def get_async_task_result(
},
)
- return build_result_response(
+ return await build_result_response(
background_tasks=background_tasks,
status_code=200,
output_dir=task.output_dir,
diff --git a/mineru/cli/router.py b/mineru/cli/router.py
index 8d924f3d..b36c21e5 100644
--- a/mineru/cli/router.py
+++ b/mineru/cli/router.py
@@ -32,6 +32,7 @@ from mineru.cli.api_client import (
TASK_STATUS_POLL_INTERVAL_SECONDS,
build_managed_process_popen_kwargs,
build_http_timeout,
+ build_result_download_timeout,
find_free_port,
normalize_base_url,
stop_managed_process,
@@ -1212,7 +1213,11 @@ async def proxy_router_task_result(
result_url = f"{task.upstream_base_url}{TASKS_ENDPOINT}/{task.upstream_task_id}/result"
try:
upstream_response = await client.send(
- client.build_request("GET", result_url),
+ client.build_request(
+ "GET",
+ result_url,
+ timeout=build_result_download_timeout(),
+ ),
stream=True,
)
except httpx.HTTPError as exc:
@@ -1263,7 +1268,11 @@ async def build_sync_router_task_result_response(
result_url = f"{task.upstream_base_url}{TASKS_ENDPOINT}/{task.upstream_task_id}/result"
try:
upstream_response = await client.send(
- client.build_request("GET", result_url),
+ client.build_request(
+ "GET",
+ result_url,
+ timeout=build_result_download_timeout(),
+ ),
stream=True,
)
except httpx.HTTPError as exc: