Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit cf65433

Browse files
authored
Fix a memory leak when running the unit tests. (#13798)
1 parent eaed4e6 commit cf65433

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

changelog.d/13798.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a memory leak when running the unit tests.

synapse/util/caches/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ def register_cache(
205205
add_resizable_cache(cache_name, resize_callback)
206206

207207
metric = CacheMetric(cache, cache_type, cache_name, collect_callback)
208+
metric_name = "cache_%s_%s" % (cache_type, cache_name)
208209
caches_by_name[cache_name] = cache
209-
CACHE_METRIC_REGISTRY.register_hook(metric.collect)
210+
CACHE_METRIC_REGISTRY.register_hook(metric_name, metric.collect)
210211
return metric
211212

212213

synapse/util/metrics.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import logging
1616
from functools import wraps
1717
from types import TracebackType
18-
from typing import Awaitable, Callable, Generator, List, Optional, Type, TypeVar
18+
from typing import Awaitable, Callable, Dict, Generator, Optional, Type, TypeVar
1919

2020
from prometheus_client import CollectorRegistry, Counter, Metric
2121
from typing_extensions import Concatenate, ParamSpec, Protocol
@@ -220,21 +220,21 @@ class DynamicCollectorRegistry(CollectorRegistry):
220220

221221
def __init__(self) -> None:
222222
super().__init__()
223-
self._pre_update_hooks: List[Callable[[], None]] = []
223+
self._pre_update_hooks: Dict[str, Callable[[], None]] = {}
224224

225225
def collect(self) -> Generator[Metric, None, None]:
226226
"""
227227
Collects metrics, calling pre-update hooks first.
228228
"""
229229

230-
for pre_update_hook in self._pre_update_hooks:
230+
for pre_update_hook in self._pre_update_hooks.values():
231231
pre_update_hook()
232232

233233
yield from super().collect()
234234

235-
def register_hook(self, hook: Callable[[], None]) -> None:
235+
def register_hook(self, metric_name: str, hook: Callable[[], None]) -> None:
236236
"""
237237
Registers a hook that is called before metric collection.
238238
"""
239239

240-
self._pre_update_hooks.append(hook)
240+
self._pre_update_hooks[metric_name] = hook

0 commit comments

Comments
 (0)