Skip to content

Commit d6e6552

Browse files
Copilotshaypal5
andcommitted
Fix PermissionError in _clear_all_cache_files on Windows by adding retry loop
Co-authored-by: shaypal5 <917954+shaypal5@users.noreply.github.com>
1 parent 75aa8d1 commit d6e6552

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/cachier/cores/pickle.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,19 @@ def _clear_all_cache_files(self) -> None:
145145
path, name = os.path.split(self.cache_fpath)
146146
for subpath in os.listdir(path):
147147
if subpath.startswith(f"{name}_"):
148-
os.remove(os.path.join(path, subpath))
148+
fpath = os.path.join(path, subpath)
149+
# Retry loop to handle Windows mandatory file-locking (WinError 32):
150+
# portalocker holds an exclusive lock while a thread is computing,
151+
# so os.remove() may fail transiently until the lock is released.
152+
for attempt in range(3):
153+
try:
154+
os.remove(fpath)
155+
break
156+
except PermissionError:
157+
if attempt < 2:
158+
time.sleep(0.1 * (attempt + 1))
159+
else:
160+
raise
149161

150162
def _clear_being_calculated_all_cache_files(self) -> None:
151163
path, name = os.path.split(self.cache_fpath)

0 commit comments

Comments
 (0)