Everything you never wanted to know about file locking (2010)
4 months ago
- #file-locking
- #python
- #unix
- File locking in Unix is complex and varies between systems, with three main APIs: flock(), fcntl(), and lockf().
- flock() is simple but not POSIX-standardized, doesn't work over NFS, and has race conditions when upgrading locks.
- fcntl() is POSIX-standardized, supports byte-range locks, but has insane behaviors like losing locks when any file descriptor to the inode is closed.
- lockf() is also POSIX-standardized but may not be supported on all BSD systems and lacks features like querying lock ownership.
- Mixing different lock types (flock(), fcntl(), lockf()) on the same file is undefined and non-portable.
- Mandatory locks are unreliable and should be avoided; advisory locks are the only sensible choice.
- Python's fcntl module provides access to all three lock types but has its own quirks and inconsistencies.
- A bug in MacOS X 10.6's fcntl(F_SETLK) could cause silent file corruption, affecting programs like sqlite.
- Windows 10 WSL 'implements' fcntl() locks by always returning success, leading to file corruption.
- Linux introduced F_OFD_SETLK in 2015, a saner alternative to traditional fcntl() locks, but it's not yet widely portable.