Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/inferrsi/apps/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
9 changes: 8 additions & 1 deletion src/inferrsi/harness/artifact_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions tests/session_lock_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
48 changes: 48 additions & 0 deletions tests/test_artifact_helper_copies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions tests/test_session_lock_across_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading