@@ -208,46 +208,72 @@ def _file_path(self):
208208 class _FileLocker :
209209 def __init__ (self , fd ):
210210 self ._fd = fd
211+ self ._lock_fd = None
212+ self ._already_locked = False
211213
212214 def __enter__ (self ):
213215 try :
214- msvcrt .locking (self ._fd .fileno (), msvcrt .LK_LOCK | msvcrt .LK_NBLCK , 1 )
216+ self ._lock_fd = open (self ._lock_file_path , 'w' )
217+ msvcrt .locking (self ._lock_fd .fileno (), msvcrt .LK_LOCK | msvcrt .LK_NBLCK , 1 )
215218 except OSError :
219+ self ._already_locked = True
216220 raise FileAlreadyLocked ('File {0} already locked' .format (self ._file_path ))
217221
218222 def __exit__ (self , exc_type , exc_val , exc_tb ):
219- msvcrt .locking (self ._fd .fileno (), msvcrt .LK_UNLCK , 1 )
223+ if self ._already_locked :
224+ if self ._lock_fd :
225+ self ._lock_fd .close ()
226+ return
227+
228+ try :
229+ msvcrt .locking (self ._lock_fd .fileno (), msvcrt .LK_UNLCK , 1 )
230+ finally :
231+ self ._lock_fd .close ()
232+ os .remove (self ._lock_file_path )
220233
221234 @property
222235 def _file_path (self ):
223236 return self ._fd .name
224237
238+ @property
239+ def _lock_file_path (self ):
240+ """
241+ Returns
242+ -------
243+ str
244+ """
245+ return self ._file_path + '.lock'
246+
225247else :
226248 class _FileLocker :
227249 def __init__ (self , fd ):
228250 self ._fd = fd
251+ self ._already_locked = False
229252
230253 def __enter__ (self ):
231254 try :
232255 # Atomic file creation
233256 open_flags = os .O_EXCL | os .O_RDWR | os .O_CREAT
234- fd = os .open (self .lock_file_path , open_flags )
257+ fd = os .open (self ._lock_file_path , open_flags )
235258 os .close (fd )
236- except IOError :
259+ except OSError :
260+ self ._already_locked = True
237261 raise FileAlreadyLocked ('File {0} already locked' .format (self ._file_path ))
238262
239263 def __exit__ (self , exc_type , exc_val , exc_tb ):
264+ if self ._already_locked :
265+ return
240266 try :
241- os .remove (self .lock_file_path )
242- except IOError :
267+ os .remove (self ._lock_file_path )
268+ except OSError :
243269 pass
244270
245271 @property
246272 def _file_path (self ):
247273 return self ._fd .name
248274
249275 @property
250- def lock_file_path (self ):
276+ def _lock_file_path (self ):
251277 """
252278 Returns
253279 -------
@@ -770,6 +796,10 @@ def __shrink_cache(self):
770796 os .chmod (shrink_file_path , 0o666 )
771797
772798 # rename file
799+ if is_windows :
800+ # windows must close first, or will raise permission error
801+ # be careful to do something with the file after this
802+ f .close ()
773803 shutil .move (shrink_file_path , self ._cache_scope .persist_path )
774804
775805 # update last shrink time
0 commit comments