Skip to content

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

fix(api): drain duplicate upload bodies before responding

myhloli83be2bb75773
diff --git a/mineru/tasks/http.py b/mineru/tasks/http.py
index 5cefd4e1..9437642a 100644
--- a/mineru/tasks/http.py
+++ b/mineru/tasks/http.py
@@ -247,6 +247,11 @@ def build_task_router(factory: Callable[[Request], TaskBackend]) -> APIRouter:
content_length=request.headers.get("content-length"),
mime_type=request.headers.get("content-type", "application/octet-stream"),
)
+ unread_body = skipped and request.headers.get("expect", "").lower() == "100-continue"
+ if skipped and not unread_body:
+ # 普通重传已发送正文;先流式丢弃,再返回响应,避免带未读数据关闭连接触发 TCP reset。
+ async for _ in request.stream():
+ pass
except ClientDisconnect:
invalid_task_request("Upload interrupted; retry this file on the same task", code="upload_interrupted")
# 大文件转传可超过租约长度;受理完成时续租,保护紧接着发布的结果。
@@ -257,7 +262,7 @@ def build_task_router(factory: Callable[[Request], TaskBackend]) -> APIRouter:
file_id=file_id,
status=next(state.status for item, state in zip(record.inputs, record.file_states) if item.file_id == file_id),
).model_dump(),
- headers={"Connection": "close"} if skipped and request.scope.get("http_version") == "1.1" else None,
+ headers={"Connection": "close"} if unread_body and request.scope.get("http_version") == "1.1" else None,
)
@router.post("/tasks/{task_id}/cancel", response_model=CancelTaskResponse)
diff --git a/tests/unittest/test_task_http.py b/tests/unittest/test_task_http.py
index 6abdce1e..2f8efe30 100644
--- a/tests/unittest/test_task_http.py
+++ b/tests/unittest/test_task_http.py
@@ -194,9 +194,10 @@ def test_pdf_without_file_id_keeps_metadata_and_cached_results(live_service: _Li
assert task["files"][0]["file_id"] == pending_files(task)[0]["file_id"]
assert task["files"][0]["file_id"].startswith("file-")
link = pending_files(task)[0]
- for _ in range(2):
+ for attempt in range(2):
upload = client.put(link.get("upload").get("url"), content=data, headers=link.get("upload")["headers"])
assert upload.status_code == 200, upload.text
+ assert live_service.counts[urlsplit(link["upload"]["url"]).path] == (attempt + 1) * len(data)
completed = _wait(client, task)
assert completed["status"] == "completed", completed
item = completed["files"][0]