🐛 fix(unix): auto-fallback to SoftFileLock on ENOSYS#480
Merged
gaborbernat merged 1 commit intotox-dev:mainfrom Feb 14, 2026
Merged
🐛 fix(unix): auto-fallback to SoftFileLock on ENOSYS#480gaborbernat merged 1 commit intotox-dev:mainfrom
gaborbernat merged 1 commit intotox-dev:mainfrom
Conversation
7ce7ea1 to
eedcc63
Compare
On platforms like Android/Termux and network filesystems (NFS, CIFS), fcntl imports fine but flock() fails at runtime with ENOSYS. Previously this raised NotImplementedError, leaving users no recourse except monkey-patching filelock.FileLock before any other imports. Now UnixFileLock catches ENOSYS, cleans up the leftover lock file, and swaps __class__ to SoftFileLock (or AsyncSoftFileLock for async instances). Both classes share the same BaseFileLock layout with no extra instance state, so the swap is safe and preserves all context. Subsequent acquire/release calls go straight through the soft path. Fixes tox-dev#289, fixes tox-dev#349
eedcc63 to
4ce9231
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On platforms like Android/Termux and network filesystems (NFS, CIFS),
fcntlimports fine butflock()fails at runtime withENOSYS. 💥 Previously this raisedNotImplementedError, leaving users no recourse except monkey-patchingfilelock.FileLock = filelock.SoftFileLockbefore any other import — fragile and easy to get wrong.This catches
ENOSYSinUnixFileLock._acquire(), cleans up the leftover lock file, and transparently swapsself.__class__toSoftFileLock(orAsyncSoftFileLockfor async instances). Both classes share the sameBaseFileLocklayout with no extra instance state, so the swap is safe and preserves all context. ✨ Subsequent acquire/release cycles go straight through the soft path with no repeatedflock()attempts.The soft fallback relies on
O_CREAT | O_EXCLwhich is atomic on NFSv3+ with Linux kernel 2.6+ and endorsed by CERT as secure against TOCTOU. Symlink attacks are mitigated byO_NOFOLLOWalready used inSoftFileLock._acquire(). On Android/Termux the local filesystem (ext4/f2fs) provides fullO_EXCLguarantees. The__class__swap is validated by CPython'scompatible_for_assignment()when both types share the same base layout.Fixes #289, fixes #349