[Bug]: Read-only collection concurrent query triggers permanent futex deadlock (process hangs, never recovers)

オープン
#837 コメント 0 件 リアクション 0 件 担当者 2 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@richyreachy がすでに取り組んでいます。

2026年10月21日 から。

評価

この issue はまだ評価されていません。

説明

bug
Description

Summary

Opening a collection in read-only mode and issuing concurrent query() calls — either from many threads in one process, or from two separate processes that open the same collection path — causes the whole process to deadlock indefinitely. Every in-flight query thread blocks inside collection.Query() and never returns; CPU usage drops to idle; the process cannot be recovered except by kill -9.

This happens with no writer at all — purely concurrent read-only queries.

The blocking call site (from py-spy dumps) is:
zvec/executor/query_executor.py:139 docs = collection.Query(query)
-> C++ proxima internal lock -> futex_wait (kernel), never wakes

Environment

  • zvec 0.7.0 (PyPI wheel)
  • Python 3.14.4
  • Linux x86_64, kernel 6.18.54 (WSL2 / Ubuntu 26.04)
  • Collection: ~668 docs, 768-dim FP32 dense vectors, 1000+ index shards (embedding.index.N.proxima)

Root cause (hypothesis)

A read-only query() path in the proxima C++ core performs internal index state mutation (mtime / segment bookkeeping) under a shared lock. When multiple queries (threads or processes) enter this path concurrently, the lock acquisition/release sequence forms a circular wait (futex_do_wait with no waker), deadlocking the entire process. Because the lock is process-internal, it also deadlocks across two processes that map the same on-disk collection.

Workaround we applied (partial)

Serializing all queries inside a single process with a reentrant lock eliminates the intra-process deadlock. This does NOT fix the cross-process case (two processes opening the same collection still deadlock each other). A robust fix likely needs an internal lock safe under concurrent read-only access and/or a cross-process file lock (cf. issue #247).

Impact

Any deployment that serves read-only zvec collections under concurrency (multi-threaded server, or multiple processes sharing one collection path) is vulnerable to a total, unrecoverable hang of the serving process.

Related issues

  • #247 Concurrent multi-process access support without FileLock (directly relevant to the cross-process deadlock)
  • #714 SIGSEGV under concurrent fetch + insert when the writer crosses a segment switch (requires a writer; ours is read-only)
  • #8 delete_by_filter with invert field causes a hang (another hang symptom)
Steps to Reproduce
1. Create a collection with FP32 vectors and insert ~20k random docs.
2. Build the index (optimize()) and close.
3. Reopen the SAME collection in read-only mode:
     zvec.open(path, zvec.CollectionOption(read_only=True, enable_mmap=False))
4. Spawn ~32 threads, each looping concurrent collection.query(...) calls.
5. Within seconds~minutes the process deadlocks: all query threads hang, CPU goes idle, the process is wedged until kill -9.

Cross-process variant: run steps 3-4 in TWO separate processes that open the same read-only collection path; both hang.

Minimal stress script:

import zvec, numpy as np, threading, time

DIM, N, PATH = 768, 20000, "/tmp/zvec_ro_repro"
schema = zvec.CollectionSchema(name="repro", vectors=zvec.VectorSchema("embedding", zvec.DataType.VECTOR_FP32, DIM))
col = zvec.create_and_open(PATH, schema)
rng = np.random.default_rng(0)
for i in range(0, N, 1000):
    docs = [zvec.Doc(id=str(j), vectors={"embedding": rng.standard_normal(DIM).astype("float32")}) for j in range(i, min(i+1000, N))]
    col.insert(docs)
col.optimize(); col.close()

ro = zvec.open(PATH, zvec.CollectionOption(read_only=True, enable_mmap=False))
vq = lambda: zvec.VectorQuery("embedding", vector=rng.standard_normal(DIM).astype("float32"), topk=10)
stop = threading.Event()
def worker():
    while not stop.is_set():
        try: ro.query(vq(), topk=10)
        except Exception: pass
for t in [threading.Thread(target=worker, daemon=True) for _ in range(32)]: t.start()
time.sleep(120)  # in our environment the process deadlocks within seconds~minutes
Logs / Stack Trace
=== py-spy dump of the deadlocked process ===
Every blocked thread ends at:
  zvec/executor/query_executor.py:139 in _execute_single_query
    docs = collection.Query(query)
  ... -> C extension -> futex_do_wait (kernel), never wakes

6 of the blocked threads were inside _execute_single_query; the rest were
callers waiting on the same C++ lock. Thread count keeps growing because each
new request spawns a thread that never returns.

=== Read-only query mutates index files ===
Listing the collection directory while ONLY read-only queries are running shows
the mtime of embedding.index.N.proxima files advancing:

  # before queries
  -rw-r--r--  embedding.index.3.proxima   Sep 18 09:10:46
  # after a few minutes of read-only queries only
  -rw-r--r--  embedding.index.3.proxima   Sep 18 09:11:02   <- mtime moved!

A read-only collection should never rewrite its index files. The writes appear
to contend with the internal proxima lock and produce the futex deadlock.
Operating System

Linux x86_64, WSL2 / Ubuntu 26.04, kernel 6.18.54

Build & Runtime Environment

zvec 0.7.0 (PyPI wheel), Python 3.14.4 (no custom build)

Additional Context
  • I've checked git status — no uncommitted submodule changes
  • I built with CMAKE_BUILD_TYPE=Debug
  • This occurs with or without COVERAGE=ON
  • The issue involves Python ↔ C++ integration (pybind11)
主要言語
C++
スター
16.3k
フォーク
1k
平均マージ
2日 10時間
マージ済み PR(30日)
32

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

alibaba/zvec のほかの issue

alibaba/zvec の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。