Skip to content

Conversation

@benoitdion
Copy link
Collaborator

@benoitdion benoitdion commented Feb 9, 2026

Describe the changes in the pull request

  • Upgrade per-node neighborsGuard from std::mutex to std::shared_mutex to allow concurrent read access during search operations.
  • Add lockNodeLinksShared/unlockNodeLinksShared methods and use them in all read-only paths: processCandidate, processCandidate_RangeSearch, greedySearchLevel, replaceEntryPoint, safeCollectAllNodeIncomingNeighbors, repairNodeConnections (read phases), getHNSWElementNeighbors, and the batch iterator. Write paths (mutuallyConnectNewElement, revisitNeighborConnections, mutuallyUpdateForRepairedNode) retain exclusive locks.
  • Make getElementIds const-correct across the HNSW class hierarchy.
  • Remove unused include.

lock audit:

Function Lock type Rationale
processCandidate shared Read-only: reads numLinks, getLinkAtPos, computes distances
processCandidate_RangeSearch shared Read-only: same pattern as above
greedySearchLevel shared Read-only: reads neighbors to find closest node
replaceEntryPoint shared Read-only: reads neighbors to find non-deleted replacement EP
safeCollectAllNodeIncomingNeighbors shared Read-only: copyLinks, getIncomingEdges, getNumLinks, getLinkAtPos
repairNodeConnections (first two sections) shared Read-only: collecting neighbor candidates before the actual repair
getHNSWElementNeighbors shared Read-only: debug API, reads neighbors
scanGraphInternal (batch iterator) shared Read-only: traversing neighbors for batch iteration
revisitNeighborConnections (lines 861/899) exclusive Write: setLinkAtPos, appendLink, setNumLinks, mutuallyRemoveNeighborAtPos
mutuallyConnectNewElement (lines 929–966) exclusive Write: appendLink, ordered lock acquisition to avoid deadlock
mutuallyUpdateForRepairedNode (lines 1386–1451) exclusive Write: setLinkAtPos, setNumLinks, newIncomingUnidirectionalEdge

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

…correctness

- Upgrade per-node neighborsGuard from std::mutex to std::shared_mutex
  to allow concurrent read access during search operations.
- Add lockNodeLinksShared/unlockNodeLinksShared methods and use them
  in all read-only paths: processCandidate, processCandidate_RangeSearch,
  greedySearchLevel, replaceEntryPoint, safeCollectAllNodeIncomingNeighbors,
  repairNodeConnections (read phases), getHNSWElementNeighbors, and the
  batch iterator. Write paths (mutuallyConnectNewElement, revisitNeighborConnections,
  mutuallyUpdateForRepairedNode) retain exclusive locks.
- Make getElementIds const-correct across the HNSW class hierarchy.
- Remove unused <iostream> include.

Co-authored-by: Cursor <cursoragent@cursor.com>
@CLAassistant
Copy link

CLAassistant commented Feb 9, 2026

CLA assistant check
All committers have signed the CLA.

@jit-ci
Copy link

jit-ci bot commented Feb 9, 2026

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 97.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.10%. Comparing base (5e08d77) to head (d586ab0).

Files with missing lines Patch % Lines
src/VecSim/algorithms/hnsw/hnsw.h 97.22% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #904      +/-   ##
==========================================
- Coverage   97.12%   97.10%   -0.02%     
==========================================
  Files         129      129              
  Lines        7500     7488      -12     
==========================================
- Hits         7284     7271      -13     
- Misses        216      217       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.


ElementGraphData *cur_element = getGraphDataByInternalId(curNodeId);
lockNodeLinks(cur_element);
lockNodeLinksShared(cur_element);
Copy link
Contributor

Choose a reason for hiding this comment

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

should we actually use lock_guards here?


auto *cur_element = getGraphDataByInternalId(curNodeId);
lockNodeLinks(cur_element);
lockNodeLinksShared(cur_element);
Copy link
Contributor

Choose a reason for hiding this comment

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

same here?

Copy link
Contributor

@JoanFM JoanFM left a comment

Choose a reason for hiding this comment

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

In general, can we try to aim for having lock guards for more safety?

- Replace manual lock/unlock pairs with std::shared_lock/std::unique_lock in hnsw.h and hnsw_batch_iterator.h to make lock release scope-safe across early exits.
- Deduplicate sorted node lock sets and guard against self-neighbor locking before acquiring paired locks, reducing deadlock/pathological lock risks without changing lock semantics.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants