Repository navigation
Description
Describe the bug
flat_hash_multi_set_gt::equal_iterator_gt::operator++() in index_plugins.hpp (line ~3920) can enter an infinite loop when every slot in the hash table is either live or tombstoned (no never-populated slots remain).
The do { ... } while (true) loop only breaks on an empty slot (~populated & mask) or a matching live entry. When all slots are populated — which happens after many add/remove cycles — neither condition is ever true after the last match is consumed, and the loop spins forever.
// index_plugins.hpp ~L3920
equal_iterator_gt& operator++() {
do {
index_ = (index_ + 1) & (parent_->capacity_slots_ - 1);
auto slot = parent_->slot_ref(index_);
bool is_empty = ~slot.header.populated & slot.mask; // never true
bool is_match = !(slot.header.deleted & slot.mask) && equals_(slot.element, query_); // false after last match
if (is_empty || is_match)
break; // never reached
} while (true); // infinite loop
return *this;
}This is called from index_dense_gt::remove() via std::distance(matching_slots.first, matching_slots.second) and the subsequent for loop over the equal_range iterators.
Steps to reproduce
- Create an index
- Add N entries (enough to fill the hash table's initial capacity)
- Remove most of them (creates tombstones:
populated=1, deleted=1) - Add new entries that reuse tombstoned slots
- Repeat add/remove cycles until zero never-populated slots remain
- Call
remove()on any key —operator++spins forever
The trigger condition is: populated_count + tombstone_count == capacity_slots_ (zero virgin slots). This happens faster with small indices and high churn.
Workload that triggers it
AI agent memory systems with ephemeral/journaling stores — continuous insert → evict cycles on a bounded-size index. The index stays small (~hundreds to low thousands of entries) but the cumulative add/remove count far exceeds the hash table capacity, filling every slot with either a live entry or a tombstone.
We hit this in production: a Node.js process pegged a CPU core at 100% for 2+ hours, stuck in a single index_dense_gt::remove() call. perf record showed 99.96% of samples in usearch::index_dense_gt::remove().
Root cause
flat_hash_multi_set_gt uses open addressing with tombstone deletion. erase() marks slots as deleted but keeps populated set. There is no rehashing or compaction, so tombstones accumulate monotonically. The operator++ iterator has no wrap-around guard — it relies on finding an empty (never-populated) slot to terminate, which is impossible when every slot has been populated at some point.
Note: commit 7d95f6f ("Fix: Hash set iterator returns deleted tombstones in equal_range") introduced the current while (true) pattern. The pre-fix code had a different form of the same underlying issue (the while condition also couldn't terminate when all slots were populated).
Affected versions
Confirmed present in 2.21.4 and 2.25.2 (current HEAD). The code is identical in both.
Related
- Feature: Add compact() method to reclaim memory from deleted vectors (tombstones) #726 requests a
compact()method to reclaim tombstone memory — the same tombstone accumulation that causes this infinite loop also causes the memory bloat described there. - Bug: Integer underflow in Index::size() during concurrent add/remove. #697 (closed by 7d95f6f) — the tombstone fix that introduced the
while (true)form.
Environment
- usearch 2.25.1 (npm), Node.js v24.15.0, Linux x86_64
- Reproduced via HNSW index with cosine metric, 384 dimensions
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
ashvardanian commentedon May 23, 2026
Thanks, @bingh0! Patching in a bit 🤗
Fix: Bounded probe in `equal_iterator_gt::operator++`
Fix: Reclaim `slot_lookup_` tombstones under churn (#769)
Fix: Bounded probe in `equal_iterator_gt::operator++`
Fix: Reclaim `slot_lookup_` tombstones under churn (unum-cloud#769)