Skip to content

Commit 5dafaca

Browse files
authored
Fix: Race condition in index_gt::update
Closes #674
1 parent 541e882 commit 5dafaca

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

include/usearch/index.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,6 +3806,25 @@ class index_gt {
38063806
return {nodes_mutexes_, slot};
38073807
}
38083808

3809+
struct optional_node_lock_t {
3810+
nodes_mutexes_t& mutexes;
3811+
std::size_t slot;
3812+
inline ~optional_node_lock_t() noexcept {
3813+
if (slot != std::numeric_limits<std::size_t>::max())
3814+
mutexes.atomic_reset(slot);
3815+
}
3816+
};
3817+
3818+
inline optional_node_lock_t optional_node_lock_(std::size_t slot, bool condition) const noexcept {
3819+
if (condition) {
3820+
while (nodes_mutexes_.atomic_set(slot))
3821+
;
3822+
return {nodes_mutexes_, slot};
3823+
} else {
3824+
return {nodes_mutexes_, std::numeric_limits<std::size_t>::max()};
3825+
}
3826+
}
3827+
38093828
struct node_conditional_lock_t {
38103829
nodes_mutexes_t& mutexes;
38113830
std::size_t slot;
@@ -4133,6 +4152,8 @@ class index_gt {
41334152
node_try_conditional_lock_(candidate_slot, updated_slot != candidate_slot, failed_to_acquire);
41344153
if (failed_to_acquire)
41354154
continue;
4155+
auto optional_node_lock =
4156+
optional_node_lock_(candidate_slot, updated_slot == candidate_slot);
41364157
neighbors_ref_t candidate_neighbors = neighbors_(candidate_ref, level);
41374158

41384159
// Optional prefetching

0 commit comments

Comments
 (0)