Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practice removal of lock file? #76

Closed
bretttully opened this issue Jul 22, 2020 · 2 comments
Closed

Best practice removal of lock file? #76

bretttully opened this issue Jul 22, 2020 · 2 comments

Comments

@bretttully
Copy link

Once the lock has been released, what is the best practice method for deleting the lock_file? Any chance of adding that to the docs.

Cheers!

@xkortex
Copy link

xkortex commented Sep 21, 2020

POSIX lockfiles are a little messy. Given the current state of locking mechanisms in *Nix systems (idk how any of this works in Windows and only superficial knowledge in linux), you probably want your application to control the state of the lock, based on your use-case.

Traditionally you write the PID of the locking process into the lock file, so that deadlocks due to processes dying while holding the lock are identifiable and fixable.

https://stackoverflow.com/a/9464945/3988037

The safest is to have a separate *.lock file, and remove it when you are done with it. If the process dies and leaves a hanging file, you know something really goofed, and it's up to you to figure out how to deal with it.

One way (probably the best overall approach) would be to wrap your locking logic in a context manager at the application/stack level and remove the lock file on __exit__(). That way you know only during cleanup phases the file will get removed and you avoid race conditions.

Better yet, use a tempdir created by whatever you designate as your master process, and put all locks in that dir. That way, when the master dies, all dangling locks are cleaned up. If your master dies, you are probably in a wack enough state that you need to reset anything master might be sharing state with anyways.

This is already mentioned in the source
#31

And the answer is...it's again quite messy:
https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition

@bretttully
Copy link
Author

Thanks -- yes, we have been noticing the messiness; it's even worse when you have multiple servers accessing NFS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants