fix(附件): 配置转换超时并重试临时文件清理
将项目文档提取超时配置为 30 秒,适应 Windows 下独立 LibreOffice 配置目录的启动耗时,并记录本地安装与 PATH 配置方法。 附件清理改为异步,遇到转换子进程退出后的短暂文件占用时进行最多 2 秒的重试,保持事件循环可响应并确保上传计数仅释放一次。 本机已安装 LibreOffice 26.8.1.1 并加入用户 PATH。真实 DOC 正文和中文表格提取、正常及强制超时后的目录清理验证通过;全量测试 134 项通过,无跳过。Ruff 静态检查、改动代码格式检查、pip check 和差异检查通过。
This commit is contained in:
1 parent
1b48d0146c
commit
bc9afe2d47
6 files changed
+43
-9
No files matched your search
@@ -20,6 +20,8 @@ Linux 安装后使用 `agent-safe-middleware --config config.local.yaml`。默
|
|||||||
|
|
||||||
DOC 提取需要 PATH 中存在 `soffice` 或 `libreoffice`(Docker 镜像已安装 LibreOffice Writer);缺失时附件状态为 `failed`。TXT/DOCX 无此依赖。附件读取接口、提取范围和限制见 [插件开发文档](docs/plugins.md#multipart-附件读取)。上传审核插件由使用方配置,并应匹配独立上传路径。
|
DOC 提取需要 PATH 中存在 `soffice` 或 `libreoffice`(Docker 镜像已安装 LibreOffice Writer);缺失时附件状态为 `failed`。TXT/DOCX 无此依赖。附件读取接口、提取范围和限制见 [插件开发文档](docs/plugins.md#multipart-附件读取)。上传审核插件由使用方配置,并应匹配独立上传路径。
|
||||||
|
|
||||||
|
Windows 可通过 `winget install --id TheDocumentFoundation.LibreOffice --exact` 安装,并将 `C:\Program Files\LibreOffice\program` 加入用户 PATH;已有终端和中间层进程需要重启以读取新环境。每次 DOC 提取使用独立 LibreOffice 配置目录,启动和转换可能超过 10 秒,因此项目 `config.yaml` 将 `attachment_timeout_seconds` 配置为 30 秒;省略配置时宿主默认仍为 10 秒。
|
||||||
|
|
||||||
检查 `GET /healthz` 和 `GET /readyz`。就绪表示配置与插件初始化通过,不表示真实模型、MCP 或上游 Agent 已连通。
|
检查 `GET /healthz` 和 `GET /readyz`。就绪表示配置与插件初始化通过,不表示真实模型、MCP 或上游 Agent 已连通。
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
|
|||||||
+1
-1
@@ -11,7 +11,7 @@ limits:
|
|||||||
max_attachment_text_bytes: 1048576
|
max_attachment_text_bytes: 1048576
|
||||||
max_attachment_unpacked_bytes: 33554432
|
max_attachment_unpacked_bytes: 33554432
|
||||||
max_attachment_zip_entries: 2048
|
max_attachment_zip_entries: 2048
|
||||||
attachment_timeout_seconds: 10
|
attachment_timeout_seconds: 30
|
||||||
max_attachment_reads: 64
|
max_attachment_reads: 64
|
||||||
|
|
||||||
llm:
|
llm:
|
||||||
|
|||||||
@@ -194,14 +194,22 @@ class AttachmentStore:
|
|||||||
raise LimitExceeded()
|
raise LimitExceeded()
|
||||||
return self.texts[file_id]
|
return self.texts[file_id]
|
||||||
|
|
||||||
def close(self):
|
async def aclose(self):
|
||||||
if not self.closed:
|
if not self.closed:
|
||||||
self.closed = True
|
self.closed = True
|
||||||
self.texts.clear()
|
self.texts.clear()
|
||||||
self.files.clear()
|
self.files.clear()
|
||||||
self.form.clear()
|
self.form.clear()
|
||||||
|
try:
|
||||||
|
for attempt in range(21):
|
||||||
try:
|
try:
|
||||||
self.directory.cleanup()
|
self.directory.cleanup()
|
||||||
|
break
|
||||||
|
except PermissionError:
|
||||||
|
# Windows can retain converter file handles briefly after taskkill.
|
||||||
|
if attempt == 20:
|
||||||
|
raise
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
finally:
|
finally:
|
||||||
if self.on_close is not None:
|
if self.on_close is not None:
|
||||||
self.on_close()
|
self.on_close()
|
||||||
+6
-6
@@ -255,20 +255,20 @@ def create_app(settings: Settings | str = "config.yaml") -> FastAPI:
|
|||||||
text = body.decode("utf-8")
|
text = body.decode("utf-8")
|
||||||
except LimitExceeded:
|
except LimitExceeded:
|
||||||
if attachments is not None:
|
if attachments is not None:
|
||||||
attachments.close()
|
await attachments.aclose()
|
||||||
return JSONResponse({"error": "RequestTooLarge"}, status_code=413)
|
return JSONResponse({"error": "RequestTooLarge"}, status_code=413)
|
||||||
except InvalidMultipart:
|
except InvalidMultipart:
|
||||||
attachments.close()
|
await attachments.aclose()
|
||||||
return JSONResponse({"error": "InvalidMultipart"}, status_code=400)
|
return JSONResponse({"error": "InvalidMultipart"}, status_code=400)
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
return JSONResponse({"error": "InvalidEncoding"}, status_code=400)
|
return JSONResponse({"error": "InvalidEncoding"}, status_code=400)
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
if attachments is not None:
|
if attachments is not None:
|
||||||
attachments.close()
|
await attachments.aclose()
|
||||||
return JSONResponse({"error": "RequestTimeout"}, status_code=408)
|
return JSONResponse({"error": "RequestTimeout"}, status_code=408)
|
||||||
except BaseException:
|
except BaseException:
|
||||||
if attachments is not None:
|
if attachments is not None:
|
||||||
attachments.close()
|
await attachments.aclose()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def prepare():
|
async def prepare():
|
||||||
@@ -357,7 +357,7 @@ def create_app(settings: Settings | str = "config.yaml") -> FastAPI:
|
|||||||
if session is not None:
|
if session is not None:
|
||||||
await session.aclose()
|
await session.aclose()
|
||||||
if attachments is not None:
|
if attachments is not None:
|
||||||
attachments.close()
|
await attachments.aclose()
|
||||||
|
|
||||||
async def prepare_with_budget():
|
async def prepare_with_budget():
|
||||||
async with asyncio.timeout(settings.limits.upstream_timeout_seconds):
|
async with asyncio.timeout(settings.limits.upstream_timeout_seconds):
|
||||||
@@ -371,6 +371,6 @@ def create_app(settings: Settings | str = "config.yaml") -> FastAPI:
|
|||||||
return JSONResponse({"error": "MiddlewareFailure"}, status_code=502)
|
return JSONResponse({"error": "MiddlewareFailure"}, status_code=502)
|
||||||
finally:
|
finally:
|
||||||
if attachments is not None and not isinstance(response, ProxyStream):
|
if attachments is not None and not isinstance(response, ProxyStream):
|
||||||
attachments.close()
|
await attachments.aclose()
|
||||||
|
|
||||||
return app
|
return app
|
||||||
@@ -267,7 +267,7 @@ class Session:
|
|||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
if self.attachments is not None:
|
if self.attachments is not None:
|
||||||
self.attachments.close()
|
await self.attachments.aclose()
|
||||||
finally:
|
finally:
|
||||||
self.manager.sessions.discard(self)
|
self.manager.sessions.discard(self)
|
||||||
|
|
||||||
|
|||||||
@@ -517,6 +517,30 @@ async def test_extraction_timeout_and_cancellation_reap_process(tmp_path, monkey
|
|||||||
assert children[1].returncode is not None
|
assert children[1].returncode is not None
|
||||||
|
|
||||||
|
|
||||||
|
async def test_cleanup_retries_transient_file_locks_without_blocking(monkeypatch):
|
||||||
|
released = asyncio.Event()
|
||||||
|
notifications = []
|
||||||
|
store = AttachmentStore(Limits(), on_close=lambda: notifications.append("closed"))
|
||||||
|
store.raw_path.write_bytes(b"original upload")
|
||||||
|
cleanup = store.directory.cleanup
|
||||||
|
|
||||||
|
def locked_cleanup():
|
||||||
|
if not released.is_set():
|
||||||
|
raise PermissionError("converter is still closing its handles")
|
||||||
|
cleanup()
|
||||||
|
|
||||||
|
monkeypatch.setattr(store.directory, "cleanup", locked_cleanup)
|
||||||
|
|
||||||
|
async def release_lock():
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
released.set()
|
||||||
|
|
||||||
|
await asyncio.gather(store.aclose(), release_lock())
|
||||||
|
await store.aclose()
|
||||||
|
assert notifications == ["closed"]
|
||||||
|
assert_clean([store])
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("stage", ["receive", "extract", "stream"])
|
@pytest.mark.parametrize("stage", ["receive", "extract", "stream"])
|
||||||
async def test_disconnect_cleans_upload_at_each_stage(tmp_path, stores, monkeypatch, stage):
|
async def test_disconnect_cleans_upload_at_each_stage(tmp_path, stores, monkeypatch, stage):
|
||||||
from middleware import attachments
|
from middleware import attachments
|
||||||
|
|||||||
Reference in new issue
Block a user