You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Multi-threaded access to index.add() runs into a deadlock (Swift) #354
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.
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
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!
@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.
Activity
ashvardanian commentedon Feb 26, 2024
Interesting! Do you have a dataset example? I will try to reproduce.
spicymatt commentedon Feb 26, 2024
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
of method: inline bool atomic_set(std::size_t i) noexcept {...}
spicymatt commentedon Feb 27, 2024
@ashvardanian is this something you can reproduce on your side ? Regards.
ashvardanian commentedon Feb 27, 2024
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!
spicymatt commentedon Feb 27, 2024
ashvardanian commentedon Nov 14, 2026
@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
addcall 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 onmain; please reopen if you still see a hang.