fix: add Windows file locking to SessionIndex - #78
Open
iceluna3416 wants to merge 1 commit into
Open
Conversation
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.
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
SessionIndex._locked()only implemented file locking via POSIXfcntl, which isNoneon Windows. This silently disabled the locking that the docstring on_synchronizedpromises, so concurrent session writes on Windows can drop each other's data andsave()'sos.replace()can raisePermissionError.msvcrt.locking()fallback for Windows, matching the existingfcntl.flockbehavior on POSIX.__init__(before any concurrent access exists) rather than on each lock acquisition, because Windows enforcesmsvcrtlocks as mandatory — a bareread()on the lock file while another thread holds the lock would itself raisePermissionError.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.pytest tests/suite on Windows: 182/184 passing (remaining 2 failures are pre-existing, unrelated test-portability issues — a hardcoded/tmppath and a test reading a file without specifying UTF-8 encoding — not touched by this change).