Skip to content

fix: add cross-process file lock to prevent usearch race condition (#543) - #556

Merged
ajianaz merged 1 commit into
developfrom
fix/543-usearch-race-condition
Jul 4, 2026
Merged

ajianaz merged 1 commit into
developfrom
fix/543-usearch-race-condition

Conversation

@ajianaz

@ajianaz ajianaz commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Problem

Multiple concurrent uteke remember invocations (e.g. xargs -P5) each load the usearch vector index, insert, and save — each save overwrites other inserts, causing data loss.

The RwLock<VectorIndex> only protects within a single process.

Fix

  • Add fs2 dependency for advisory file locking
  • VectorIndex acquires exclusive file lock on .usearch file during load_or_create(), held until Drop
  • Concurrent CLI invocations block at lock acquisition instead of corrupting the index
  • 288 tests pass, clippy/fmt clean

Closes #543

@ajianaz ajianaz added bug Something isn't working scope:core Core engine work priority:critical Must fix immediately labels Jul 4, 2026
@github-actions

github-actions Bot commented Jul 4, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

❌ Blocked — critical issues found.

🔴 Error (1)

  • crates/uteke-core/src/memory/vector.rs:67 — The new if !path.exists() { std::fs::write(path, [])?; } check happens before acquiring the file lock. This is a TOCTOU race: another process can create and save a valid .usearch file after exists() returns false but before write() runs, and std::fs::write will truncate that file back to zero bytes. That undermines the cross-process safety this change is trying to provide and can cause index data loss.

Review powered by cora-cli · BYOK · MIT

)

When multiple uteke remember commands run concurrently (e.g. xargs -P5),
each process loads the usearch vector index, inserts its vector, and
saves — but each save OVERWRITES the other process's inserts, causing
data loss. The error 'rename temp to final key mapping: No such file or
directory' appears.

The existing RwLock<VectorIndex> only protects concurrent threads within
a single process. Cross-process coordination requires file-based locking.

- Add fs2 dependency for advisory file locking
- VectorIndex acquires exclusive file lock on .usearch file during
  load_or_create(), held until drop
- Concurrent CLI invocations block at lock acquisition instead of
  corrupting the index
- 288 tests pass, clippy/fmt clean
@ajianaz
ajianaz force-pushed the fix/543-usearch-race-condition branch from 33c375c to 4c9761f Compare July 4, 2026 03:59
///
/// Acquires an **exclusive file lock** on the `.usearch` file to prevent
/// cross-process race conditions (e.g., `xargs -P5 uteke remember`).
/// The lock is held until this `VectorIndex` is dropped (#543).
@ajianaz
ajianaz merged commit 6a80c4f into develop Jul 4, 2026
8 of 9 checks passed
@ajianaz
ajianaz deleted the fix/543-usearch-race-condition branch July 9, 2026 14:27
ajianaz added a commit that referenced this pull request Aug 13, 2026
fix: add cross-process file lock to prevent usearch race condition (#543)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority:critical Must fix immediately scope:core Core engine work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CRITICAL: Race condition in concurrent writes corrupts usearch vector index

2 participants