fix: DocStore.lock() is a no-op mutex on Windows - #456
Open
Manish2102 wants to merge 1 commit into
Open
Conversation
lock() only implemented the critical section with fcntl.flock, which does not exist on Windows; the fallback there was just `yield` with no locking at all. This means the check-then-write sequence in submit_document (unique-name assignment before save) has no real mutual exclusion on Windows, so two concurrent submits of the same filename can both save as the same name, silently shadowing one document with another (test_concurrent_same_name_submits_store_unique_names reproduces this on main). Add a real Windows lock using msvcrt.locking on a 1-byte region of the lock file. msvcrt requires an existing byte to lock, so the byte is seeded once (a racing seed is harmless: same byte, and neither side holds the lock yet at that point). Adds tests/test_local_store.py with a direct mutex test (fails against the old no-op fallback, confirmed locally) and a repeat-call sanity test. Confirmed test_concurrent_same_name_submits_store_unique_names now passes on Windows; full suite otherwise unaffected. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes a correctness issue in local-mode document submission on Windows by making DocStore.lock() a real cross-process mutex (instead of a no-op fallback), preventing concurrent submit operations from assigning and saving the same “unique” filename.
Changes:
- Implement a Windows locking path for
DocStore.lock()usingmsvcrt.locking()on a 1-byte region of a lock file. - Ensure the store root/lock file path is initialized before locking and unify lock-path usage across platforms.
- Add tests validating that
DocStore.lock()serializes concurrent critical sections and remains safe across repeated calls.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
pageindex/local_store.py |
Adds a Windows-specific lock implementation to make DocStore.lock() a real mutex when fcntl is unavailable. |
tests/test_local_store.py |
Introduces new unit tests to validate lock mutual exclusion and repeat-call behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| except ImportError: | ||
| yield | ||
| import msvcrt | ||
| fd = os.open(str(lock_path), os.O_CREAT | os.O_RDWR) |
Comment on lines
+12
to
+35
| order = [] | ||
| lock_obj = threading.Lock() | ||
|
|
||
| def worker(name): | ||
| with store.lock(): | ||
| # If DocStore.lock() is not a real mutex, both workers can be | ||
| # inside this block at once and their appends interleave. | ||
| with lock_obj: | ||
| order.append(f"{name}-enter") | ||
| time.sleep(0.2) | ||
| with lock_obj: | ||
| order.append(f"{name}-exit") | ||
|
|
||
| threads = [threading.Thread(target=worker, args=(n,)) for n in ("A", "B")] | ||
| for t in threads: | ||
| t.start() | ||
| for t in threads: | ||
| t.join() | ||
|
|
||
| # Whichever thread goes first, its enter/exit pair must be contiguous — | ||
| # a real mutex never lets the other thread's enter land in between. | ||
| assert order[0][-5:] == "enter" | ||
| assert order[1][-4:] == "exit" | ||
| assert order[0][0] == order[1][0] |
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.
lock() only implemented the critical section with fcntl.flock, which does not exist on Windows; the fallback there was just
yieldwith no locking at all. This means the check-then-write sequence in submit_document (unique-name assignment before save) has no real mutual exclusion on Windows, so two concurrent submits of the same filename can both save as the same name, silently shadowing one document with another(test_concurrent_same_name_submits_store_unique_names reproduces this on main).
Add a real Windows lock using msvcrt.locking on a 1-byte region of the lock file. msvcrt requires an existing byte to lock, so the byte is seeded once (a racing seed is harmless: same byte, and neither side holds the lock yet at that point).
Adds tests/test_local_store.py with a direct mutex test (fails against the old no-op fallback, confirmed locally) and a repeat-call sanity test. Confirmed test_concurrent_same_name_submits_store_unique_names now passes on Windows; full suite otherwise unaffected.