fix: 装配切换审查修复(启动异常路径韧性/清扫隔离/文案与夹具卫生)

- main.lifespan: 预热+清扫挪进 try,finally dispose_all 覆盖启动半途
  失败(幂等于更少引擎,堵 engine 泄漏);清扫循环体 per-tenant
  try/except + logger.exception 后继续——一个租户坏库不炸平台启动,
  resolve/load_from_dir 保持 fail-fast
- routes/runtime.py: RuntimeError 文案补 registry_reload dispose 旧
  runtime 成因;TenantRuntime 注解挪 TYPE_CHECKING(engine 包运行期
  零 import platform)
- conftest client: teardown 置空 app.state.registry(关残留窗);
  rmtree 先正常删,失败 warnings.warn 留痕后再 ignore_errors 兜底

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-10-12 19:37:51 +08:00
parent d041077bb6
commit 20dddc151e
3 changed files with 48 additions and 21 deletions
+10 -2
View File
@@ -2,6 +2,7 @@ import asyncio
import os
import shutil
import tempfile
import warnings
from pathlib import Path
import pytest
@@ -72,7 +73,9 @@ def client(tmp_path):
线程需要直接造数时不得用该 engine——asyncio.run 另起循环,跨循环
复用常驻连接会死锁;直连断言走 async_db,或经真实路由(WS/REST)。
- 退出: lifespan finally 里 registry.dispose_all()(Windows 文件锁,
不关则 tmp 目录删不掉),随后清理 tmp 目录。
不关则 tmp 目录删不掉);随后置空 app.state.registry(关残留窗——
不让已 dispose 的测试 Registry 被后续无夹具的 lifespan 误用)并
清理 tmp 目录(删不掉告警留痕,不静默吞)。
"""
from fastapi.testclient import TestClient
@@ -82,4 +85,9 @@ def client(tmp_path):
with TestClient(app) as c:
yield c
finally:
shutil.rmtree(tmp_path, ignore_errors=True)
app.state.registry = None
try:
shutil.rmtree(tmp_path)
except OSError as exc:
warnings.warn(f"client 夹具清理 tmp 目录失败(文件锁?): {exc}")
shutil.rmtree(tmp_path, ignore_errors=True)