flask-caching icon indicating copy to clipboard operation
flask-caching copied to clipboard

RuntimeError: dictionary keys changed during iteration

Open udatsnoi opened this issue 1 year ago • 1 comments

I am using @cache decorator across my Flask=3.0.0 web application actively with Flask-Caching==2.2.0. In production I frequently see the following errors (not reproducible)

Example: @cache.cached(timeout=3600, key_prefix=make_cache_key)

2024-06-15 09:22:57,337: Exception possibly due to cache backend.
Traceback (most recent call last):
  File "/python3.9/site-packages/flask_caching/__init__.py", line 420, in decorated_function
    self.cache.set(
  File "/python3.9/site-packages/cachelib/simple.py", line 79, in set
    self._prune()
  File "/python3.9/site-packages/cachelib/simple.py", line 56, in _prune
    self._remove_expired(now)
  File "/python3.9/site-packages/cachelib/simple.py", line 37, in _remove_expired
    toremove = [k for k, (expires, _) in self._cache.items() if expires < now]
  File "/python3.9/site-packages/cachelib/simple.py", line 37, in <listcomp>
    toremove = [k for k, (expires, _) in self._cache.items() if expires < now]
RuntimeError: dictionary keys changed during iteration

Since it is a decorator and I don't control pruning cache, I suspect there could be a bug in the library or its dependencies. Please advice. Happy to fix it myself here if you guide me to the potential problem.

udatsnoi avatar Jun 15 '24 09:06 udatsnoi

I got the exact same error and stacktrace, I think it's related to the fact Flask can be use with multithreading but that cachelib is not threadsafe.

I think the best solution is to use a lock either on cachelib or flask-caching

DarkRedman avatar Oct 23 '24 18:10 DarkRedman