OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug] Session commit Phase 2 hangs forever on transient VLM Connection error — no overall timeout, cannot cancel, blocks all later commits (QueueFS active=1)

Open lkaiiu opened this issue 3 months ago • 3 comments

问题

会话 commit 的 Phase 2(post-commit 记忆提取)在 VLM/LLM API 出现瞬时 Connection error 且重试请求挂在 TCP 层时,会永久卡死:任务一直 running、无报错、无法取消,且阻塞后续所有 commit(QueueFS active=1)。

行为对比:

  • ✅ 正常:VLM/LLM 正常返回 → Phase 2 几分钟内完成(Extracted N memories),写入 .done
  • ❌ 故障:long_term 提取的 LLM 调用碰上一次瞬时 Connection error → 任务永久 running(CPU 冻结)、无 retry 2/3、无最终异常、无 .done 也无 .failed.json → 后续所有 commit 在队列里无限等待(QueueFS concurrent=1)。唯一恢复方式:重启服务进程

复现步骤

  1. POST /api/v1/sessions/{sid}/commit,session 消息量足够大让 Phase 2 跑一段时间(实测 133 条归档消息触发)
  2. 趁 Phase 2 的 long_term 提取 LLM 调用进行中,对 VLM API 制造一次瞬时连接中断(如重启代理/丢包一次)
  3. 观察:日志出现 retry 1/3: Connection error 后静默;GET /api/v1/tasks/{id} 永远 running 且 updated_at 冻结;CPU 增量约 0
  4. POST /api/v1/tasks/{id}/cancel → PERMISSION_DENIED: ROOT may not cancel tasks
  5. 再次 POST /api/v1/sessions/{sid}/commit → 被接受但永不执行(QueueFS active=1 被卡死任务占用)

预期行为

一次瞬时网络错误不应卡死整个 commit 管线。任务应在有界时间内失败(写 .failed.json),让下一次 commit 的 Phase-2 重放(_prepare_phase2_archive_messages,session.py:3892)自动恢复。

实际行为

任务永久 running。真实日志证据(2026-08-14,openviking 0.4.11,Windows,VLM=mimo-v2.5 OpenAI 兼容端点):

2026-08-14 18:34:55,584 - openviking.models.vlm.backends.openai_vlm - WARNING - OpenAI VLM async completion failed with retryable error (retry 1/3): Connection error.; retrying in 0.54s
# 之后 25+ 分钟静默。没有 retry 2/3、没有 3/3、没有异常,任务保持 running。
# 10 秒窗口 CPU 增量:0.06s(进程在等网络,不是计算)

另外注意:wm_v2 的 tool_call 参数解析失败有完整兜底(session.py:4370-4400 regex 恢复 → 4400+ creation prompt 兜底),那些不是卡死点。卡死纯粹发生在 VLM/LLM HTTP 调用层。

根本原因

