Repository navigation
feat: file_lock / FileLock next to atomic_write (#163) - #164
Merged
Merged
Conversation
…ar='', loud open errors, non-reentrant guard, stronger tests)
…ds; single finalizer
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.
Closes #163. Adds
dol.file_lock(path)(one-off context manager, yields whether the lock is held) anddol.FileLock(reusable, keeps its handle) indol/atomic.py. Additive; no new dependency.Behaviour (from xdol#23): fork-safe (pid check, reopen; thread lock renewed in the child), Windows polls
LK_NBLCKinstead ofLK_LOCK, read-only lock file falls back to a read-only open, unsupported filesystems / unopenable lock file run the body withheld=False. Threads of one process exclude each other too.Not included: the optional "appender that follows an atomically replaced file" (stays in xdol; follow-up issue).
Tests: multi-process, threads, fork inheritance, read-only lock file, release on exception, doctests.
Adversarial review (Opus, two rounds)
Round 1 found: a forked child leaving the
withreleased the parent's lock; the multiprocess test was vacuous (O_APPEND is atomic anyway);sidecar=""swapped the locked inode underatomic_write; open failures were swallowed; same-thread nesting deadlocked; unclosed handles. Round 2 found the fork hook's fresh thread lock made the child's__exit__raise, finalizer pile-up,close()inside the block deadlocking.Fixed: child
__exit__is a no-op; multiprocess test is now a read-modify-write counter;sidecarmust be non-empty; only read-only/permission cases degrade toheld=False, anything else raises; reentry and in-blockclose()raise; one finalizer; defaults hoisted to constants. Not changed: NFS per-processfcntlsemantics (two locks in one process don't exclude on NFS) is inherent toflockemulation. Verdict after fixes: merge.