Skip to content

Commit

Permalink
fix: Break reference cycles around DictCache
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisburr committed Jun 11, 2024
1 parent 43713c9 commit 49913a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
34 changes: 22 additions & 12 deletions src/DIRAC/Core/Utilities/DictCache.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def __init__(self, deleteFunction=False, threadLocal=False):
# Function to clean the elements
self.__deleteFunction = deleteFunction

self.__finalizer = weakref.finalize(self, self.purgeAll, useLock=False)
# Called when this object is deleted or the program ends
self.__finalizer = weakref.finalize(self, _purgeAll, None, self.__cache, self.__deleteFunction)

@property
def lock(self):
Expand Down Expand Up @@ -225,17 +226,26 @@ def purgeExpired(self, expiredInSeconds=0):

def purgeAll(self, useLock=True):
"""Purge all entries
CAUTION: useLock parameter should ALWAYS be True except when called from __del__
CAUTION: useLock parameter should ALWAYS be True
:param bool useLock: use lock
"""
if useLock:
self.lock.acquire()
try:
for cKey in list(self.__cache):
if self.__deleteFunction:
self.__deleteFunction(self.__cache[cKey]["value"])
del self.__cache[cKey]
finally:
if useLock:
self.lock.release()
_purgeAll(self.lock if useLock else None, self.__cache, self.__deleteFunction)


def _purgeAll(lock, cache, deleteFunction):
"""Purge all entries
This is split in to a helper function to be used by the finalizer without
needing to add a reference to the DictCache object itself.
"""
if lock:
lock.acquire()
try:
for cKey in list(cache):
if deleteFunction:
deleteFunction(cache[cKey]["value"])
del cache[cKey]
finally:
if lock:
lock.release()
3 changes: 2 additions & 1 deletion src/DIRAC/FrameworkSystem/Client/ProxyManagerClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def __init__(self):
self.__pilotProxiesCache = DictCache()
self.__filesCache = DictCache(self.__deleteTemporalFile)

def __deleteTemporalFile(self, filename):
@staticmethod
def __deleteTemporalFile(filename):
"""Delete temporal file
:param str filename: path to file
Expand Down

0 comments on commit 49913a3

Please sign in to comment.