@@ -259,35 +259,6 @@ def __init__(self, *args, **kwargs):
259259 self ._wait (wait )
260260 continue
261261
262- except FileExistsError :
263- # Lockfile exists, wait and try again
264- self ._wait (wait )
265- if max_lock_time is not None :
266- try :
267- kill_lock = False
268- try :
269- with self .lockfile .open ('r' ) as fid :
270- info = json .load (fid )
271- except :
272- continue
273- if 'free_after' in info and int (info ['free_after' ]) > 0 \
274- and int (info ['free_after' ]) < time .time ():
275- # We free the original process by deleting the lockfile
276- # and then we go to the next step in the while loop.
277- # Note that this does not necessarily imply this process
278- # gets to use the file; which is the intended behaviour
279- # (first one wins).
280- kill_lock = True
281- if kill_lock :
282- self .lockfile .unlink ()
283- self ._print_debug ("init" ,f"freed { self .lockfile } because "
284- + "of exceeding max_lock_time" )
285- except FileNotFoundError :
286- # All is fine, the lockfile disappeared in the meanwhile.
287- # Return to the while loop.
288- continue
289- continue
290-
291262 except PermissionError :
292263 # Special case: we can still access eos files when permission has expired, using `eos`
293264 if isinstance (self .file , EosPath ):
@@ -315,35 +286,36 @@ def __init__(self, *args, **kwargs):
315286 raise PermissionError (f"Cannot access { self .lockfile } ; permission denied." )
316287
317288 except OSError :
318- # An error happened while trying to generate the Lockfile. This raised an
319- # OSError: [Errno 5] Input/output error!
289+ # Two typical cases: the lockfile already exists (FileExistsError, a subclass of OSError),
290+ # or an input/output error happened while trying to generate it (generic OSError).
291+ # In both cases, we wait a bit and try again.
320292 self ._wait (wait )
321- if self . lockfile . exists ():
322- # if max_lock_time is not None and self.lockfile.exists():
323- try :
324- kill_lock = False
325- try :
326- with self .lockfile .open ('r' ) as fid :
327- info = json .load (fid )
328- except :
329- continue
330- if 'free_after' in info and int ( info [ 'free_after' ]) > 0 \
331- and int ( info [ 'free_after' ]) < time . time ():
332- # We free the original process by deleting the lockfile
333- # and then we go to the next step in the while loop.
334- # Note that this does not necessarily imply this process
335- # gets to use the file; which is the intended behaviour
336- # (first one wins).
337- kill_lock = True
338- if kill_lock :
339- self . lockfile . unlink ( )
340- self . _print_debug ( "init" , f" freed { self . lockfile } because "
341- + "of exceeding max_lock_time" )
342- except FileNotFoundError :
343- # All is fine, the lockfile disappeared in the meanwhile.
344- # Return to the while loop.
345- continue
346- continue
293+ # We also have to capture the case where the lockfile expired and can be freed.
294+ # So we try to read it and look for the timeout period; if this fails (e.g. because the
295+ # lock disappeared in the meanwhile), we continue the mainloop
296+ try :
297+ kill_lock = False
298+ with self .lockfile .open ('r' ) as fid :
299+ info = json .load (fid )
300+ if 'free_after' in info and int ( info [ 'free_after' ]) > 0 \
301+ and int ( info [ 'free_after' ]) < time . time ():
302+ # We free the original process by deleting the lockfile
303+ # and then we go to the next step in the while loop.
304+ # Note that this does not necessarily imply this process
305+ # gets to use the file; which is the intended behaviour
306+ # (first one wins).
307+ kill_lock = True
308+ if kill_lock :
309+ self . lockfile . unlink ()
310+ self . _print_debug ( "init" , f"freed { self . lockfile } because "
311+ + "of exceeding max_lock_time" )
312+ # Whether or not the lockfile was freed, we continue to the main loop
313+ continue
314+
315+ except ( OSError , json . JSONDecodeError ):
316+ # Any error in trying to read (and potentially kill the lock) implies
317+ # a return to the main loop
318+ continue
347319
348320 # Success!
349321 self ._access = True
0 commit comments