summary refs log tree commit diff
path: root/synapse/util/caches
diff options
context:
space:
mode:
authorSean Quah <8349537+squahtx@users.noreply.github.com>2021-11-30 16:28:02 +0000
committerGitHub <noreply@github.com>2021-11-30 16:28:02 +0000
commit7ff22d6da41cd5ca80db95c18b409aea38e49fcd (patch)
tree99dbc2a8eb19b7c1ecf23665970d706a995e7b91 /synapse/util/caches
parentEliminate a few `Any`s in `LruCache` type hints (#11453) (diff)
downloadsynapse-7ff22d6da41cd5ca80db95c18b409aea38e49fcd.tar.xz
Fix `LruCache` corruption bug with a `size_callback` that can return 0 (#11454)
When all entries in an `LruCache` have a size of 0 according to the
provided `size_callback`, and `drop_from_cache` is called on a cache
node, the node would be unlinked from the LRU linked list but remain in
the cache dictionary. An assertion would be later be tripped due to the
inconsistency.

Avoid unintentionally calling `__len__` and use a strict `is None`
check instead when unwrapping the weak reference.
Diffstat (limited to 'synapse/util/caches')
-rw-r--r--synapse/util/caches/lrucache.py5
1 files changed, 4 insertions, 1 deletions
diff --git a/synapse/util/caches/lrucache.py b/synapse/util/caches/lrucache.py
index 05c4dcb062..eb96f7e665 100644
--- a/synapse/util/caches/lrucache.py
+++ b/synapse/util/caches/lrucache.py
@@ -271,7 +271,10 @@ class _Node(Generic[KT, VT]):
         removed from all lists.
         """
         cache = self._cache()
-        if not cache or not cache.pop(self.key, None):
+        if (
+            cache is None
+            or cache.pop(self.key, _Sentinel.sentinel) is _Sentinel.sentinel
+        ):
             # `cache.pop` should call `drop_from_lists()`, unless this Node had
             # already been removed from the cache.
             self.drop_from_lists()