tkem/cachetools

Documentation is not helpful on thread safety

fithisux opened this issue · 2 comments

Before reporting a bug, please make sure you have the latest cachetools version installed:

pip install --upgrade cachetools

Version 5.3.0 On Intel Mac with Python 3.11.2 , details

Darwin C02CH2W2MD6R.local 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:42:11 PST 2023; root:xnu-8792.81.3~2/RELEASE_X86_64 x86_64

Describe the bug
The bug has to do with the thread safety of cachetools according to my understanding based on the docs. The next code is based on what I was able to understand from documentation. I may be wrong.

Expected result
The string AAA should be printed once. (Thread safe)

Actual result
The string AAA is printed sometimes once and sometimes twice. (Non thread safe)

Reproduction steps

from threading import Lock
from concurrent.futures import ThreadPoolExecutor, Future
from cachetools import cached, TTLCache

lock = Lock()

if __name__ == "__main__":

    @cached(cache=TTLCache(maxsize=10, ttl=60), lock=lock)
    def do_something(some_arg: str):
        print(some_arg)

    with ThreadPoolExecutor() as executor:
        first_future: Future = executor.submit(do_something, "AAA")
        last_future: Future = executor.submit(do_something, "AAA")

    first_future.result()
    last_future.result()
tkem commented

https://cachetools.readthedocs.io/en/latest/#memoizing-decorators

The lock context manager is used only to guard access to the cache object. The underlying wrapped function will be called outside the with statement, and must be thread-safe by itself.

Thank you