Skip to content

vector-store: Fix race condition during replacement - #330

Merged
ewienik merged 2 commits into
scylladb:masterfrom
knowack1:vector-464-index-replacement-race-condition
Jan 13, 2026
Merged

ewienik merged 2 commits into
scylladb:masterfrom
knowack1:vector-464-index-replacement-race-condition

Conversation

@knowack1

@knowack1 knowack1 commented Dec 31, 2025 •

Copy link
Copy Markdown
Collaborator

Concurrent replace operations could lead to a panic on unwrap. This race
occurs if a key is removed by another thread between the existence check
and the unwrap.

Investigation revealed two underlying thread-safety issues in the usearch
library's replace operation (see unum-cloud/USearch#697):

  1. The remove operation is not safe to be called concurrently with other operations.
  2. Concurrent add and remove operations on the same key are not thread-safe.

This change fixes the panic and the non-exclusive remove by decoupling
add_or_replace into separate remove and add operations within
monitor_items. This serializes the operations, preventing the race.

The second issue (concurrent add/remove on the same key) is not addressed
in this change. It should be guaranteed by monitor_items that updates
for the same primary key are processed serially to ensure the final
state of the index matches the database.

Fixes: VECTOR-464

@mykaul

mykaul commented Jan 7, 2026

Copy link
Copy Markdown
Contributor

What's the status here?

@knowack1

knowack1 commented Jan 7, 2026

Copy link
Copy Markdown
Collaborator Author

What's the status here?

Still in progress. While fixing it, I encountered another issue, most likely on the USearch side: unum-cloud/USearch#697. I am trying to workaround the USearch problem and hope to have something ready tomorrow.

@knowack1
knowack1 force-pushed the vector-464-index-replacement-race-condition branch 9 times, most recently from 73cf7fa to 7a04caf Compare January 12, 2026 09:03
@knowack1 knowack1 changed the title Fix index replacement race condition vector-store: Fix race condition during replacement Jan 12, 2026
@knowack1
knowack1 force-pushed the vector-464-index-replacement-race-condition branch 4 times, most recently from f95792c to b82c54a Compare January 12, 2026 11:55
@knowack1
knowack1 requested a review from Copilot January 12, 2026 11:55
@github-actions github-actions Bot added the P1 label Jan 12, 2026
@knowack1

Copy link
Copy Markdown
Collaborator Author

The problem of concurrent adds for the same key (2. from cover letter) to be fixed in the scope of: https://scylladb.atlassian.net/browse/VECTOR-463.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a race condition in the vector store that could cause panics during concurrent replacement operations. The fix decouples the atomic add_or_replace operation into separate remove and add operations, with the remove being serialized using an exclusive write lock to address thread-safety issues in the underlying usearch library.

Changes:

  • Refactored the replacement logic to send separate Remove and Add index operations when updating existing embeddings
  • Updated the usearch backend to handle Remove operations with exclusive locking to prevent concurrent access issues
  • Renamed the AddOrReplace enum variant and related methods to Add to reflect the new separation of concerns

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
crates/vector-store/src/monitor_items.rs Modified to check if a key exists and send Remove before Add for updates; updated tests to expect separate Remove/Add operations
crates/vector-store/src/index/usearch.rs Refactored add_or_replace into separate add and remove functions; added exclusive lock handling for Remove operations in dispatch_task; updated tests
crates/vector-store/src/index/opensearch.rs Updated enum pattern matching from AddOrReplace to Add (internal implementation still uses add_or_replace function)
crates/vector-store/src/index/actor.rs Renamed AddOrReplace enum variant to Add and updated the IndexExt trait method accordingly

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@knowack1
knowack1 marked this pull request as ready for review January 12, 2026 12:46
@knowack1
knowack1 requested a review from ewienik January 12, 2026 12:46
ewienik
ewienik previously approved these changes Jan 12, 2026
Concurrent replace operations could lead to a panic on `unwrap`. This race
occurs if a key is removed by another thread between the existence check
and the unwrap.

