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

RuntimeError: dictionary keys changed during iteration #572

Open
udatsnoi opened this issue Jun 15, 2024 · 1 comment
Open

RuntimeError: dictionary keys changed during iteration #572

udatsnoi opened this issue Jun 15, 2024 · 1 comment

Comments

@udatsnoi
Copy link

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.

@DarkRedman
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants