Repository navigation
Conversation
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>
razdoburdin
approved these changes
Nov 19, 2026
razdoburdin
left a comment
Member
There was a problem hiding this comment.
looks correct for me,
but benchmark it first.
Contributor
There was a problem hiding this comment.
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
node_neighbors.assign(...)may repeatedly reallocate on the hot path. Consider reserving once… · New This test assumes the on-disk dataset contains at leastkPointsrows; if that dataset is ever… · New For TSan debugging quality, consider adding-fno-omit-frame-pointer(and optionally disabling… · New
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_tsanCatch2 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 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


VamanaBuilder::add_reverse_edgesiteratesgraph_.get_node(node_id)withoutnode_id's lock while another thread mayadd_edge(node_id, ...)under it. The unlocked read of the degree word races the locked write: UB, though benign on x86 today becauseadd_edgeonly appends. The fix copies the list under its own lock first.Gotchas:
graph_max_degreeids per node, on the hot build path. Needs L2 and compressed build-time numbers before merge.vamana_build_tsantarget 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_edgelooks like a sorted insert but appends (std::findreturnsend - 1when absent). Enabling its commented-outlower_boundwould make this race harmful.