OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

本地 GGUF embedder 并发段错误:SemanticProcessor 默认 max_concurrent_llm=32 × llama-cpp-python 单实例无锁,崩溃后队列重放形成崩溃循环

Open zzxsmile opened this issue 2 months ago • 3 comments

Description(现象)

本地部署(openviking[local-embed],embedding 走本地 GGUF 模型)在密集写入场景下,服务进程直接 SIGSEGV 崩溃,Python 层无任何异常栈。更糟的是崩溃后重启,队列会重放同一批语义处理任务 → 再次崩溃,形成崩溃循环(毒队列),必须手工清空 pending 队列才能恢复。

Steps to Reproduce

  1. uv tool install "openviking[local-embed]",embedding provider 配置为本地 GGUF 模型(本例 bge-small-zh)
  2. 触发批量语义写入,任一即可复现:
    • ov skills add 批量注册技能(一次性几十个)
    • 多路并发调用 add-memory / write
  3. 观察进程崩溃

Expected Behavior

  • 默认配置下,语义处理的并发度不应击穿本地 embedder
  • 单条任务失败不应导致整进程段错误
  • 即使单条任务异常,队列重放不应形成无限崩溃循环

Actual Behavior

  • 崩溃栈落在 libggml-blas / libggml-base(llama.cpp 底层),Python 进程直接 SIGSEGV,无 traceback
  • 重启后同一批队列任务重放,再次段错误,循环往复

Root Cause(源码定位)

两个默认值/实现相遇:

  • openviking/storage/queuefs/semantic_processor.py 中 SemanticProcessor.__init__ 默认 max_concurrent_llm=32
  • openviking/models/embedder/local_embedders.py 的本地 embedder 持有单个共享的 llama_cpp.Llama 实例,create_embedding 调用无锁

32 路并发 × 单实例无锁 → libggml 内部状态被并发读写 → 段错误。云端 embedding provider(HTTP API)天然无此问题,仅本地 GGUF provider 中招。

Environment

  • OpenViking v0.4.22(uv tool 安装;0.4.15 / 0.4.16 同样复现)
  • macOS arm64(Apple Silicon,darwin 25.6.0)
  • llama-cpp-python 0.3.35(0.3.36 源码结构相同,未见锁保护)
  • embedding provider = local GGUF(bge-small-zh)

Workaround(本地已验证)

把 semantic_processor.py 的默认值改为 max_concurrent_llm: int = 1(串行化),8 路并发写入压测稳定通过。缺点:补丁打在 site-packages 里,升级即被覆盖;且串行化牺牲语义处理吞吐。

Suggested Fixes

  1. 本地 embedder 的 create_embedding 加锁(最直接),或 provider 为 local 时强制串行
  2. max_concurrent_llm 可通过配置文件覆盖,且 provider=local 时默认值取 1
  3. 队列侧毒丸保护:同一任务连续 N 次导致进程崩溃后跳过并告警,避免崩溃循环

感谢开源这个项目——本地多 agent 共享记忆的场景它非常好用 🙏

zzxsmile avatar Oct 01 '26 09:10 zzxsmile

+1 — same root cause confirmed on a Linux x86_64 server (OpenViking 0.4.23, llama-cpp-python 0.3.35, bge-small-zh-v1.5-f16 local embedder).

Our evidence (production journal): 12 hard crashes over 3 days (Oct 5–7), 11 kernel segfault at … in libggml-cpu.so.0 + 1 GGML_ASSERT(i01 >= 0 && i01 < ne01) failed at ggml_compute_forward_get_rows, all during retrieval-peak hours. No Python traceback ever reaches the surface (SIGSEGV kills the process); clients only survived because systemd restarted within ~5 s.

One addition to the analysis: we saw the race without any ov skills add / batch import — plain search traffic on the event loop (embed_async default implementation calls embed() synchronously from coroutines) plus background queue-ingest threads reaching the shared Llama instance concurrently is enough. So max_concurrent_llm=1 alone may not cover the event-loop side of the race.

Workaround we shipped today (verified): a threading.Lock() around create_embedding() inside LocalDenseEmbedder — process-wide serialization at the embedder level, no queue reconfiguration. Throughput is fine for bge-small (~100 ms/vec, mostly serialized in practice anyway); 0 crashes since the patch+restart (12 in the 3 days before).

Suggested upstream fix: the lock belongs inside LocalDenseEmbedder (the single shared Llama instance is the invariant it protects), not only as a queue default — the queue concurrency cap and the embedder-level lock together would be belt and braces.

zwg2026pc avatar Oct 07 '26 06:10 zwg2026pc

感谢报告和补充验证。#5548 已在本地 GGUF embedder 内对共享 Llama 实例的访问加锁,修复检索请求与后台写入并发进入同一实例的竞态;修复已包含在 v0.5.0 中,无需把整个语义队列的并发度降到 1。

本 Issue 按共享实例并发问题已修复关闭。这里的修复范围是该竞态,不代表所有原生库崩溃或崩溃后队列重放问题都已解决。

qin-ctx avatar Oct 12 '26 09:10 qin-ctx

Independent confirmation on Windows x64 (win32, Python 3.14.7 venv) — same root cause, same crash signature:

  • OpenViking 0.4.23, llama-cpp-python 0.3.36, local GGUF embedder bge-small-zh-v1.5-f16 (CPU), auth_mode=trusted single-tenant server.
  • openviking-server.log records 30 occurrences of GGML_ASSERT(i01 >= 0 && i01 < ne01) failed (ggml-cpu ops.cpp:5281) between 2026-10-21 and 2026-11-01, each followed by an immediate native abort — no Python traceback, exactly as described (libggml internal state corrupted → assertion in ggml_compute_forward_get_rows).
  • The crash kills the whole server process, so port 1934 goes silent. Unlike the systemd setups above, our runtime only lazily restarts on the next request, which produced an observed 21-minute outage window (verified independently: server process chain CreationDate == restart moment, watchpoint by another agent runtime).
  • Mitigation deployed meanwhile: a cron-driven probe that detects the dead listener and respawns openviking-server.exe with a 60 s cooldown (outage reduced from ~21 min to ~45 s including model load).
  • Reproduction note: calling the embedder directly in the same venv (single call, even a 6000-char input) does not crash — consistent with the concurrency-race analysis; the crash needs the server's concurrent request pattern.

Planning to move to 0.5.2.1 (which includes the #5548 lock) and will report back if the crashes stop. Thanks for the clear root-cause analysis.

braincat avatar Nov 01 '26 14:11 braincat