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

Fix a memory leak when running the unit tests. #13798

Merged
merged 3 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/13798.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a memory leak when running the unit tests.
3 changes: 2 additions & 1 deletion synapse/util/caches/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ def register_cache(
add_resizable_cache(cache_name, resize_callback)

metric = CacheMetric(cache, cache_type, cache_name, collect_callback)
metric_name = "cache_%s_%s" % (cache_type, cache_name)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is reintroduced from before #13717

caches_by_name[cache_name] = cache
CACHE_METRIC_REGISTRY.register_hook(metric.collect)
CACHE_METRIC_REGISTRY.register_hook(metric_name, metric.collect)
return metric


Expand Down
10 changes: 5 additions & 5 deletions synapse/util/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import logging
from functools import wraps
from types import TracebackType
from typing import Awaitable, Callable, Generator, List, Optional, Type, TypeVar
from typing import Awaitable, Callable, Dict, Generator, Optional, Type, TypeVar

from prometheus_client import CollectorRegistry, Counter, Metric
from typing_extensions import Concatenate, ParamSpec, Protocol
Expand Down Expand Up @@ -220,21 +220,21 @@ class DynamicCollectorRegistry(CollectorRegistry):

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

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

for pre_update_hook in self._pre_update_hooks:
for pre_update_hook in self._pre_update_hooks.values():
pre_update_hook()

yield from super().collect()

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

self._pre_update_hooks.append(hook)
self._pre_update_hooks[metric_name] = hook
clokep marked this conversation as resolved.
Show resolved Hide resolved