Repository: matrix-org/synapse

Issue:
AttributeError: 'LruCache' object has no attribute '_on_resize'
I'm getting the following after updating `a9a8f2972..85c56445f`

```
2020-10-19 13:52:34,985 - synapse.federation.federation_server - 927 - ERROR - PUT-181 - Failed to handle edu 'm.typing'
Traceback (most recent call last):
  File "/home/erikj/synapse/synapse/federation/federation_server.py", line 923, in on_edu
    await handler(origin, content)
  File "/home/erikj/synapse/synapse/handlers/typing.py", line 328, in _recv_edu
    users = await self.store.get_users_in_room(room_id)
  File "/home/erikj/synapse/synapse/util/caches/descriptors.py", line 153, in __get__
    if expected_thread is not threading.current_thread():
  File "/home/erikj/synapse/synapse/util/caches/deferred_cache.py", line 93, in __init__
  File "/home/erikj/synapse/synapse/util/caches/lrucache.py", line 128, in __init__
  File "/home/erikj/synapse/synapse/util/caches/__init__.py", line 120, in register_cache
    metric_name = "cache_%s_%s" % (cache_type, cache_name)
  File "/home/erikj/synapse/synapse/config/cache.py", line 83, in add_resizable_cache
    properties.resize_all_caches_func()
  File "/home/erikj/synapse/synapse/config/cache.py", line 208, in resize_all_caches
    callback(new_factor)
  File "/home/erikj/synapse/synapse/util/caches/lrucache.py", line 386, in set_cache_factor
AttributeError: 'LruCache' object has no attribute '_on_resize'
```


Discussion:
Looks like https://github.com/matrix-org/synapse/pull/8561 calls `register_cache` in `LruCache.__init__` before we actually define the `_on_resize` callbacks. I think its just a case of moving it to the end of the function.

(How is CI happy with this?)
CI is happy with it because it doesn't have any per-cache cache-factor config, and `set_cache_factor` has:

```
        if new_size != self.max_size:
```

in other words, no resizing is necessary so it doesn't run _on_resize.

Resolving patch:
diff --git a/synapse/util/caches/lrucache.py b/synapse/util/caches/lrucache.py
--- a/synapse/util/caches/lrucache.py
+++ b/synapse/util/caches/lrucache.py
@@ -124,6 +124,10 @@ def __init__(
         else:
             self.max_size = int(max_size)
 
+        # register_cache might call our "set_cache_factor" callback; there's nothing to
+        # do yet when we get resized.
+        self._on_resize = None  # type: Optional[Callable[[],None]]
+
         if cache_name is not None:
             metrics = register_cache(
                 "lru_cache",
@@ -332,7 +336,10 @@ def cache_contains(key: KT) -> bool:
             return key in cache
 
         self.sentinel = object()
+
+        # make sure that we clear out any excess entries after we get resized.
         self._on_resize = evict
+
         self.get = cache_get
         self.set = cache_set
         self.setdefault = cache_set_default
@@ -383,6 +390,7 @@ def set_cache_factor(self, factor: float) -> bool:
         new_size = int(self._original_max_size * factor)
         if new_size != self.max_size:
             self.max_size = new_size
-            self._on_resize()
+            if self._on_resize:
+                self._on_resize()
             return True
         return False


Test patch:
diff --git a/tests/util/test_lrucache.py b/tests/util/test_lrucache.py
--- a/tests/util/test_lrucache.py
+++ b/tests/util/test_lrucache.py
@@ -19,7 +19,8 @@
 from synapse.util.caches.lrucache import LruCache
 from synapse.util.caches.treecache import TreeCache
 
-from .. import unittest
+from tests import unittest
+from tests.unittest import override_config
 
 
 class LruCacheTestCase(unittest.HomeserverTestCase):
@@ -83,6 +84,11 @@ def test_clear(self):
         cache.clear()
         self.assertEquals(len(cache), 0)
 
+    @override_config({"caches": {"per_cache_factors": {"mycache": 10}}})
+    def test_special_size(self):
+        cache = LruCache(10, "mycache")
+        self.assertEqual(cache.max_size, 100)
+
 
 class LruCacheCallbacksTestCase(unittest.HomeserverTestCase):
     def test_get(self):