Bläddra i källkod

fix: LLM 完成后前端无响应 — _background_execute 过早清理 _running_tasks

竞态条件: LLM 调用极快完成(0.14s),_background_execute 的 finally 立即
pop 掉 _running_tasks 中的 event_queue,前端 EventSource 连接时队列已不存在。

修复: _background_execute 不再立即清理 _running_tasks,改为 30s 延迟兜底清理。
GET SSE 端点消费完毕后自行清理(已完成),确保 EventSource 有足够时间连接。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
WangXuMing 1 vecka sedan
förälder
incheckning
8f28a61b18
1 ändrade filer med 8 tillägg och 1 borttagningar
  1. 8 1
      views/debug/debug_api.py

+ 8 - 1
views/debug/debug_api.py

@@ -476,7 +476,14 @@ async def _background_execute(
                 "message": str(exc),
             }))
         finally:
-            _running_tasks.pop(task_id, None)
+            # 不立即清理 _running_tasks,让 GET SSE 端点消费事件后自行清理。
+            # 兜底:30s 后如仍无人消费则清理。
+            async def _delayed_cleanup():
+                await asyncio.sleep(30)
+                if task_id in _running_tasks:
+                    logger.warning("[_background_execute] 任务 %s 30s 未被消费,强制清理", task_id)
+                    _running_tasks.pop(task_id, None)
+            asyncio.create_task(_delayed_cleanup())
 
 
 # ============ SSE 断线重连端点 ============