fix: 下载文件改由任务自己的线程读写,消除并发竞争和漏下载

This commit is contained in:
2026-10-25 09:27:26 +08:00
parent 2aa8990b63
commit d3e553155d
13 changed files with 1151 additions and 509 deletions
+9
View File
@@ -21,6 +21,7 @@ from starlette.responses import JSONResponse
from app.features.download.router import router as download_router
from app.platform.auth import router as auth_router
from app.platform.keepalive import session_keepalive
from app.shared.files import RECORDS
from app.shared.paths import DATA_ROOT
from app.shared.state import state
@@ -96,6 +97,7 @@ async def _watch_parent(pid: int) -> None:
if _parent_running(pid):
continue
logger.info("父进程已退出,后台结束")
await _drain_records()
os._exit(0)
@@ -183,9 +185,16 @@ async def lifespan(_app: FastAPI):
await state.client.aclose()
for client, _challenge in state.flows.values():
await client.aclose()
await _drain_records()
_release_data_lock()
async def _drain_records() -> None:
"""把排队中的任务记录写完再退出。"""
with suppress(Exception):
await asyncio.wait_for(asyncio.wrap_future(RECORDS.barrier()), timeout=3)
app = FastAPI(title="NqiTools", lifespan=lifespan)
app.add_middleware(LocalTokenMiddleware)
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["127.0.0.1", "localhost"])