Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search function in ngtpy is not thread-safe #174

Open
y-oyama-tg opened this issue Nov 24, 2024 · 0 comments
Open

search function in ngtpy is not thread-safe #174

y-oyama-tg opened this issue Nov 24, 2024 · 0 comments

Comments

@y-oyama-tg
Copy link

I'd like to report a potential bug in ngtpy. I assumed the search function in ngtpy was thread-safe because it's a read-only operation, but I encountered the issue below. (search in C++ is thread-safe according to #117, but python binding seems to be not the case.)

Repro steps

Save the following code as main.py and execute python main.py in Python 3.10 or newer.

Expected result

the Python process should exit successfully after search.

Actual result

the Python process got frozen forever.

from concurrent.futures import ThreadPoolExecutor
import ngtpy
import random

dim = 10
query = [random.random() for _ in range(dim)]

ngtpy.create(b"tmp", dim)
index = ngtpy.Index(b"tmp")

# warm-up search. if you comment in this one line, the deadlock does not occur
# index.search(query, 1)


def f(i):
    # this causes a deadlock when initializing numpy inside ngtpy if the warm-up search is not run
    return index.search(query, 1)


with ThreadPoolExecutor() as executor:
    futs = []
    for i in range(2):
        fut = executor.submit(f, i)
        futs.append(fut)

    for fut in futs:
        print(fut.result())

Workaround

As described above, adding a warm-up search on the main thread is a workaround for this problem.

Potential cause

It looks like some lines like this triggers lazy import of numpy, which may not be thread-safe.

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

No branches or pull requests

1 participant