A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
tools: machine_lock no longer starves a waiting exclusive request (#207) (#208)
Eyal Golan3a837bd6ed1d
diff --git a/tools/machine_lock.py b/tools/machine_lock.pyindex fc6d5022d..4f2598e64 100644--- a/tools/machine_lock.py+++ b/tools/machine_lock.py@@ -13,9 +13,10 @@ and no process (an interactive editor included) holding any worktree's unity/Temuser. Exit code: the command's, or 3 when --wait-unity times out.Unity processes are recognised by their executable (`ps -o comm=`), never by argument text, so queued wrappers that-name Unity in their command line do not count (review C1). macOS flock does not queue fairly: new shared holders may-enter while an exclusive request waits, so an exclusive profile can wait behind a stream of shared jobs; the controller-dispatches one profiling step at a time (review M9)."""+name Unity in their command line do not count (review C1). macOS flock does not queue fairly, so every request first+passes a gate (the same path plus ".gate"): it takes the gate exclusively, then the machine lock, then releases the gate.+A waiting exclusive request holds the gate until it has the lock, so no new shared holder gets in ahead of it and the+running ones drain (issue #207; before, a stream of shared jobs starved a profile, review M9)."""import argparseimport fcntlimport os@@ -25,6 +26,7 @@ import timefrom pathlib import PathLOCK = Path(os.environ.get("STATECRAFT_MACHINE_LOCK", str(Path.home() / ".cache" / "statecraft" / "machine.lock")))+GATE = LOCK.with_name(LOCK.name + ".gate")UNITY_EXE = "/Unity.app/Contents/MacOS/Unity"@@ -102,9 +104,11 @@ def wait_for_unity(timeout: float, pids=unity_pids, sleep=time.sleep, now=time.mdef main(argv: list[str] | None = None) -> int:a = parse(sys.argv[1:] if argv is None else argv)LOCK.parent.mkdir(parents=True, exist_ok=True)- with open(LOCK, "a+") as f:+ with open(GATE, "a+") as gate, open(LOCK, "a+") as f:print(f"machine_lock: taking the machine lock ({'exclusive' if a.exclusive else 'shared'})", flush=True)+ fcntl.flock(gate, fcntl.LOCK_EX)fcntl.flock(f, fcntl.LOCK_EX if a.exclusive else fcntl.LOCK_SH)+ fcntl.flock(gate, fcntl.LOCK_UN)if a.wait_unity and not wait_for_unity(a.timeout):print("machine_lock: timed out waiting for Unity")return 3diff --git a/tools/tests/test_machine_lock.py b/tools/tests/test_machine_lock.pyindex 58414ac5f..7c672b385 100644--- a/tools/tests/test_machine_lock.py+++ b/tools/tests/test_machine_lock.py@@ -88,3 +88,32 @@ def test_only_the_unity_executable_counts_never_a_wrapper_naming_it():])assert ml.batch_unity_pids(comm, args) == [101]assert ml.batch_unity_pids(comm, args, exclude={101}) == []+++def test_a_waiting_exclusive_request_runs_before_a_later_shared_one(tmp_path):+ # Issue #207: while a shared job runs and an exclusive request waits, a newer shared request must not get in first.+ env = {**os.environ, "STATECRAFT_MACHINE_LOCK": str(tmp_path / "machine.lock")}+ stamp = [sys.executable, "-c", "import time; print(time.time(), flush=True); time.sleep(0.3)"]++ def start(mode, command):+ return subprocess.Popen([sys.executable, ml.__file__, mode, "--", *command], env=env, stdout=subprocess.PIPE,+ text=True)++ holder = start("--shared", [sys.executable, "-c", "import time; time.sleep(2)"])+ procs = [holder]+ try:+ assert "taking the machine lock (shared)" in holder.stdout.readline()+ exclusive = start("--exclusive", stamp)+ procs.append(exclusive)+ assert "taking the machine lock (exclusive)" in exclusive.stdout.readline()+ time.sleep(0.5) # the exclusive request is now waiting behind the holder+ later = start("--shared", stamp)+ procs.append(later)+ assert "taking the machine lock (shared)" in later.stdout.readline()+ ran_exclusive, ran_later = float(exclusive.stdout.readline()), float(later.stdout.readline())+ assert ran_exclusive < ran_later, "a later shared request got the lock before a waiting exclusive one"+ finally:+ for p in procs:+ p.terminate()+ p.wait(10)+ p.stdout.close()