'how to solve python ilock FileNotFoundError?

so i am trying to use ilock in python as a system wide lock, but after few iterations in my code i get the following error, what might cause such an error? and how i can start solving it

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/ilock-bfe0d208735d8d5f20bb2b8abcf8bf67d696f23629b4ee2d4e7304f69063db61.lock'



Solution 1:[1]

I'm seeing the same error. Looks to me like it's a bug in the ilock library.

In short, when two ILock objects are created with the same unique name, they'll use the same file as the locking entity (passed to portalocker). They create the file (if it doesn't exist) using open(path, 'w') upon ILock.__enter__, and they call os.unlink(path) upon ILock.__exit__.

However consider the following scenario:

process1: ILock.__enter__  # file is created, lock acquired
process2: ILock.__enter__  # file already exists, lock pending
process1: does its thing under the lock
process1: ILock.__exit__   # file is unlinked, lock released
process2: does its thing under the lock
process2: ILock.__exit__   # Error: cannot unlink, file does not exist

On the surface, it could be that this can be fixed by silently allowing unlink to fail; or perhaps, by recreating the file as necessary after the lock has been acquired. I am not sure though if portalocker would behave nicely in this case.

Perhaps the easiest workaround is to simply NEVER delete the file (get rid of os.unlink altogether).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yonatan