diff --git a/src/inferrsi/apps/artifacts.py b/src/inferrsi/apps/artifacts.py index 45abfd8aa..ac7153e56 100644 --- a/src/inferrsi/apps/artifacts.py +++ b/src/inferrsi/apps/artifacts.py @@ -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 @@ -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) diff --git a/src/inferrsi/harness/artifact_primitives.py b/src/inferrsi/harness/artifact_primitives.py index 49ba831c6..af0e74533 100644 --- a/src/inferrsi/harness/artifact_primitives.py +++ b/src/inferrsi/harness/artifact_primitives.py @@ -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 @@ -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) diff --git a/tests/test_codex_search_shell_forms.py b/tests/apps/test_codex_search_shell_forms.py similarity index 100% rename from tests/test_codex_search_shell_forms.py rename to tests/apps/test_codex_search_shell_forms.py diff --git a/tests/apps/test_project_service_reuse.py b/tests/apps/test_project_service_reuse.py index 079a63b44..e3839373d 100644 --- a/tests/apps/test_project_service_reuse.py +++ b/tests/apps/test_project_service_reuse.py @@ -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": ""} + 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", [ diff --git a/tests/harness/test_parallel_round.py b/tests/harness/test_parallel_round.py index f3ef60ae4..4ad4ab925 100644 --- a/tests/harness/test_parallel_round.py +++ b/tests/harness/test_parallel_round.py @@ -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 @@ -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") diff --git a/tests/test_dashboard_service_page.py b/tests/test_dashboard_service_page.py deleted file mode 100644 index 85f6bc06c..000000000 --- a/tests/test_dashboard_service_page.py +++ /dev/null @@ -1,11 +0,0 @@ -from inferrsi.apps.views.dashboard import _write_view_document, render_dashboard - - -def test_service_reuse_page_can_be_rewritten_by_the_dashboard_writer(tmp_path): - # write_dashboard renders this page and replaces only pages it recognizes as its own. - snapshot = {"data": {"evaluation_mode": "service_reuse"}, "chart": ""} - 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