@@ -172,19 +172,22 @@ def _clear_all_cache_files(self) -> None:
172172 path , name = os .path .split (self .cache_fpath )
173173 for subpath in os .listdir (path ):
174174 if subpath .startswith (f"{ name } _" ):
175- fpath = os .path .join (path , subpath )
176- # Retry loop to handle Windows mandatory file-locking (WinError 32):
177- # portalocker holds an exclusive lock while a thread is computing,
178- # so os.remove() may fail transiently until the lock is released.
179- for attempt in range (3 ): # pragma: no branch
180- try :
181- os .remove (fpath )
182- break
183- except PermissionError :
184- if attempt < 2 :
185- time .sleep (0.1 * (attempt + 1 ))
186- else :
187- raise
175+ self ._remove_cache_file_with_retries (os .path .join (path , subpath ))
176+
177+ @staticmethod
178+ def _remove_cache_file_with_retries (fpath : str ) -> None :
179+ # Retry loop to handle Windows mandatory file-locking (WinError 32):
180+ # portalocker holds an exclusive lock while a thread is computing,
181+ # so os.remove() may fail transiently until the lock is released.
182+ for attempt in range (3 ): # pragma: no branch
183+ try :
184+ os .remove (fpath )
185+ break
186+ except PermissionError :
187+ if attempt < 2 :
188+ time .sleep (0.1 * (attempt + 1 ))
189+ else :
190+ raise
188191
189192 def _clear_being_calculated_all_cache_files (self ) -> None :
190193 path , name = os .path .split (self .cache_fpath )
@@ -423,7 +426,7 @@ def clear_cache(self) -> None:
423426 def clear_cache_entry (self , key : str ) -> None :
424427 if self .separate_files :
425428 with suppress (FileNotFoundError ):
426- os . remove (f"{ self .cache_fpath } _{ key } " )
429+ self . _remove_cache_file_with_retries (f"{ self .cache_fpath } _{ key } " )
427430 return
428431
429432 with self .lock :
0 commit comments