Skip to content

Bug: Concurrent add() creates nodes unreachable by search() #735

Open

Description

@DavIvek

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

[email protected]

Are you open to being tagged as a contributor?

  • I am open to being mentioned in the project .git history 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

  1. added
    bug
    on Mar 24, 2026
  2. added
    v3
    on Mar 24, 2026
  3. ashvardanian commented on Mar 24, 2026

    @ashvardanian
    Member

    Fixing this requires substantial changes to the HNSW algorithm and were planned for the v3 🤗

  4. cpegeric commented on Jun 25, 2026

    @cpegeric
    Contributor

    please check the PR for the fix. #772

  5. swasik commented on Oct 18, 2026

    @swasik
    Contributor

    The root cause is in index_gt::add. Before linking the new node on each level, it called neighbors_(new_node, level).clear(). By then the node is already linked on its higher levels, so a concurrent add can 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. The clear() then erases that reverse link, and the other node is left with no incoming link. New nodes are zeroed in node_make_, so the clear() 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.

  6. added 2 commits that reference this issue on Oct 18, 2026
    e484cbd
    634e4ac
  7. added 2 commits that reference this issue on Nov 3, 2026
    0a8f4f0
    522572f
  8. swasik commented on Nov 4, 2026

    @swasik
    Contributor

    @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.

  9. ashvardanian commented on Nov 14, 2026

    @ashvardanian
    Member

    @DavIvek, thanks for the report! As planned, 3.0 rebuilds the insertion path and adds API designed for exactly this: health audits how much of the graph a search can reach, refine re-selects neighbors and re-links what it finds unreachable, and cursor walks 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

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

    bugv3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions