summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJimmy <jimmy@spalge.com>2022-03-26 12:47:04 +1300
committerJimmy <jimmy@spalge.com>2022-03-26 13:00:55 +1300
commit56a370b971cc1bfbb5b15e97ea94348c904b7950 (patch)
treed0c5442652f7fe66fa83b11764d031311ff4b38a
parentb830c13c0bece14201ac1cf264c0550ed1f3dd4b (diff)
downloadqutebrowser-56a370b971cc1bfbb5b15e97ea94348c904b7950.tar.gz
qutebrowser-56a370b971cc1bfbb5b15e97ea94348c904b7950.zip
debugcachestats: wrap cached functions in weakref
In 4b93da6c69 I moved a cache that was registered with the debugcache module to be per-window. Which means they may be deleted at some point and we shouldn't hold strong references to them.
-rw-r--r--qutebrowser/misc/debugcachestats.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/qutebrowser/misc/debugcachestats.py b/qutebrowser/misc/debugcachestats.py
index 2004ad7ab..1e4139e43 100644
--- a/qutebrowser/misc/debugcachestats.py
+++ b/qutebrowser/misc/debugcachestats.py
@@ -23,6 +23,7 @@ Because many modules depend on this command, this needs to have as few
dependencies as possible to avoid cyclic dependencies.
"""
+import weakref
from typing import Any, Callable, List, Optional, Tuple, TypeVar
@@ -36,7 +37,8 @@ _T = TypeVar('_T', bound=Callable[..., Any])
def register(name: Optional[str] = None) -> Callable[[_T], _T]:
"""Register a lru_cache wrapped function for debug_cache_stats."""
def wrapper(fn: _T) -> _T:
- _CACHE_FUNCTIONS.append((fn.__name__ if name is None else name, fn))
+ fn_ref = weakref.ref(fn)
+ _CACHE_FUNCTIONS.append((fn.__name__ if name is None else name, fn_ref))
return fn
return wrapper
@@ -44,5 +46,9 @@ def register(name: Optional[str] = None) -> Callable[[_T], _T]:
def debug_cache_stats() -> None:
"""Print LRU cache stats."""
from qutebrowser.utils import log
- for name, fn in _CACHE_FUNCTIONS:
+ for idx, (name, fn_ref) in reversed(list(enumerate(_CACHE_FUNCTIONS))):
+ fn = fn_ref()
+ if not fn:
+ _CACHE_FUNCTIONS.pop(idx)
+ continue
log.misc.info('{}: {}'.format(name, fn.cache_info()))