Skip to content

feat(_internal): Add typed, asyncio-friendly file locking module - #501

Merged
tony merged 4 commits into
masterfrom
pr/file-lock
Nov 29, 2026
Merged

tony merged 4 commits into
masterfrom
pr/file-lock

Conversation

@tony

@tony tony commented Dec 30, 2025

Copy link
Copy Markdown
Member

Summary

  • Add FileLock class for cross-platform file locking with context manager support
  • Add atomic_init function for race-free initialization (useful with pytest-xdist)
  • Support both sync and async usage patterns

Test plan

  • Unit tests for FileLock (acquire, release, context manager, timeout)
  • Tests for atomic_init with concurrent access simulation
  • Cross-platform compatibility (uses fcntl on Unix)

tony added a commit that referenced this pull request Dec 30, 2025
tony added a commit that referenced this pull request Dec 30, 2025
tony added a commit that referenced this pull request Dec 30, 2025
@codecov

codecov Bot commented Dec 30, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.51701% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.47%. Comparing base (3555330) to head (c6e35f4).

Files with missing lines Patch % Lines
src/libvcs/_internal/file_lock.py 84.46% 20 Missing and 12 partials ⚠️
tests/_internal/test_file_lock.py 99.57% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #501      +/-   ##
==========================================
+ Coverage   72.75%   73.47%   +0.72%     
==========================================
  Files          52       54       +2     
  Lines       11567    12008     +441     
  Branches     1709     1741      +32     
==========================================
+ Hits         8415     8823     +408     
- Misses       2336     2356      +20     
- Partials      816      829      +13     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tony

tony commented Aug 22, 2026

Copy link
Copy Markdown
Member Author

Automated review finding. Recorded here so it is not lost. Nothing has been pushed to this branch and no fix is applied — this is a note, not a change request.

Stale-lock removal is time-based only, never checks whether the holder is alive

src/libvcs/_internal/file_lock.py treats a lock as stale purely on mtime age against stale_timeout, and unlinks it. The PID recorded in the lock is never consulted.

Failure scenario: process A legitimately holds the lock for longer than stale_timeout doing real work. Process B sees an old mtime, deletes the lock, and enters the critical section while A is still inside it. Both run the guarded region concurrently — which is the one thing the lock exists to prevent, and it happens specifically under slow-but-healthy conditions rather than crashes.

Suggested fix: before removing, confirm the recorded PID is actually gone:

try:
    os.kill(pid, 0)
except ProcessLookupError:
    pass  # holder is gone; safe to reclaim
else:
    return  # holder is alive; not stale

Note: #502 vendors this file verbatim and carries the same bug.

tony added 4 commits November 28, 2026 20:51
…module

why: Enable safe concurrent access to shared resources across multiple
processes, particularly for pytest-xdist parallel test execution.

what:
- Add FileLock class with context manager support
- Add atomic_init() for one-time initialization with locking
- Support both sync and async usage patterns
- Use fcntl for Unix file locking
what:
- Add FileLock context manager tests
- Add atomic_init race condition tests
- Add timeout and error handling tests
- Add multi-process locking tests
what:
- Add docs/internals/file_lock.md with API reference
- Add file_lock to internals toctree
@tony
tony merged commit 3114eb6 into master Nov 29, 2026
9 checks passed
@tony
tony deleted the pr/file-lock branch November 29, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant