fix(qdrant): make _ensure_collection_exists safe against concurrent callers - #174
Open
sadiqkhzn wants to merge 1 commit into
Open
fix(qdrant): make _ensure_collection_exists safe against concurrent callers#174sadiqkhzn wants to merge 1 commit into
sadiqkhzn wants to merge 1 commit into
Conversation
…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."
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.