Skip to content

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

test(file-parse): cover explicit cancellation boundaries

myhloli091244b55c79
diff --git a/docs/next/api/file-parse.md b/docs/next/api/file-parse.md
index 2983e4fa..a2786d9b 100644
--- a/docs/next/api/file-parse.md
+++ b/docs/next/api/file-parse.md
@@ -301,6 +301,10 @@ curl --fail-with-body -o report.zip \
完整接收后的客户端断连不取消已托管的上传或解析。上传失败先查询状态,确认已经就绪便不重传;若保留的源文件仍可用,使用相同请求 ID 和相同输入可继续原任务。
+包装服务当前不提供独立的取消接口。等待超时、客户端断连或包装服务关闭都不会自动向上游发送取消请求。
+已取得非空 `task_id` 且有权使用对应上游身份时,可向配置的上游 API 调用 `POST /v1/tasks/{task_id}/cancel`;不要使用包装服务的基址,也不要把 `request_id` 当作上游任务 ID。
+上游取消成功后,再查询 `/v1/file_parse/{request_id}` 可观察 `canceled` 状态。取消能力沿用上游 Tasks 契约;file_parse 自身的 `status_url`、响应字段和 null 规则保持不变。
+
## 去重与持久化边界
完整接收并持久化源文件后,包装服务才接受请求并托管上游上传。此后连接中断不取消上传或解析;文件尚未完整接收时中断不会创建任务。
diff --git a/tests/unittest/test_file_parse.py b/tests/unittest/test_file_parse.py
index e1f0bbff..887a143f 100644
--- a/tests/unittest/test_file_parse.py
+++ b/tests/unittest/test_file_parse.py
@@ -16,6 +16,7 @@ import zipfile
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Any
+from urllib.parse import urlsplit
import httpx
import pytest
@@ -174,6 +175,16 @@ class Upstream:
return httpx.Response(200, content=artifact_bytes(task["options"]["output_formats"]))
+def assert_no_upstream_cancellation(upstream: Upstream) -> None:
+ """超时、断连和关闭包装服务均不得发送新取消请求或旧 DELETE 请求。"""
+ cancellations = [
+ (method, url)
+ for method, url, _authorization in upstream.calls
+ if method == "DELETE" or (method == "POST" and urlsplit(url).path.endswith("/cancel"))
+ ]
+ assert not cancellations
+
+
class InterruptedStream(httpx.AsyncByteStream):
"""模拟结果传输到一半时上游断开连接。"""
@@ -267,9 +278,10 @@ def test_expired_wait_does_not_cancel_upstream(tmp_path: Path) -> None:
response = submit(client, wait_timeout=1)
assert response.status_code == 202
assert 0.9 <= time.monotonic() - started < 3
- assert not any(method == "DELETE" for method, _, _ in upstream.calls)
+ assert_no_upstream_cancellation(upstream)
upstream.complete = True
assert client.get(response.json()["status_url"]).json()["status"] == "completed"
+ assert_no_upstream_cancellation(upstream)
def test_wait_budget_excludes_storage_notification_delay(tmp_path: Path) -> None:
@@ -551,6 +563,7 @@ def test_disconnect_does_not_cancel_upload(tmp_path: Path) -> None:
assert not list(app.state.service.store.sources.iterdir())
asyncio.run(exercise())
+ assert_no_upstream_cancellation(upstream)
def test_attachment_cleanup_after_send_disconnect(tmp_path: Path) -> None:
diff --git a/tests/unittest/test_file_parse_http.py b/tests/unittest/test_file_parse_http.py
index 045ed8ff..c18ff70f 100644
--- a/tests/unittest/test_file_parse_http.py
+++ b/tests/unittest/test_file_parse_http.py
@@ -17,7 +17,7 @@ from fastapi import FastAPI, Request, Response
from PIL import Image, ImageDraw
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen.canvas import Canvas
-from test_file_parse import FORMATS, Upstream, artifact_bytes
+from test_file_parse import FORMATS, Upstream, artifact_bytes, assert_no_upstream_cancellation
from test_http_api_example_script import _Server
from test_task_http import HTML, _LiveService, live_service as tasks_live_service
@@ -145,7 +145,7 @@ def test_saas_storage_all_formats_and_recovery(tmp_path: Path) -> None:
assert waiting.returncode == 124, waiting.stdout + waiting.stderr
assert "status_url" in waiting.stdout and not (tmp_path / "pending.zip").exists()
assert len(state.tasks) == 17
- assert all(method != "DELETE" for method, _, _ in state.calls)
+ assert_no_upstream_cancellation(state)
finally:
storage.stop()
api.stop()
@@ -225,6 +225,7 @@ def test_tcp_disconnect_keeps_accepted_request(tmp_path: Path) -> None:
assert response.json()["status"] == "completed"
assert client.get(response.json()["result_url"]).content == artifact_bytes(["default"])
assert state.uploads == 1 and len(state.tasks) == 1
+ assert_no_upstream_cancellation(state)
finally:
storage.stop()
api.stop()