From 03036af4b7b4d82d0bc4f6873cd021362ca7b2d3 Mon Sep 17 00:00:00 2001 From: dongxinwen Date: Wed, 25 Nov 2026 08:20:56 +0800 Subject: [PATCH] Initialize file lock markers after acquiring the OS lock Reserve the lock before writing its legacy marker so a contender on an empty Windows lock file gets the normal busy result. Exercise that interval in the existing cross-process device lease test. Agent-assisted-by: Codex / gpt-6.2-astra [effort=max] (implementation and verification) Agent-assisted-by: Codex / gpt-6.2-sol [effort=high] (lock compatibility investigation) Agent-assisted-by: Codex / gpt-6.2-sol [effort=high] (regression test investigation) Signed-off-by: dongxinwen --- src/inferrsi/apps/artifacts.py | 8 ++++---- src/inferrsi/harness/artifact_primitives.py | 8 ++++---- tests/harness/test_parallel_round.py | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 9 deletions(-) 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/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")