Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

fix(visual): machine_lock selects Unity by executable, not argument text (review C1)

eyalgolan2a69c6152e33
diff --git a/tools/machine_lock.py b/tools/machine_lock.py
index 32920778e..fc6d5022d 100644
--- a/tools/machine_lock.py
+++ b/tools/machine_lock.py
@@ -10,7 +10,12 @@ Usage: uv run tools/machine_lock.py (--shared | --exclusive) [--wait-unity] [--t
profiles and boot budgets) and, with --wait-unity, then waits until no Unity is left running: no batch-mode Unity process,
and no process (an interactive editor included) holding any worktree's unity/Temp/UnityLockfile. The lock file is
~/.cache/statecraft/machine.lock (fcntl.flock; STATECRAFT_MACHINE_LOCK overrides it), shared by every worktree of this
-user. Exit code: the command's, or 3 when --wait-unity times out."""
+user. 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)."""
import argparse
import fcntl
import os
@@ -20,7 +25,7 @@ import time
from pathlib import Path
LOCK = Path(os.environ.get("STATECRAFT_MACHINE_LOCK", str(Path.home() / ".cache" / "statecraft" / "machine.lock")))
-UNITY_BATCH = "Unity.app/Contents/MacOS/Unity .*-batchmode"
+UNITY_EXE = "/Unity.app/Contents/MacOS/Unity"
def parse(argv: list[str]) -> argparse.Namespace:
@@ -39,9 +44,28 @@ def parse(argv: list[str]) -> argparse.Namespace:
return a
+def _rows(ps_text: str) -> dict[int, str]:
+ rows = {}
+ for line in ps_text.splitlines():
+ pid, _, rest = line.strip().partition(" ")
+ if pid.isdigit():
+ rows[int(pid)] = rest.strip()
+ return rows
+
+
+def batch_unity_pids(comm_text: str, args_text: str, exclude: set[int] | frozenset[int] = frozenset()) -> list[int]:
+ """Pids whose executable (`ps -o comm=`, the binary's path) is Unity.app's Unity and whose arguments include
+ -batchmode. Selected by executable, never by argument text: a queued `machine_lock.py --shared -- .../Unity
+ -batchmode` wrapper and its `uv run` parent name Unity in their arguments and must not count (review C1)."""
+ args = _rows(args_text)
+ return sorted(pid for pid, exe in _rows(comm_text).items()
+ if exe.endswith(UNITY_EXE) and pid not in exclude and " -batchmode" in f" {args.get(pid, '')} ")
+
+
def unity_batch_pids() -> list[int]:
- r = subprocess.run(["pgrep", "-f", UNITY_BATCH], capture_output=True, text=True)
- return [int(p) for p in r.stdout.split() if p.isdigit() and int(p) != os.getpid()]
+ def ps(field: str) -> str:
+ return subprocess.run(["ps", "-axo", f"pid=,{field}="], capture_output=True, text=True).stdout
+ return batch_unity_pids(ps("comm"), ps("args"), exclude={os.getpid()})
def worktrees() -> list[Path]:
diff --git a/tools/tests/test_machine_lock.py b/tools/tests/test_machine_lock.py
index 480bd6cc8..e8b1022cf 100644
--- a/tools/tests/test_machine_lock.py
+++ b/tools/tests/test_machine_lock.py
@@ -63,3 +63,27 @@ def test_an_editor_holding_a_worktree_lockfile_counts_as_unity(tmp_path):
assert pids == [4242]
assert asked == [str(temp / "UnityLockfile")]
assert ml.lockfile_holders([tmp_path / "b"], lsof=lambda files: "1\n") == []
+
+
+UNITY_EXE = "/Applications/Unity/Hub/Editor/6000.3.25f1/Unity.app/Contents/MacOS/Unity"
+
+
+def test_only_the_unity_executable_counts_never_a_wrapper_naming_it():
+ """Review C1: a queued `machine_lock.py --shared -- .../Unity -batchmode` wrapper and its `uv run` parent name Unity in
+ their arguments; selecting by argument text made an exclusive profile wait on them (a deadlock)."""
+ comm = "\n".join([
+ f" 101 {UNITY_EXE}",
+ " 102 /Users/me/.local/share/uv/python/cpython-3.13/bin/python3.13",
+ " 103 /opt/homebrew/bin/uv",
+ f" 104 {UNITY_EXE}",
+ " 105 /Applications/Unity Hub.app/Contents/MacOS/Unity Hub",
+ ])
+ args = "\n".join([
+ f" 101 {UNITY_EXE} -batchmode -projectPath unity -runTests",
+ f" 102 python3 tools/machine_lock.py --shared -- {UNITY_EXE} -batchmode -projectPath unity",
+ f" 103 uv run tools/machine_lock.py --shared -- {UNITY_EXE} -batchmode -projectPath unity",
+ f" 104 {UNITY_EXE} -projectPath unity",
+ " 105 /Applications/Unity Hub.app/Contents/MacOS/Unity Hub -batchmode",
+ ])
+ assert ml.batch_unity_pids(comm, args) == [101]
+ assert ml.batch_unity_pids(comm, args, exclude={101}) == []