From e64840d1a59a511b24ec20ef3f0f207e738e5f65 Mon Sep 17 00:00:00 2001 From: RRFZ <247457381+Plastic-time@users.noreply.github.com> Date: Sat, 31 Oct 2026 21:29:49 -0400 Subject: [PATCH] fix(learning_journal): wait for the Windows journal lock instead of failing The journal's cross-process lock read byte 0 of the lock file before taking msvcrt.LK_LOCK, to see whether the file still needed its one byte. Windows byte-range locks are mandatory, so while another process held that byte the read failed with PermissionError and the second writer crashed instead of waiting for the lock. Size the file with seek/tell, as the grants, cron and Codex-auth locks already do, then lock. --- deeptutor/services/learning_journal/store.py | 6 ++- tests/services/learning_journal/test_store.py | 48 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/deeptutor/services/learning_journal/store.py b/deeptutor/services/learning_journal/store.py index 35b75b606..cb57ddc67 100644 --- a/deeptutor/services/learning_journal/store.py +++ b/deeptutor/services/learning_journal/store.py @@ -329,7 +329,11 @@ class LearningJournalStore: if sys.platform == "win32": import msvcrt - if not handle.read(1): + # Size the file instead of reading it: Windows locks are + # mandatory, so reading byte 0 while another process holds it + # fails with PermissionError instead of letting LK_LOCK wait. + handle.seek(0, 2) + if handle.tell() == 0: handle.write(b"\0") handle.flush() handle.seek(0) diff --git a/tests/services/learning_journal/test_store.py b/tests/services/learning_journal/test_store.py index 18306cc5f..f95a40411 100644 --- a/tests/services/learning_journal/test_store.py +++ b/tests/services/learning_journal/test_store.py @@ -3,6 +3,8 @@ from __future__ import annotations from pathlib import Path +import sys +from types import SimpleNamespace import pytest @@ -161,3 +163,49 @@ def test_two_workers_do_not_drop_confirmed_records(journal_store): records = journal_store.load().records assert len(records) == 20 assert len({record.id for record in records}) == 20 + + +class _LockHeldElsewhere: + """A lock file whose byte 0 another process holds. + + Windows byte-range locks are mandatory: reading a locked byte fails with + PermissionError, while ``msvcrt.locking(..., LK_LOCK, 1)`` waits for it. + """ + + def __init__(self, handle: object) -> None: + self._handle = handle + + def __enter__(self) -> _LockHeldElsewhere: + return self + + def __exit__(self, *_exc: object) -> None: + self._handle.close() + + def read(self, *_args: object) -> bytes: + raise PermissionError(13, "Permission denied") + + def __getattr__(self, name: str) -> object: + return getattr(self._handle, name) + + +def test_windows_lock_waits_for_a_held_lock_instead_of_reading_it(journal_store, monkeypatch): + import deeptutor.services.learning_journal.store as module + + calls: list[int] = [] + fake_msvcrt = SimpleNamespace( + LK_LOCK=1, LK_UNLCK=0, locking=lambda _fileno, mode, _length: calls.append(mode) + ) + monkeypatch.setattr(module, "sys", SimpleNamespace(platform="win32")) + monkeypatch.setitem(sys.modules, "msvcrt", fake_msvcrt) + real_open = Path.open + + def open_lock_held_elsewhere(self: Path, *args: object, **kwargs: object) -> object: + handle = real_open(self, *args, **kwargs) + return _LockHeldElsewhere(handle) if self.suffix == ".lock" else handle + + monkeypatch.setattr(Path, "open", open_lock_held_elsewhere) + + journal_store.add_record(title="Held", insight="written once the lock is free") + + assert calls == [fake_msvcrt.LK_LOCK, fake_msvcrt.LK_UNLCK] + assert [record.title for record in journal_store.load().records] == ["Held"]