harlowja / fasteners

A python package that provides useful locks.
Apache License 2.0
243 stars 45 forks source link

Ferature request: A lock type that works for both threads and processes #93

Open defnull opened 1 year ago

defnull commented 1 year ago

InterProcessLock silently fails if multiple locks on the same file are created in multiple threads of the same process. All threads can acquire the lock, because their process already owns the lock and fcntl.lockf() returns without error. This is obvious if you know how InterProcessLock is implemented and how file lock work on the OS level, but still may surprise users (including myself) and result in hard to debug and reproduce bugs. For example, most deployment options for web applications allow both thread- and process-based concurrency, or even a combination of both. An app might work in one environment but silently fail in other deployments.

To follow the principle of least surprise, InterProcessLock should also acquire thread-level locks, or come with a very visible warning that it does NOT work as expected in multi-threaded environments. I'd love to see a new lock implementation that combines both lock-types in a save way. This would perhaps also help avoid confusion for developers using this library.

andersjel commented 5 months ago

Maybe https://www.gnu.org/software/libc/manual/html_node/Open-File-Description-Locks.html could help if you can find something else for other platforms than Linux.

Or maybe https://manpages.debian.org/bookworm/manpages-dev/flock.2.en.html. The Python docs are a little unclear on how exactly this is wrapped across platforms.