Skip to content

Fix: Load into metric-made index left thread pool empty - #755

Merged
ashvardanian merged 1 commit into
unum-cloud:main-devfrom
desertfury:fix/load-from-stream-threads
May 23, 2026
Merged

ashvardanian merged 1 commit into
unum-cloud:main-devfrom
desertfury:fix/load-from-stream-threads

Conversation

@desertfury

Copy link
Copy Markdown
Contributor

Problem

index_dense_gt::make(metric, config) builds the typed graph with
index_gt's default limits of {0, 0} — zero worker threads — and never
reserves it. index_dense_gt::load_from_stream (and view) then sized
available_threads_ straight from those pre-load limits, so loading a file
into a metric-made index left the thread pool empty. The first search or
add threw std::runtime_error("No available threads to lock").

index_gt::load_from_stream already guards against this by flooring the
thread count to 1; the dense wrapper did not.

Reproduction

auto index = index_dense_gt<std::int64_t, std::uint32_t>::make(metric, config);
index.load("file.usearch");          // never reserve()'d
index.search(query, 5);              // throws "No available threads to lock"

Fix

Floor threads_add / threads_search to at least 1 right after capturing
the old limits, in both load_from_stream and view — mirroring the
existing guard in index_gt::load_from_stream.

Test

Adds test_load_after_metric_make to cpp/test.cpp: builds and saves an
index, then loads the file into a fresh metric-made index and searches it.
Aborts on main, passes with the fix.

@ashvardanian
ashvardanian force-pushed the fix/load-from-stream-threads branch 2 times, most recently from db65345 to c030cf0 Compare May 23, 2026 17:44
@ashvardanian
ashvardanian changed the base branch from main to main-dev May 23, 2026 17:45
A freshly-`make`d dense index inherited `index_gt`'s `{0, 0}` thread
limits. The wrapper paths `load_from_stream` and `view` then sized
`available_threads_` and `cast_buffer_` from those zero limits, so the
first `search` threw "No available threads to lock".

Reserve `index_limits_t{}` inside `make` itself so the returned index is
immediately usable. The new fourth parameter accepts an explicit
`index_limits_t`; pass `{unreserved}` (a `std::defer_lock`-style tag) to
skip the upfront allocation when the next call will overwrite everything
anyway. Floor `std::thread::hardware_concurrency()` at 1 inside
`index_limits_t`'s default ctor so hosts where the runtime returns 0 don't
land in the same trap. `load_from_stream` and `view` use the new
`index_limits_t::with_thread_defaults()` helper to fill in zero thread
counts inherited from a prior `unreserved` index.

Drop the now-redundant post-`make` reserves in the Rust and C bindings
and the `hc==0` fallback in the Java binding - they are centralized.

Regression test exercises both `load` and `view` into a metric-made
index.

Co-Authored-By: Ash Vardanian <[email protected]>
Co-Authored-By: Mikhail Chichvarin <[email protected]>
Co-Authored-By: Mikhail Chichvarin <[email protected]>
@ashvardanian
ashvardanian force-pushed the fix/load-from-stream-threads branch from c030cf0 to fc26a3d Compare May 23, 2026 17:48
@ashvardanian
ashvardanian merged commit b296566 into unum-cloud:main-dev May 23, 2026
ashvardanian pushed a commit that referenced this pull request May 24, 2026
### Patch

- Fix: Refuse operations without reserved thread contexts (#757) (b8d3403)
- Fix: Checked arithmetic for allocation sizes (#763) (89da7c5)
- Fix: Preserve hash lookup capacity across thread reserves (#765) (3cf843b)
- Fix: Guard quantized casts against zero-magnitude inputs (#758) (0c903f4)
- Fix: Refuse missing metrics in C change-metric API (#760) (490c1b2)
- Fix: Short-circuit self-renames (#761) (830f31a)
- Fix: Keep `vectors_lookup_` capacity after `clear()` #759 (9d77be5)
- Improve: Serialize concurrent same-`Index` Python access with a mutex (47528b5)
- Improve: Test GIL-release contract and progress-callback path (a598493)
- Improve: Release Python GIL during long index operations (d8be67d)
- Fix: Restore `ring_gt::try_push` return value (18c44ee)
- Fix: Stop JavaScript `Remove` loop after exception throw (f3e1052)
- Fix: Stop `usearch_init` on `make` failure (2aa0070)
- Fix: Stop C-ABI metadata readers on failure (34889ee)
- Fix: Report OOM from C-ABI thread-limit changers (ad24056)
- Fix: Bounded probe in `equal_iterator_gt::operator++` (b779c47)
- Fix: Resize cast buffer in `change_metric` for new bytes-per-vector (d544745)
- Fix: Eager-reserve thread contexts in `index_dense_gt::make` (#755) (b296566)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants