fix(hnsw): fix concurrent access to max_level_ in HNSW search operations#1573
fix(hnsw): fix concurrent access to max_level_ in HNSW search operations#1573
Conversation
Fixed race condition where max_level_ was accessed outside the protection of max_level_mutex_ in searchKnn() and searchRange() functions. The issue occurred when: 1. A search thread reads max_level_ without holding the lock 2. An add thread concurrently increases max_level_ and adds new nodes 3. The search thread may traverse uninitialized or invalid node IDs, causing 'cand error' exceptions Changes: - In searchKnn(): Read max_level_ inside the lock scope into a local copy - In searchRange(): Read max_level_ inside the lock scope into a local copy This ensures both enterpoint_node_ and max_level_ are read atomically under the same lock protection, preventing inconsistent state. Fixes: cand error in concurrent add/search scenarios Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
Summary of ChangesHello @inabao, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical concurrency issue within the HNSW algorithm's search functions. By ensuring that the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses a race condition in the searchKnn and searchRange functions of the HNSW implementation. Previously, max_level_ was accessed outside the protection of max_level_mutex_, which could lead to inconsistent state and exceptions during concurrent add and search operations. The fix involves reading max_level_ into a local variable within the same critical section where enterpoint_node_ is read. This ensures both values are retrieved atomically. The changes are correct, minimal, and effectively resolve the identified concurrency issue.
|
@copilot, please help to review this pull request |
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## main #1573 +/- ##
==========================================
- Coverage 91.24% 91.01% -0.23%
==========================================
Files 329 329
Lines 19396 19398 +2
==========================================
- Hits 17697 17656 -41
- Misses 1699 1742 +43
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
…ons (#1573) Fixed race condition where max_level_ was accessed outside the protection of max_level_mutex_ in searchKnn() and searchRange() functions. The issue occurred when: 1. A search thread reads max_level_ without holding the lock 2. An add thread concurrently increases max_level_ and adds new nodes 3. The search thread may traverse uninitialized or invalid node IDs, causing 'cand error' exceptions Changes: - In searchKnn(): Read max_level_ inside the lock scope into a local copy - In searchRange(): Read max_level_ inside the lock scope into a local copy This ensures both enterpoint_node_ and max_level_ are read atomically under the same lock protection, preventing inconsistent state. Fixes: cand error in concurrent add/search scenarios Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
…ons (#1573) Fixed race condition where max_level_ was accessed outside the protection of max_level_mutex_ in searchKnn() and searchRange() functions. The issue occurred when: 1. A search thread reads max_level_ without holding the lock 2. An add thread concurrently increases max_level_ and adds new nodes 3. The search thread may traverse uninitialized or invalid node IDs, causing 'cand error' exceptions Changes: - In searchKnn(): Read max_level_ inside the lock scope into a local copy - In searchRange(): Read max_level_ inside the lock scope into a local copy This ensures both enterpoint_node_ and max_level_ are read atomically under the same lock protection, preventing inconsistent state. Fixes: cand error in concurrent add/search scenarios Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
…ons (#1573) Fixed race condition where max_level_ was accessed outside the protection of max_level_mutex_ in searchKnn() and searchRange() functions. The issue occurred when: 1. A search thread reads max_level_ without holding the lock 2. An add thread concurrently increases max_level_ and adds new nodes 3. The search thread may traverse uninitialized or invalid node IDs, causing 'cand error' exceptions Changes: - In searchKnn(): Read max_level_ inside the lock scope into a local copy - In searchRange(): Read max_level_ inside the lock scope into a local copy This ensures both enterpoint_node_ and max_level_ are read atomically under the same lock protection, preventing inconsistent state. Fixes: cand error in concurrent add/search scenarios Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
…ons (#1573) Fixed race condition where max_level_ was accessed outside the protection of max_level_mutex_ in searchKnn() and searchRange() functions. The issue occurred when: 1. A search thread reads max_level_ without holding the lock 2. An add thread concurrently increases max_level_ and adds new nodes 3. The search thread may traverse uninitialized or invalid node IDs, causing 'cand error' exceptions Changes: - In searchKnn(): Read max_level_ inside the lock scope into a local copy - In searchRange(): Read max_level_ inside the lock scope into a local copy This ensures both enterpoint_node_ and max_level_ are read atomically under the same lock protection, preventing inconsistent state. Fixes: cand error in concurrent add/search scenarios Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
Fixed race condition where max_level_ was accessed outside the protection of max_level_mutex_ in searchKnn() and searchRange() functions.
The issue occurred when:
Changes:
This ensures both enterpoint_node_ and max_level_ are read atomically under the same lock protection, preventing inconsistent state.
Fixes: cand error in concurrent add/search scenarios