Repository navigation
Open
Description
Describe the bug
When multiple threads call add() in parallel, some nodes become unreachable via search() despite being correctly stored in the index. contains() and get() confirm the nodes exist with correct data, but graph traversal never visits them.
Steps to reproduce
#include <iostream>
#include <random>
#include <thread>
#include <vector>
#include <usearch/index_dense.hpp>
using namespace unum::usearch;
int main() {
constexpr auto num_sessions = 10;
constexpr auto vectors_per_session = 100;
constexpr auto total_vectors = num_sessions * vectors_per_session;
constexpr auto dimensions = 128;
auto index_result = index_dense_gt<>::make(metric_punned_t(dimensions, metric_kind_t::cos_k, scalar_kind_t::f32_k));
auto& index = index_result.index;
index.try_reserve({total_vectors, num_sessions});
// Generate vectors
auto all_vectors = std::vector<std::vector<float>>(total_vectors);
auto rng = std::mt19937(42);
auto dist = std::uniform_real_distribution<float>(-1.0f, 1.0f);
for (auto& v : all_vectors) {
v.resize(dimensions);
for (auto& x : v) x = dist(rng);
}
// Parallel insert — jthreads auto-join at scope exit
{
auto threads = std::vector<std::jthread>();
for (auto s = 0; s < num_sessions; s++) {
threads.emplace_back([&, s] {
auto start = s * vectors_per_session;
for (auto i = 0; i < vectors_per_session; i++)
index.add(start + i, all_vectors[start + i].data(), s);
});
}
}
// Verify: search for every vector requesting ALL results
auto misses = 0;
for (auto i = 0; i < total_vectors; i++) {
auto sr = index.search(all_vectors[i].data(), total_vectors);
auto keys = std::vector<std::uint64_t>(total_vectors);
auto distances = std::vector<float>(total_vectors);
auto found = sr.dump_to(keys.data(), distances.data());
auto found_self = false;
for (auto r = 0; r < found; r++)
if (keys[r] == i) { found_self = true; break; }
if (!found_self) misses++;
}
// Confirm all nodes are stored correctly
auto contains_ok = 0;
for (auto i = 0; i < total_vectors; i++)
if (index.contains(i)) contains_ok++;
std::cout << "Search misses: " << misses << " / " << total_vectors << "\n";
std::cout << "contains() confirms " << contains_ok << " / " << total_vectors << " present\n";
if (misses > 0)
std::cout << "BUG: " << misses << " nodes stored but unreachable via HNSW graph traversal\n";
else
std::cout << "PASS: all nodes reachable\n";
}
Expected behavior
Nodes to be reachable.
USearch version
v2.24.0
Operating System
Ubuntu 24.04
Hardware architecture
Arm
Which interface are you using?
C++ implementation
Contact Details
Are you open to being tagged as a contributor?
- I am open to being mentioned in the project
.githistory as a contributor
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
ashvardanian commentedon Mar 24, 2026
Fixing this requires substantial changes to the HNSW algorithm and were planned for the v3 🤗
fix: hnsw run build with single thread to avoid usearch known bug (us…
cpegeric commentedon Jun 25, 2026
please check the PR for the fix. #772
Fix: concurrent add() no longer strands unreachable nodes (unum-cloud…
fix: hnsw run build with single thread to avoid usearch known bug (us…
fix: hnsw run build with single thread to avoid usearch known bug (us…
fix: hnsw run build with single thread to avoid usearch known bug (us…
swasik commentedon Oct 18, 2026
The root cause is in
index_gt::add. Before linking the new node on each level, it calledneighbors_(new_node, level).clear(). By then the node is already linked on its higher levels, so a concurrentaddcan reach it there and search the lower level from it. Finding the node's list empty, that insertion links to the node and appends a reverse link into the node's list. Theclear()then erases that reverse link, and the other node is left with no incoming link. New nodes are zeroed innode_make_, so theclear()never removes anything else.Your reproduction on 2.26.4, run over 100 rounds with random data (100,000 inserts in total), leaves 957 nodes unreachable. With the
clear()removed, 2 remain. Those 2 come from a second, single-threaded cause: refining a full neighbor list can drop a node's last incoming link. #805 fixes both and adds your scenario as a regression test.Fix: Keep reverse links added to a node still being inserted
Fix: Keep reverse links added to a node still being inserted
Fix: Keep reverse links added to a node still being inserted
Fix: Keep reverse links added to a node still being inserted
swasik commentedon Nov 4, 2026
@DavIvek, #805 has been reworked into one rule that also covers pruning, slot reuse in
update(#801, #821) and equal vectors (#815). It still fixes this issue. On your pattern (10 threads × 100 vectors, 100 rounds), it leaves 0 vectors unreachable, against 1,493 on main-dev. The pattern is one of its tests, credited to you.ashvardanian commentedon Nov 14, 2026
@DavIvek, thanks for the report! As planned, 3.0 rebuilds the insertion path and adds API designed for exactly this:
healthaudits how much of the graph a search can reach,refinere-selects neighbors and re-links what it finds unreachable, andcursorwalks the stored links for a manual audit. Reachability will be a continuous space of improvement, so don't expect 3.0 to land the final algorithm: it solidifies the API first, and we'll keep improving on it from there, so I'm keeping this open. cc @swasik @cpegeric