diff --git a/docs/changelog/roadmap.md b/docs/changelog/roadmap.md
index 90391679..41409af2 100644
--- a/docs/changelog/roadmap.md
+++ b/docs/changelog/roadmap.md
@@ -7,7 +7,6 @@
## Bugs
-- [ ] upload 接口会阻塞主进程
- [ ] LightRAG 知识库查看不了解析后的文本,偶然出现,未复现
## Next
@@ -41,4 +40,5 @@
- [x] 基于 create_agent 创建 SQL Viewer 智能体
- [x] 优化 MCP 逻辑,支持 common + special 创建方式
- [x] 修复本地知识库的 metadata 和 向量数据库中不一致的情况。
-- [x] v1 版本的 LangGraph 的工具渲染有问题
\ No newline at end of file
+- [x] v1 版本的 LangGraph 的工具渲染有问题
+- [x] upload 接口会阻塞主进程
\ No newline at end of file
diff --git a/pyproject.toml b/pyproject.toml
index efed6bc1..8982b121 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -58,6 +58,7 @@ dependencies = [
"pypinyin>=0.55.0",
"tomli",
"tomli-w",
+ "aiofiles>=24.1.0",
]
[tool.ruff]
line-length = 120 # 代码最大行宽
diff --git a/server/routers/knowledge_router.py b/server/routers/knowledge_router.py
index ad4905d6..51f86221 100644
--- a/server/routers/knowledge_router.py
+++ b/server/routers/knowledge_router.py
@@ -1,3 +1,4 @@
+import aiofiles
import asyncio
import os
import traceback
@@ -593,19 +594,26 @@ async def upload_file(
basename, ext = os.path.splitext(file.filename)
filename = f"{basename}_{hashstr(basename, 4, with_salt=True)}{ext}".lower()
file_path = os.path.join(upload_dir, filename)
- os.makedirs(upload_dir, exist_ok=True)
+
+ # 在线程池中执行同步文件系统操作,避免阻塞事件循环
+ await asyncio.to_thread(os.makedirs, upload_dir, exist_ok=True)
file_bytes = await file.read()
- content_hash = calculate_content_hash(file_bytes)
- if knowledge_base.file_existed_in_db(db_id, content_hash):
+ # 在线程池中执行计算密集型操作,避免阻塞事件循环
+ content_hash = await asyncio.to_thread(calculate_content_hash, file_bytes)
+
+ # 在线程池中执行同步数据库查询,避免阻塞事件循环
+ file_exists = await asyncio.to_thread(knowledge_base.file_existed_in_db, db_id, content_hash)
+ if file_exists:
raise HTTPException(
status_code=409,
detail="数据库中已经存在了相同文件,File with the same content already exists in this database",
)
- with open(file_path, "wb") as buffer:
- buffer.write(file_bytes)
+ # 使用异步文件写入,避免阻塞事件循环
+ async with aiofiles.open(file_path, "wb") as buffer:
+ await buffer.write(file_bytes)
return {
"message": "File successfully uploaded",