Skip to content

fix: DocStore.lock() is a no-op mutex on Windows - #456

Open
Manish2102 wants to merge 1 commit into
VectifyAI:mainfrom
Manish2102:fix/windows-store-lock
Open

fix: DocStore.lock() is a no-op mutex on Windows#456
Manish2102 wants to merge 1 commit into
VectifyAI:mainfrom
Manish2102:fix/windows-store-lock

Conversation

@Manish2102

Copy link
Copy Markdown

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.

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>
Copilot AI lite review requested due to automatic review settings September 1, 2026 11:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() using msvcrt.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.

Comment thread pageindex/local_store.py
except ImportError:
yield
import msvcrt
fd = os.open(str(lock_path), os.O_CREAT | os.O_RDWR)
Comment thread tests/test_local_store.py
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants