fix(lock): kernel-mediated byte lock with fail-safe cleanup and classified contention - #1
Merged
Merged
Conversation
The stale-file sweep read a pid from the lock file and unlinked the path in separate steps, so a contender could unlink a live lock a peer had recreated after the contender's stale observation — two processes could hold the same key. Exceptional acquisition paths also leaked the in-process thread lock, and every claim error was reported as contention. Cross-process exclusion is now a kernel-mediated byte lock on a persistent per-key lock file (the C runtime _locking operation on Windows, fcntl.flock on POSIX); the OS releases it when the holder dies, so no sweep exists and lock files are never unlinked during acquisition or release. Cleanup is nested so the thread lock is released even when unlocking or closing fails, preserving the primary exception. Claim errors are classified: BlockingIOError/EACCES (POSIX) and EACCES/EDEADLK (Windows) are contention and map to the documented busy error; other errors keep their cause. The pid record is diagnostic-only: write failures no longer abort acquisition (previously they raised) — a deliberate behavior change.
…rage Subprocess contenders coordinated by marker-file barriers force the stale-observation overlap that previously let both processes hold the lock; fault injection covers os.open, os.write, unlock, and close failures; errno classification is pinned for both platforms; the in-process waiting floor is exercised with bounded event coordination. Fail-fast and transient-holder tests now use real subprocess holders — writing a live pid into the lock file no longer represents holding the lock. Verified non-vacuous against the original implementation and a restored faulty __exit__ ordering.
Resolve the four ruff findings in the test suite: unused redact import, unused os import, and two never-read task bindings (side-effect calls kept).
trollbot2012
force-pushed
the
fix/repository-lock-concurrency
branch
from
August 17, 2026 02:50
cd98dfe to
bf6e6e9
Compare
Ruff 0.16 expanded its default rule set substantially, so an unpinned install (the cap resolved 0.15.22) began failing on already-shipped code (RUF022, RUF100, BLE001, ...). Select the historical default rules (E4, E7, E9, F) explicitly so lint behavior is version-independent and re-expanding the rule set is a deliberate future decision rather than release-note roulette. Reverts the incidental __all__ sorting and drops the <0.16 version cap.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit bf6e6e9. Configure here.
| self._path.unlink(missing_ok=True) | ||
| continue | ||
| if time.monotonic() < deadline: | ||
| _claim_lock_file(candidate) |
There was a problem hiding this comment.
Lock scheme breaks mixed upgrades
Medium Severity
The new kernel byte lock still uses the same per-key .lock path as the old O_EXCL scheme. An upgraded process can open that file and take flock/msvcrt.locking while a pre-upgrade holder only owns the path via exclusive create and an open fd, so both can enter the critical section during a rolling upgrade.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit bf6e6e9. Configure here.
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.


Summary
_lockingoperation on Windows,fcntl.flockon POSIX. The OS releases the lock when the holder dies, so stale recovery is kernel-provided and lock files are never unlinked.Lint policy
Ruff 0.16 expanded its default rule set and began failing on already-shipped code (RUF022, RUF100, BLE001, …) — a zero-change PR from main fails the same way. This PR pins the historical default selection explicitly (
select = ["E4", "E7", "E9", "F"]) instead of capping the version, keeping lint behavior version-independent; re-expanding the rule set is left as a deliberate follow-up.Test plan
__exit__ordering.fcntl.flockpath.