From cc6b0ff0cbec003aecafdfd28417f77be31aade2 Mon Sep 17 00:00:00 2001 From: Inference_ <68734681+Inference1@users.noreply.github.com> Date: Sat, 21 Nov 2026 16:22:31 +0800 Subject: [PATCH] fix(apps): report Windows session lock contention as retryable Normalize the Windows nonblocking acquire error in both session lock helpers. Keep the retryable export error consistent across platforms and cover both lock directions. Agent-assisted-by: Codex (author) Signed-off-by: Inference1 <3617237115@qq.com> --- src/inferrsi/apps/artifacts.py | 9 +++- src/inferrsi/harness/artifact_primitives.py | 9 +++- tests/session_lock_probe.py | 3 +- tests/test_artifact_helper_copies.py | 48 +++++++++++++++++++++ tests/test_session_lock_across_processes.py | 5 +-- 5 files changed, 67 insertions(+), 7 deletions(-) diff --git a/src/inferrsi/apps/artifacts.py b/src/inferrsi/apps/artifacts.py index 5690afdd..45abfd8a 100644 --- a/src/inferrsi/apps/artifacts.py +++ b/src/inferrsi/apps/artifacts.py @@ -2,6 +2,7 @@ # those in the other cores' copies; tests/test_artifact_helper_copies.py keeps them equal. # Apps imports no other inferrsi module here. +import errno import hashlib import json import os @@ -27,7 +28,13 @@ def file_lock(path: Path): if os.name == "nt": import msvcrt - msvcrt.locking(stream.fileno(), msvcrt.LK_NBLCK, 1) + try: + msvcrt.locking(stream.fileno(), msvcrt.LK_NBLCK, 1) + except PermissionError as error: + if error.errno != errno.EACCES: + raise + # LK_NBLCK reports a lock violation as EACCES. + raise BlockingIOError(errno.EAGAIN, "File lock is held", path) from error else: import fcntl diff --git a/src/inferrsi/harness/artifact_primitives.py b/src/inferrsi/harness/artifact_primitives.py index ef7c2698..49ba831c 100644 --- a/src/inferrsi/harness/artifact_primitives.py +++ b/src/inferrsi/harness/artifact_primitives.py @@ -3,6 +3,7 @@ # locks exclude each other on the same path; Harness does not import that module here. # tests/test_artifact_helper_copies.py keeps the copies equal. +import errno import hashlib import json import os @@ -21,7 +22,13 @@ def file_lock(path: Path): if os.name == "nt": import msvcrt - msvcrt.locking(stream.fileno(), msvcrt.LK_NBLCK, 1) + try: + msvcrt.locking(stream.fileno(), msvcrt.LK_NBLCK, 1) + except PermissionError as error: + if error.errno != errno.EACCES: + raise + # LK_NBLCK reports a lock violation as EACCES. + raise BlockingIOError(errno.EAGAIN, "File lock is held", path) from error else: import fcntl diff --git a/tests/session_lock_probe.py b/tests/session_lock_probe.py index d7279ad3..d8175213 100644 --- a/tests/session_lock_probe.py +++ b/tests/session_lock_probe.py @@ -109,9 +109,8 @@ def main(work: Path) -> dict: assert holder.poll() is None, holder.communicate() assert time.monotonic() < deadline, "the Harness holder never took the lock" time.sleep(0.02) - # Windows reports contention as PermissionError (#494); POSIX raises BlockingIOError, - # which Apps maps to SnapshotBusy. Either way the export must not publish. busy = delivery.export_bundle(root, work / "busy", export_id="busy") + assert "writer is active" in busy.get("error", ""), busy busy_published = (work / "busy").exists() finally: release.write_text("done", encoding="utf-8") diff --git a/tests/test_artifact_helper_copies.py b/tests/test_artifact_helper_copies.py index f04a37f6..c8841f06 100644 --- a/tests/test_artifact_helper_copies.py +++ b/tests/test_artifact_helper_copies.py @@ -4,9 +4,15 @@ imports another for them. This test fails when one copy drifts from the others. """ +import errno import importlib import inspect import json +import os +import sys +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import Mock import pytest @@ -75,3 +81,45 @@ def test_every_digest_copy_reads_the_same_bytes(tmp_path): for module in COPIES["sha256"]: with pytest.raises(FileNotFoundError): importlib.import_module(module).sha256(tmp_path / "missing.bin") + + +@pytest.mark.parametrize("module_name", COPIES["file_lock"]) +@pytest.mark.parametrize("code", [errno.EACCES, errno.EPERM, errno.EBADF]) +def test_windows_only_translates_locking_contention(tmp_path, monkeypatch, module_name, code): + module = importlib.import_module(module_name) + failure = OSError(code, os.strerror(code)) + locking = Mock(side_effect=[failure, None, None]) + monkeypatch.setitem(sys.modules, "msvcrt", SimpleNamespace( + locking=locking, LK_NBLCK=2, LK_UNLCK=0, + )) + monkeypatch.setattr(module, "os", SimpleNamespace(name="nt", fstat=os.fstat)) + + path = tmp_path / ".session.lock" + expected = BlockingIOError if code == errno.EACCES else type(failure) + with pytest.raises(expected) as raised, module.file_lock(path): + pytest.fail("acquired a lock after the OS refused it") + assert locking.call_count == 1 + if code == errno.EACCES: + assert raised.value.errno == errno.EAGAIN + assert raised.value.__cause__ is failure + else: + assert raised.value is failure + + with module.file_lock(path): + assert locking.call_count == 2 + assert locking.call_count == 3 + assert [call.args[1:] for call in locking.call_args_list] == [(2, 1), (2, 1), (0, 1)] + + +@pytest.mark.parametrize("module_name", COPIES["file_lock"]) +def test_lock_preserves_errors_opening_the_file(tmp_path, module_name): + acquire = importlib.import_module(module_name).file_lock + with pytest.raises(FileNotFoundError), acquire(tmp_path / "missing" / ".session.lock"): + pytest.fail("opened a lock in a missing directory") + + path = Mock(spec=Path) + denied = PermissionError(errno.EACCES, "cannot open lock file") + path.open.side_effect = denied + with pytest.raises(PermissionError) as raised, acquire(path): + pytest.fail("opened a lock without permission") + assert raised.value is denied diff --git a/tests/test_session_lock_across_processes.py b/tests/test_session_lock_across_processes.py index f1df5101..28e2554f 100644 --- a/tests/test_session_lock_across_processes.py +++ b/tests/test_session_lock_across_processes.py @@ -9,8 +9,7 @@ ``tests/session_lock_probe.py`` drives the real pair with a valid managed session: a Harness process holds the lock and the Apps ``export_bundle`` of that session is refused, then the same export publishes after release. The probe runs here against the checkout and in -``tests/check_installed.py`` against the installed wheel. The exception type that Windows -raises for contention is #494 and stays a separate assertion in tests/test_result_delivery.py. +``tests/check_installed.py`` against the installed wheel. """ import json @@ -107,7 +106,7 @@ def test_a_held_lock_excludes_the_other_copy_in_another_process(tmp_path, hold, lock = tmp_path / ".session.lock" release = hold(holder, lock) blocked = _probe(contender, lock) - assert blocked["acquired"] is False, blocked + assert blocked == {"acquired": False, "error": "BlockingIOError"} release.write_text("done", encoding="utf-8") deadline = time.monotonic() + 30 while True: