Skip to content

Multi-threaded access to index.add() runs into a deadlock (Swift) #354

Closed

Description

@spicymatt

Describe the bug

When calling USearchIndex.add() from multiple threads in the Swift concurrency (Swift) a deadlock situation arises in this code:

inline node_lock_t node_lock_(std::size_t slot) const noexcept

Threads are stuck in this loop and never return

Steps to reproduce

Use a TaskGroup like in the following example to add the vectors to the array concurrently:

public protocol USearchSignature:Sendable {
var us_id:UInt64 {get}
var us_vector:[Float] {get}
}

var index = USearchIndex
...
// used to control how concurrent we are
let numberOfConcurentTask = 5

// vectors is an array of USearchSignature
public add (vectors:[USearchSignature]) async throws {

var iterator = vectors.makeIterator()

// async code here adds the content in a concurrent manner into the index
for _ in 0..<numberOfConcurentTask {
if let current = iterator.next() {
try Task.checkCancellation()
group.addTask{
index.add(key: current.us_id, vector: current.us_vector)
}
}
}
for try await _ in group {
if let current = iterator.next() {
try Task.checkCancellation()
group.addTask{
index.add(key: current.us_id, vector: current.us_vector)
}
}
}
}

Expected behavior

No deadlock

uSearch is supposed to be thread safe for addition of vectors. It is crucial for us to be able to concurrently add vectors to the index when the system launches since the index building is too slow in single threaded mode. We use a saved index whenever we can...but sometimes we need to reload a 100k vector index (512 floats per vector) efficiently. Multi-threaded loading gives us decent performance. But we cannot afford such deadlocks and we do get them in the current code.

USearch version

v2.9

Operating System

macOS Sonoma

Hardware architecture

Arm

Which interface are you using?

Other bindings

Contact Details

[email protected]

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    bug
    on Feb 25, 2024
  2. ashvardanian commented on Feb 26, 2024

    @ashvardanian
    Member

    Interesting! Do you have a dataset example? I will try to reproduce.

  3. spicymatt commented on Feb 26, 2024

    @spicymatt
    Author

    The dataset does not seem to matter. Will try to reproduce it in a sample but here is an example of a blocked code. I have 3 threads waiting for each other in

        return __atomic_fetch_or(&slots_[i / bits_per_slot()], mask, __ATOMIC_ACQUIRE) & mask;
    

    of method: inline bool atomic_set(std::size_t i) noexcept {...}

    Screenshot 2024-02-26 at 09 38 32

  4. spicymatt commented on Feb 27, 2024

    @spicymatt
    Author

    @ashvardanian is this something you can reproduce on your side ? Regards.

  5. ashvardanian commented on Feb 27, 2024

    @ashvardanian
    Member

    Hi @spicymatt! I think I can, but I was working on the C# issues yesterday, and will try to find time to work on Swift today. It would be great if you could open a PR with a minimal test that breaks. It will definitely make things faster for me. Thanks!

  6. spicymatt commented on Feb 27, 2024

    @spicymatt
    Author
  7. ashvardanian commented on Nov 14, 2026

    @ashvardanian
    Member

    @spicymatt, thanks for the report! Insertion is rewritten in 3.0, and USearch is now one of the few search engines with batch consistency guarantees: every add call is all-or-nothing, a concurrent search sees all of its entries or none, and a failed batch leaves the index untouched. Batches claim and publish their entries under per-node locks, with the protocol model-checked in Spin and GenMC, and the Swift SDK passes an executor per call. It's in the 3.0 release candidate on main; please reopen if you still see a hang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions