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
8 changes: 4 additions & 4 deletions src/inferrsi/apps/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
@contextmanager
def file_lock(path: Path):
"""Hold a nonblocking OS file lock, released even if the process crashes."""
with path.open("a+b") as stream:
if os.fstat(stream.fileno()).st_size == 0:
stream.write(b"0")
stream.flush()
with path.open("a+b", buffering=0) as stream:
stream.seek(0)
if os.name == "nt":
import msvcrt
Expand All @@ -40,6 +37,9 @@ def file_lock(path: Path):

fcntl.flock(stream.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
try:
# Windows can lock past EOF. Only the owner may initialize byte zero.
if os.fstat(stream.fileno()).st_size == 0:
stream.write(b"0")
yield
finally:
stream.seek(0)
Expand Down
8 changes: 4 additions & 4 deletions src/inferrsi/harness/artifact_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
@contextmanager
def file_lock(path: Path):
"""Hold a nonblocking OS file lock, released even if the process crashes."""
with path.open("a+b") as stream:
if os.fstat(stream.fileno()).st_size == 0:
stream.write(b"0")
stream.flush()
with path.open("a+b", buffering=0) as stream:
stream.seek(0)
if os.name == "nt":
import msvcrt
Expand All @@ -34,6 +31,9 @@ def file_lock(path: Path):

fcntl.flock(stream.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
try:
# Windows can lock past EOF. Only the owner may initialize byte zero.
if os.fstat(stream.fileno()).st_size == 0:
stream.write(b"0")
yield
finally:
stream.seek(0)
Expand Down
12 changes: 12 additions & 0 deletions tests/apps/test_project_service_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ def test_knowledge_and_dashboard_never_enter_legacy_fingerprint_readers(campaign
assert write_dashboard(campaign["root"]) == output


def test_service_reuse_page_can_be_rewritten_by_the_dashboard_writer(tmp_path):
from inferrsi.apps.views.dashboard import _write_view_document, render_dashboard

# write_dashboard renders this page and replaces only pages it recognizes as its own.
snapshot = {"data": {"evaluation_mode": "service_reuse"}, "chart": "<svg></svg>"}
page = render_dashboard(snapshot)
output = tmp_path / "dashboard.html"
assert _write_view_document(output, page, generator="InferRSI dashboard") == output
assert _write_view_document(output, page, generator="InferRSI dashboard") == output
assert output.read_text(encoding="utf-8") == page


@pytest.mark.parametrize(
"changes,expected",
[
Expand Down
15 changes: 14 additions & 1 deletion tests/harness/test_parallel_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_ticket_status_reads_queued_and_finished_results(self, tmp_path):

def test_two_acquires_do_not_share_one_device(self, tmp_path):
lease = DeviceLease(tmp_path)
(tmp_path / "cpu-0").touch() # An empty lock file can remain from an older release.
lock_path = tmp_path / "cpu-0"
worker = """
import sys
from pathlib import Path
Expand All @@ -184,6 +184,19 @@ def probe():
assert completed.returncode == 0, completed.stderr
return completed.stdout.strip()

# A first owner can hold the OS lock while the legacy lock file is still empty.
with lock_path.open("w+b") as holder:
if os.name == "nt":
import msvcrt

msvcrt.locking(holder.fileno(), msvcrt.LK_NBLCK, 1)
else:
import fcntl

fcntl.flock(holder.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
assert probe() == "device busy: cpu-0"
assert lock_path.stat().st_size == 0

with lease.acquire("cpu-0"):
with pytest.raises(LeaseError, match="device busy: cpu-0"):
lease.acquire("cpu-0")
Expand Down
11 changes: 0 additions & 11 deletions tests/test_dashboard_service_page.py

This file was deleted.

Loading