Improve HNSW concurrency: shared locks for read-only paths#904
Improve HNSW concurrency: shared locks for read-only paths#904benoitdion wants to merge 2 commits intomainfrom
Conversation
…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>
|
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
src/VecSim/algorithms/hnsw/hnsw.h
Outdated
|
|
||
| ElementGraphData *cur_element = getGraphDataByInternalId(curNodeId); | ||
| lockNodeLinks(cur_element); | ||
| lockNodeLinksShared(cur_element); |
There was a problem hiding this comment.
should we actually use lock_guards here?
src/VecSim/algorithms/hnsw/hnsw.h
Outdated
|
|
||
| auto *cur_element = getGraphDataByInternalId(curNodeId); | ||
| lockNodeLinks(cur_element); | ||
| lockNodeLinksShared(cur_element); |
JoanFM
left a comment
There was a problem hiding this comment.
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.
|
cursor review |
| void unlockNodeLinks(ElementGraphData *node_data) const; | ||
| [[nodiscard]] std::unique_lock<std::shared_mutex> | ||
| nodeLinksGuard(ElementGraphData *node_data) const; | ||
| [[nodiscard]] std::unique_lock<std::shared_mutex> nodeLinksGuard(idType node_id) const; |
There was a problem hiding this comment.
Unused exclusive guard helpers added
Low Severity
nodeLinksGuard(ElementGraphData*) and nodeLinksGuard(idType) are introduced but never used, while write paths lock neighborsGuard directly. This leaves dead locking APIs in hnsw.h, which increases maintenance overhead and can cause future lock behavior to diverge between helpers and call sites.


Describe the changes in the pull request
lock audit:
Mark if applicable
Note
Medium Risk
Touches core HNSW locking and neighbor update logic; while intended to increase concurrency and avoid deadlocks, any mistake could introduce races or hangs under parallel insert/search/delete workloads.
Overview
Improves HNSW per-node concurrency by upgrading
ElementGraphData::neighborsGuardtostd::shared_mutexand switching search/debug/batch-iteration neighbor traversals to shared (read) locks via new RAII helpers (nodeLinksSharedGuard/nodeLinksGuard).Refactors write/update paths to use RAII
unique_lockownership, release/reacquire locks in sorted node-id order to reduce deadlock risk (including de-duplicating the lock list), and makesgetElementIdsconstacross the HNSW class hierarchy; also removes an unused<iostream>include.Written by Cursor Bugbot for commit d586ab0. This will update automatically on new commits. Configure here.