Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: add Windows file locking to SessionIndex - #78

Open
iceluna3416 wants to merge 1 commit into
HKUDS:mainfrom
iceluna3416:fix/windows-session-lock
Open

fix: add Windows file locking to SessionIndex#78
iceluna3416 wants to merge 1 commit into
HKUDS:mainfrom
iceluna3416:fix/windows-session-lock

Conversation

@iceluna3416

Copy link
Copy Markdown

Summary

  • SessionIndex._locked() only implemented file locking via POSIX fcntl, which is None on Windows. This silently disabled the locking that the docstring on _synchronized promises, so concurrent session writes on Windows can drop each other's data and save()'s os.replace() can raise PermissionError.
  • Adds an msvcrt.locking() fallback for Windows, matching the existing fcntl.flock behavior on POSIX.
  • The lock file's required byte is written once in __init__ (before any concurrent access exists) rather than on each lock acquisition, because Windows enforces msvcrt locks as mandatory — a bare read() on the lock file while another thread holds the lock would itself raise PermissionError.

Test plan

  • tests/test_robustness.py::TestSessionIndexDurability::test_concurrent_creates_do_not_lose_sessions — previously lost roughly half of 80 concurrently created sessions on Windows; now passes consistently across repeated runs.
  • Full pytest tests/ suite on Windows: 182/184 passing (remaining 2 failures are pre-existing, unrelated test-portability issues — a hardcoded /tmp path and a test reading a file without specifying UTF-8 encoding — not touched by this change).

SessionIndex._locked() only implemented file locking via POSIX fcntl,
which is unavailable on Windows (fcntl is None there). This left the
documented read-modify-write guarantee completely unenforced on
Windows: concurrent session writes silently drop each other's data,
and concurrent os.replace() calls in save() can raise PermissionError.

Add an msvcrt.locking() fallback for Windows. The lock file's single
required byte is written once in __init__, before any concurrent
access exists, because Windows enforces this lock as mandatory: a
bare read() on the lock file while another thread holds it would
itself raise PermissionError.

Verified with tests/test_robustness.py::TestSessionIndexDurability
(previously lost half of 80 concurrently created sessions; now passes
consistently across repeated runs) and the full test suite on Windows.
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