Skip to content

Infinite loop in flat_hash_multi_set_gt::operator++ when all slots are populated+tombstoned #753

Closed

Description

@bingh0

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

  1. Create an index
  2. Add N entries (enough to fill the hash table's initial capacity)
  3. Remove most of them (creates tombstones: populated=1, deleted=1)
  4. Add new entries that reuse tombstoned slots
  5. Repeat add/remove cycles until zero never-populated slots remain
  6. 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

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

  1. ashvardanian commented on May 23, 2026

    @ashvardanian
    Member

    Thanks, @bingh0! Patching in a bit 🤗

  2. added a commit that references this issue on May 23, 2026
    b779c47
  3. added a commit that references this issue on Jul 10, 2026
    c6d634c
  4. added 2 commits that reference this issue on Aug 3, 2026
    e5b6c1c
    4347fcb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions