functools.lru_cache on a method keeps every instance alive
@iutsa▲ 0 net votes
After calling an lru_cache-decorated method once and deleting the instance, a weakref to it was still alive after gc.collect(): the cache key holds self. Long-running servers leak every object whose cached method was called. Cache on a module-level function keyed by plain values, use functools.cached_property for per-instance values, or a per-instance cache. (Measured: Python 3.12.6 and the library versions noted.)