四个独立因素叠加:

  1. retry_async 无总超时(utils/model_retry.py:368):重试是 per-attempt(max_retries=3),但每次尝试可阻塞 timeout=600s(openai_vlm.py:56,ov.conf vlm.timeout: 600)。OpenAI SDK 的 600s 超时只覆盖请求读取阶段——TCP 连接本身挂起时(半开/SYN 重传)由系统协议栈决定,实际等待可远超 600s。日志停在 retry 1/3 因为 retry 2 永远不抛异常。
  2. asyncio.gather 等待所有 Phase-2 子任务(session.py:2593):archive_summary / long_term / execution 并发执行,一个网络调用挂起 → 整个 gather 停滞 → 任务既不完结也不失败。
  3. RUNNING 任务无 TTL/看门狗(service/task_tracker.py 清理只删 COMPLETED/FAILED;#3396 指出过 reindex 的同类缺口)。
  4. ROOT 不能取消任务(server/routers/tasks.py:65 PERMISSION_DENIED: ROOT may not cancel tasks)且 QueueFS concurrent=1(日志 active=1)→ 卡死任务阻塞后续所有 commit。

建议修复

按鲁棒性排序(任一即可):

  1. 给 retry_async 加总 deadline(utils/model_retry.py):整个重试循环包一层总超时,max_retries × 单次超时 后抛出异常 → Phase 2 失败 → 写 .failed.json → 下次 commit 重放自动恢复(自愈,对齐 #3334 的重放设计)
  2. 约束 openai 客户端的连接阶段:设置 httpx 超时,让 TCP 连接挂起远低于 600s 封顶(如 connect=30, read=600)
  3. 允许取消 RUNNING 任务(#3383 在做)和/或加 RUNNING 任务 TTL 看门狗(#3396 指出同一缺口)

环境

  • openviking 0.4.11,pip 安装(Windows 10),VikingFS storage,query_planner/vlm = mimo-v2.5(api.xiaomimimo.com OpenAI 兼容端点),vlm.timeout=600,vlm.max_retries=3
  • 启动方式:openviking-server.exe --config ov.conf --port 1933

关联

  • #3396 —— reindex 路径的同类「任务永久卡死」(裸 asyncio task + wait_for_request 无超时);代码路径不同,但生命周期保障缺失相同
  • #3023(closed)—— 重启时 in-flight 任务保持 running;那里处理了重启对账,但没处理本次的挂起
  • #3383 —— running 任务取消 API(进行中,仅覆盖 add-resource)

lkaiiu avatar Aug 14 '26 12:08 lkaiiu

I can take a narrow first pass on current main.

retry_async() still has no overall deadline, so a later attempt that stalls in the TCP connect path never returns and Phase 2 stays running. _build_openai_client_kwargs() also passes a single float timeout into the OpenAI client, so connect can sit for the full vlm.timeout (default 600s) or longer on a half-open socket.

Proposed scope:

  • keep retry_async() backward compatible; add an optional overall timeout that cancels the in-flight attempt and does not retry deadline expiry
  • wire that deadline only on the OpenAI VLM async completion paths as (max_retries + 1) * vlm.timeout
  • split the OpenAI/httpx timeout so connect is capped at 30s (or vlm.timeout when smaller) while read/write stay at the configured value
  • add unit tests for a hanging later attempt and for the connect/read split

Out of scope: task cancel API, RUNNING TTL/watchdog, QueueFS concurrency, Phase 2 asyncio.gather shape, retry_sync, and other VLM backends.

@qin-ctx @chenjw

starslittle avatar Aug 14 '26 14:08 starslittle

感谢反馈。我重新确认了问题,这里的长时间 running 更可能是超时和两层重试叠加造成的,不一定是永久卡死。 当前 vlm.timeout=600 是单次 HTTP 请求的超时。vlm.max_retries=3 表示首次请求失败后最多再重试 3 次,因此 VLM 层最多尝试 4 次,单轮理论上可能等待约 40 分钟。 Phase 2 外面还有一层固定的 3 次重试,也就是整个提取步骤最多执行 4 轮。两层叠加后,极端情况下可能达到:

 600 秒 × 4 次 VLM 尝试 × 4 轮 Phase 2
 ≈ 160 分钟

还需要加上重试退避时间。因此,目前“出现 retry 1/3 后 25 分钟没有新日志”的现象,仍然处于现有重试策略允许的时间范围内,暂时不足以证明请求会永久挂起。 是否增加整体 deadline 用作防御性增强,需 @qin-ctx 帮确认下。 您这边可以先通过配置缩短等待时间,例如:

{
  "vlm": {
    "timeout": 120,
    "max_retries": 1
  }
}

这样单次请求最多等待 120 秒,VLM 层最多尝试 2 次,可以显著缩短连接异常后的等待时间。具体数值可以根据模型正常响应耗时调整。

yeshion23333 avatar Aug 21 '26 07:08 yeshion23333

感谢反馈。我重新确认了问题,这里的长时间 running 更可能是超时和两层重试叠加造成的,不一定是永久卡死。 当前 vlm.timeout=600 是单次 HTTP 请求的超时。vlm.max_retries=3 表示首次请求失败后最多再重试 3 次,因此 VLM 层最多尝试 4 次,单轮理论上可能等待约 40 分钟。 Phase 2 外面还有一层固定的 3 次重试,也就是整个提取步骤最多执行 4 轮。两层叠加后,极端情况下可能达到:

 600 秒 × 4 次 VLM 尝试 × 4 轮 Phase 2
 ≈ 160 分钟

还需要加上重试退避时间。因此,目前“出现 retry 1/3 后 25 分钟没有新日志”的现象,仍然处于现有重试策略允许的时间范围内,暂时不足以证明请求会永久挂起。 是否增加整体 deadline 用作防御性增强,需 @qin-ctx 帮确认下。 您这边可以先通过配置缩短等待时间,例如:

{ "vlm": { "timeout": 120, "max_retries": 1 } } 这样单次请求最多等待 120 秒,VLM 层最多尝试 2 次,可以显著缩短连接异常后的等待时间。具体数值可以根据模型正常响应耗时调整。

感谢您的回复,我这边尝试一下

lkaiiu avatar Aug 21 '26 07:08 lkaiiu