Skip to content

fix(qdrant): make _ensure_collection_exists safe against concurrent callers - #174

Open
sadiqkhzn wants to merge 1 commit into
qdrant:masterfrom
sadiqkhzn:fix/ensure-collection-exists-race
Open

fix(qdrant): make _ensure_collection_exists safe against concurrent callers#174
sadiqkhzn wants to merge 1 commit into
qdrant:masterfrom
sadiqkhzn:fix/ensure-collection-exists-race

Conversation

@sadiqkhzn

Copy link
Copy Markdown

What

`_ensure_collection_exists` in `qdrant.py` does a check-then-create against the client without holding any lock. When two `store()` calls hit a not-yet-existing collection concurrently, both see `collection_exists` return `False`, both call `create_collection`, and the loser gets an "already exists" error. Local mode raises `ValueError`, a real server returns `UnexpectedResponse` with status `409`. Same race window applies to `create_payload_index`.

Fix

Narrow `except` around `create_collection` that treats the "already exists" outcome as success, since it means another caller raced us and produced the state we wanted. Same handling for `create_payload_index`. No signature or public API change.

Test

`tests/test_concurrent_collection_create.py` patches `collection_exists` to always return `False` and fires three concurrent `_ensure_collection_exists` calls. Fails on `master` with `ValueError: Collection ... already exists`, passes on this branch. The local `:memory:` client does not yield inside the check-create window, so the mock is used to force the interleaving a real server exposes naturally over the network.

Full suite: `25 passed`. `ruff check` and `ruff format --check` both clean. No `uv.lock` or `pyproject.toml` changes.

AI disclosure

Per the parent `qdrant/qdrant` CONTRIBUTING (this repo has no CONTRIBUTING of its own, so I applied the parent rules): the single commit on this branch is `[AI]` prefixed. Fix and test were drafted with Claude Opus 4.7 assistance; I read the resulting diff, ran the tests locally, and can answer questions about the reasoning. Initial prompt: audit `mcp-server-qdrant` source for real concurrency, correctness, or security defects a senior AI backend engineer would catch, and propose a minimal fix with a failing-then-passing regression test.

…ent callers

The existing check-then-create pattern is not atomic against the Qdrant server.
Two concurrent store() calls into a not-yet-existing collection can both observe
collection_exists=False and both call create_collection. Against local mode the
loser gets ValueError("Collection ... already exists"); against a server it gets
UnexpectedResponse with status 409. Either way, the caller sees a spurious error
on what should be an idempotent operation.

Wrap create_collection (and create_payload_index) in narrow except handlers that
treat the "already exists" outcome as success, since it means another caller
raced us and produced the state we wanted.

Test forces the interleaving deterministically with a mocked collection_exists,
since local ":memory:" mode does not yield inside the natural check-create
window that a real server exposes.

Generated with Claude Opus 4.7 (this repo has no CONTRIBUTING but the parent
qdrant/qdrant CONTRIBUTING requires AI disclosure). Prompt used: "Audit the
mcp-server-qdrant source for real defects a senior AI backend engineer would
catch; find any concurrency, correctness or security bugs in qdrant.py and
mcp_server.py and propose a minimal fix with a failing-then-passing regression
test."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant