fix(chat): resolve Auto-route failover event-loop freeze (#812)
Root cause: ensure_structured_column() executed a synchronous blocking engine.connect().execute(SELECT information_schema.columns ...) inside the async request path (_build_chat_history / _maybe_compress_history), freezing the uvicorn event loop and hanging all endpoints (~200s, recovered only by docker restart). Fix: - chat_context.py: remove per-request ensure_structured_column() calls; run once idempotently at app startup (main.py lifespan). Use CURRENT_DATABASE() for PostgreSQL compatibility. - main.py: add startup-only ensure_structured_column() hook (try/except). - llm_client.py: trust_env=False + bounded httpx.Timeout/wait_for so broken endpoints fail fast instead of hanging the event loop. - chat_service.py: run media generation in a background daemon thread with its own event loop (_launch_media_task_bg) so SSE end does not cancel image/video tasks (#811); remove TEMP faulthandler diagnostic. Safe-align: persist accumulated live container changes (admin/*, knowledge, market, models, config, observability, mcp_client, platform_knowledge, im_bot_platform, chat_media, capability_registry, requirements) and add workflow_templates. Verified: live failover test (model-006 -> conn-refused) returns in ~1.9s with concurrent login 0.83s/200; event-loop responsiveness restored (sub-second).
This commit is contained in:
1 parent
394a7786e0
commit
a2504bce34
27 files changed
+253
-59
No files matched your search
@@ -118,6 +118,18 @@ def _record_api_call(db, method: str, path: str, status_code: int, elapsed: floa
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("Qihu-AIFlow v2.0.0 启动中...")
|
||||
|
||||
# 幂等确保 conversation_summaries.structured_memory 列。
|
||||
# 原实现在每次聊天的请求路径中同步执行 engine.connect().execute(SELECT information_schema.columns),
|
||||
# 该同步阻塞调用运行在 asyncio 事件循环线程上,会导致整个事件循环冻结(所有端点挂死,
|
||||
# 表现为「单 worker 永久阻塞、docker restart 才恢复」)。改为启动时一次性执行,
|
||||
# 既保留对全新库的兼容,又绝不阻塞任何用户请求。
|
||||
try:
|
||||
from app.api.chat_context import ensure_structured_column
|
||||
ensure_structured_column()
|
||||
logger.info("[startup] ensure_structured_column 完成")
|
||||
except Exception as _e:
|
||||
logger.warning(f"[startup] ensure_structured_column 失败(可忽略): {_e}")
|
||||
|
||||
# 启动后台 Evolution Pipeline(知识库图谱已存在的用户配置自动恢复运行)
|
||||
try:
|
||||
from app.services.evolution_service import EvolutionWatcherManager
|
||||
|
||||
Reference in new issue
Block a user