Investigation revealed two underlying thread-safety issues in the `usearch`
library's replace operation (see unum-cloud/USearch#697):
1. The `remove` operation is not safe to be called concurrently with other
    operations.
2. Concurrent `add` and `remove` operations on the same key are not
    thread-safe.

This change fixes the panic and the non-exclusive remove by decoupling
`add_or_replace` into separate `remove` and `add` operations within
`monitor_items`. This serializes the operations, preventing the race.

The second issue (concurrent add/remove on the same key) is not addressed
in this change. It should be guaranteed by `monitor_items` that updates
for the same primary key are processed serially to ensure the final
state of the index matches the database.

Fixes: VECTOR-464
The test was flaky because it assumed that a search would always
succeed. However, since the add operation is concurrent, there is no
guarantee that the search will find the newly added item immediately.

This fix relaxes the assertion to account for this race condition,
allowing the search to return no results.
@knowack1
knowack1 force-pushed the vector-464-index-replacement-race-condition branch from b82c54a to a9809b2 Compare January 13, 2026 08:47
@ewienik

ewienik commented Jan 13, 2026

Copy link
Copy Markdown
Collaborator

ci failed similar to known issue: VECTOR-440

@ewienik
ewienik self-requested a review January 13, 2026 09:03
@ewienik
ewienik enabled auto-merge January 13, 2026 09:05
@ewienik
ewienik added this pull request to the merge queue Jan 13, 2026
Merged via the queue into scylladb:master with commit 88dd368 Jan 13, 2026
ewienik added a commit to ewienik/vector-store that referenced this pull request Jan 13, 2026
While testing we can see seldom crash in usearch running
concurrent_add_and_search test. The crash is in the search path.

According to scylladb#330 and unum-cloud/USearch#697 we can run concurrently only one
family of operations: add/insert is the first, search/count is the second. This
patch introduces two family operations: insert and search. Each operation
increases counter for operations as a flag that some task is running and
decreases it at finish.

Fixes: VECTOR-474
ewienik added a commit to ewienik/vector-store that referenced this pull request Jan 14, 2026
While testing we can see seldom crashes in usearch running
concurrent_add_and_search test. The crash is in the search path.

According to scylladb#330 and unum-cloud/USearch#697 we can run concurrently only one
family of operations: add/insert is the first, search/count is the second. This
patch introduces two family operations: insert and search. The commit
introduces Operation type to synchronise between them. Each operation needs to
get permit to proceed, and getting the permit is an async task (it could wait
if the operation is different than requested).

The Permit contains counter and notifier - in Drop if counter decreses to 0 the
listener will be notified.

Fixes: VECTOR-474
github-merge-queue Bot pushed a commit that referenced this pull request Jan 19, 2026
While testing we can see seldom crash in usearch running
concurrent_add_and_search test. The crash is in the search path.

According to #330 and unum-cloud/USearch#697 we can run concurrently
only one family of operations: add/insert is the first, search/count is
the second. This patch introduces two family operations: insert and
search. Each operation increases counter for operations as a flag that
some task is running and decreases it at finish.

I've run 4000 times the concurrent_add_and_search test without crash.

Fixes: VECTOR-474
swasik pushed a commit to swasik/vector-store that referenced this pull request Feb 23, 2026
While testing we can see seldom crashes in usearch running
concurrent_add_and_search test. The crash is in the search path.

According to scylladb#330 and unum-cloud/USearch#697 we can run concurrently only one
family of operations: add/insert is the first, search/count is the second. This
patch introduces two family operations: insert and search. The commit
introduces Operation type to synchronise between them. Each operation needs to
get permit to proceed, and getting the permit is an async task (it could wait
if the operation is different than requested).

The Permit contains counter and notifier - in Drop if counter decreses to 0 the
listener will be notified.

Fixes: VECTOR-474
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants