Skip to content

Commit 1f7a91a

Browse files
committed
TST: Lock cache directory during cleanup.
This prevents multiple processes from trying to cleanup the directory at the same time (e.g., using `pytest -nauto`).
1 parent 82cb933 commit 1f7a91a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,17 @@ def _clean_conversion_cache():
321321
# (actually an overestimate: we don't convert png baselines and results).
322322
max_cache_size = 2 * baseline_images_size
323323
# Reduce cache until it fits.
324-
cache_stat = {
325-
path: path.stat() for path in _get_cache_path().glob("*")}
326-
cache_size = sum(stat.st_size for stat in cache_stat.values())
327-
paths_by_atime = sorted( # Oldest at the end.
328-
cache_stat, key=lambda path: cache_stat[path].st_atime, reverse=True)
329-
while cache_size > max_cache_size:
330-
path = paths_by_atime.pop()
331-
cache_size -= cache_stat[path].st_size
332-
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()
333335

334336

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

0 commit comments

Comments
 (0)