Skip to content

Commit 1b23ec1

Browse files
authored
Merge pull request matplotlib#18667 from QuLogic/cache-lock
TST: Lock cache directory during cleanup.
2 parents d0753c0 + 1f7a91a commit 1b23ec1

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ def make_test_filename(fname, purpose):
3434
return '%s-%s%s' % (base, purpose, ext)
3535

3636

37-
def get_cache_dir():
37+
def _get_cache_path():
3838
cache_dir = Path(mpl.get_cachedir(), 'test_cache')
3939
cache_dir.mkdir(parents=True, exist_ok=True)
40-
return str(cache_dir)
40+
return cache_dir
41+
42+
43+
def get_cache_dir():
44+
return str(_get_cache_path())
4145

4246

4347
def get_file_hash(path, block_size=2 ** 20):
@@ -286,7 +290,7 @@ def convert(filename, cache):
286290
# Only convert the file if the destination doesn't already exist or
287291
# is out of date.
288292
if not newpath.exists() or newpath.stat().st_mtime < path.stat().st_mtime:
289-
cache_dir = Path(get_cache_dir()) if cache else None
293+
cache_dir = _get_cache_path() if cache else None
290294

291295
if cache_dir is not None:
292296
_register_conversion_cache_cleaner_once()
@@ -317,15 +321,17 @@ def _clean_conversion_cache():
317321
# (actually an overestimate: we don't convert png baselines and results).
318322
max_cache_size = 2 * baseline_images_size
319323
# Reduce cache until it fits.
320-
cache_stat = {
321-
path: path.stat() for path in Path(get_cache_dir()).glob("*")}
322-
cache_size = sum(stat.st_size for stat in cache_stat.values())
323-
paths_by_atime = sorted( # Oldest at the end.
324-
cache_stat, key=lambda path: cache_stat[path].st_atime, reverse=True)
325-
while cache_size > max_cache_size:
326-
path = paths_by_atime.pop()
327-
cache_size -= cache_stat[path].st_size
328-
path.unlink()
324+
with cbook._lock_path(_get_cache_path()):
325+
cache_stat = {
326+
path: path.stat() for path in _get_cache_path().glob("*")}
327+
cache_size = sum(stat.st_size for stat in cache_stat.values())
328+
paths_by_atime = sorted( # Oldest at the end.
329+
cache_stat, key=lambda path: cache_stat[path].st_atime,
330+
reverse=True)
331+
while cache_size > max_cache_size:
332+
path = paths_by_atime.pop()
333+
cache_size -= cache_stat[path].st_size
334+
path.unlink()
329335

330336

331337
@functools.lru_cache() # Ensure this is only registered once.

0 commit comments

Comments
 (0)