usearch icon indicating copy to clipboard operation
usearch copied to clipboard

Feature: Better handling for threads more than configuration

Open mapleFU opened this issue 8 months ago • 0 comments

Describe what you are looking for

This might related to https://github.com/unum-cloud/usearch/issues/550

In usearch index_dense_gt, there are some multi-thread related context: https://github.com/unum-cloud/usearch/blob/e414f46b440fc2b989aca648fecfc4db35e22332/include/usearch/index_dense.hpp#L465

index_dense_gt::thread_lock_ will return an tid in avaiable_threads_.try_pop id if it's ::any_thread ( which is uint64_t::max ). It doesn't acquire any mutex after thread_lock_ it's called, just change the config.thread, like: https://github.com/unum-cloud/usearch/blob/e414f46b440fc2b989aca648fecfc4db35e22332/include/usearch/index_dense.hpp#L880

Noted that like: https://github.com/unum-cloud/usearch/issues/550 says. If users thread is more than the configuration threads, the assertion will failed. Moreover, if it's NDEBUG mode, the assertion will not be triggered, and try_pop will return false, cause the thread_lock_ returns uint64::max() as thread_id.

https://github.com/unum-cloud/usearch/blob/e414f46b440fc2b989aca648fecfc4db35e22332/include/usearch/index_dense.hpp#L1980-L1989

https://github.com/unum-cloud/usearch/blob/e414f46b440fc2b989aca648fecfc4db35e22332/include/usearch/index.hpp#L1324

After that, since uint64::max as thread_id is invalid, the below code will causing segment fault, since context_ptr points to an invalid address.

https://github.com/unum-cloud/usearch/blob/e414f46b440fc2b989aca648fecfc4db35e22332/include/usearch/index.hpp#L2964

There're multiple ways to fix this

  1. Possible Fix1: Regard thread more than reserve ( capacity of available_threads_ as users bug), so thread_lock_ can return a failed status in this case, and it should raise error rather than causing segment fault
  2. Possible Fix2: Adding a contexts_condition_variable_, and wait for available_threads_ until it's not empty.
  3. Possible Fix3: Allowing more contexts?

Can you contribute to the implementation?

  • [x] I can contribute

Is your feature request specific to a certain interface?

It applies to everything

Contact Details

[email protected]

Is there an existing issue for this?

  • [x] I have searched the existing issues

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

mapleFU avatar Apr 23 '25 13:04 mapleFU