Skip to content

fix(vamana): copy a node's list under its lock before adding its back-edges - #479

Draft
ahuber21 wants to merge 2 commits into
mainfrom
fix/static-vamana-back-edge-race
Draft

ahuber21 wants to merge 2 commits into
mainfrom
fix/static-vamana-back-edge-race

Conversation

@ahuber21

Copy link
Copy Markdown
Member

VamanaBuilder::add_reverse_edges iterates graph_.get_node(node_id) without node_id's lock while another thread may add_edge(node_id, ...) under it. The unlocked read of the degree word races the locked write: UB, though benign on x86 today because add_edge only appends. The fix copies the list under its own lock first.

Gotchas:

  • Not measured yet. One extra lock and a copy of at most graph_max_degree ids per node, on the hot build path. Needs L2 and compressed build-time numbers before merge.
  • No red test. The new vamana_build_tsan target is green before and after the fix: the per-vertex spinlocks order nearly every conflicting pair, so TSan never observes the race (zero reports up to 200k points and 112 threads; the existing negative control still reports). It stays as TSan coverage of the static builder.
  • add_edge looks like a sorted insert but appends (std::find returns end - 1 when absent). Enabling its commented-out lower_bound would make this race harmful.

ahuber21 and others added 2 commits November 19, 2026 03:28
vamana_build_tsan builds 2,000 points of data/test_dataset with 8 threads
and graph_max_degree 4 under the existing opt-in TSan option, next to the
concurrent-index targets. VamanaBuilder and SimpleGraph are header-only, so
the builder's accesses are instrumented in the test translation unit.

It passes today, including on the unlocked degree read in
add_reverse_edges fixed by the next commit: TSan reports only accesses
unordered by happens-before, and the per-vertex spinlocks order nearly
every conflicting pair there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-edges

add_reverse_edges iterated graph_.get_node(node_id) without holding
vertex_locks_[node_id], while another thread could hold that lock and call
graph_.add_edge(node_id, ...). get_node reads the degree word that add_edge
writes, so the unlocked read and the locked write were a data race, which
is undefined behaviour. It was benign on x86: add_edge only appends, so the
reader saw the old or the new degree, and slots below the old degree never
changed.

The list is now copied into a per-thread buffer under its own lock. This
costs one extra lock and one copy of at most graph_max_degree ids per
node, on the build's hot path; the build-time impact is not yet measured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ahuber21
ahuber21 requested a review from razdoburdin November 19, 2026 12:38

@razdoburdin razdoburdin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks correct for me,
but benchmark it first.

@ahuber21
ahuber21 requested a max review from Copilot November 19, 2026 13:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

3 open issues
What changed in this PR

Fixes a data race in VamanaBuilder::add_reverse_edges by copying a node’s adjacency list while holding that node’s lock, and adds a ThreadSanitizer (TSan) test target for the static builder.

Changes:

  • Copy per-node neighbor lists under vertex_locks_[node_id] before iterating for reverse-edge insertion.
  • Add a new vamana_build_tsan Catch2 test exercising the static builder under TSan.
  • Extend CMake TSan target documentation and wiring to cover the static builder.
File Description
include/​svs/​index/​vamana/​vamana_build.h Avoids an unlocked adjacency-list read by copying neighbors under the node lock before adding back-edges.
tests/​svs/​index/​vamana/​build_tsan.cpp Adds a TSan-oriented test that runs a static Vamana build and asserts the resulting graph is non-empty.
tests/​CMakeLists.txt Adds the vamana_build_tsan executable/test and updates TSan target docs to include the static builder.

Review effort: Lite

Comment on lines +508 to +516
std::vector<Idx> node_neighbors{};
for (auto node_id : is) {
for (auto other_id : graph_.get_node(node_id)) {
// Another thread may append to node_id's list under its lock, so an
// unlocked read of the degree word would be a data race.
{
std::lock_guard lock{vertex_locks_[node_id]};
const auto& node_list = graph_.get_node(node_id);
node_neighbors.assign(node_list.begin(), node_list.end());
}
Comment on lines +47 to +53
auto full_data = svs::data::SimpleData<float>::load(test_dataset::data_svs_file());
const size_t dim = full_data.dimensions();

auto data = svs::data::SimpleData<float>(kPoints, dim);
for (size_t i = 0; i < kPoints; ++i) {
data.set_datum(i, full_data.get_datum(i));
}
Comment thread tests/CMakeLists.txt
Comment on lines +333 to +334
target_compile_options(vamana_build_tsan PRIVATE -fsanitize=thread -g -O1)
target_link_options(vamana_build_tsan PRIVATE -fsanitize=thread)

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants