physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / tests / test_gb10_lock.sh
| 1 | #!/bin/bash |
| 2 | # tests/test_gb10_lock.sh -- host-only tests (no GPU) for tests/gb10_lock.sh, on a scratch lock file. |
| 3 | # |
| 4 | # red a fresh descriptor under a parent that holds the lock is refused (the 2026-10-18 judge deadlock) |
| 5 | # green gb10_lock_acquire under that parent returns at once and says so |
| 6 | # wait a holder that is not an ancestor is waited for, then the lock is ours |
| 7 | # own with no holder the lock is taken through fd 9 and gb10_lock_release drops it |
| 8 | # new a missing lock file is created |
| 9 | # marker AIEN_GB10_LOCK_HELD=1 from the runner skips the lock without opening the file; other values are ignored |
| 10 | # A broken helper would hang, so every case that could block runs bounded in its own process group (scratch file, no |
| 11 | # chip work). Exit 0 only when every case passes. Shell + coreutils + util-linux flock. No Python. |
| 12 | set -u |
| 13 | HERE=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
| 14 | LIB=$HERE/gb10_lock.sh |
| 15 | work=$(mktemp -d) |
| 16 | trap 'rm -rf "$work"' EXIT |
| 17 | L=$work/gb10.lock |
| 18 | : > "$L" |
| 19 | pass=0 fail=0 |
| 20 | check() { if [ "$1" = 0 ]; then pass=$((pass + 1)); echo "[PASS] $2"; else fail=$((fail + 1)); echo "[FAIL] $2"; fi; } |
| 21 | # held_elsewhere FILE: 0 when another process holds FILE (a non-blocking try from a fresh process fails) |
| 22 | held_elsewhere() { ! flock -n "$1" true; } |
| 23 | # run_bounded TENTHS OUT CMD...: CMD in its own process group (setsid), output to OUT; past TENTHS tenths of a |
| 24 | # second the whole group is killed and the status is 124 (a broken helper leaves a child waiting on a lock it |
| 25 | # inherited, so killing only the top process would not end the case) |
| 26 | run_bounded() { |
| 27 | local t=$1 o=$2 p i=0; shift 2 |
| 28 | setsid "$@" > "$o" 2>&1 & |
| 29 | p=$! |
| 30 | while kill -0 "$p" 2>/dev/null; do |
| 31 | if [ "$i" -ge "$t" ]; then kill -- -"$p" 2>/dev/null; wait "$p" 2>/dev/null; return 124; fi |
| 32 | sleep 0.1; i=$((i + 1)) |
| 33 | done |
| 34 | wait "$p" |
| 35 | } |
| 36 | |
| 37 | # red: the old pattern (open the file again, flock it) under a holding parent cannot get the lock |
| 38 | flock -x "$L" bash -c 'exec 9< "$1"; flock -w 2 -x 9' _ "$L" |
| 39 | check $([ $? -ne 0 ] && echo 0 || echo 1) "red: a second descriptor under the holding parent is refused (the deadlock)" |
| 40 | |
| 41 | # green: the helper under the same parent |
| 42 | out=$work/green.out |
| 43 | run_bounded 50 "$out" flock -x "$L" bash -c '. "$1"; GB10_LOCK=$2; gb10_lock_acquire && echo ACQUIRED' _ "$LIB" "$L"; rc=$? |
| 44 | check $([ $rc = 0 ] && grep -q 'held by a parent process' "$out" && grep -q ACQUIRED "$out" && echo 0 || echo 1) "green: under a holding parent gb10_lock_acquire returns at once (rc $rc)" |
| 45 | |
| 46 | # a deeper ancestor counts too (holder -> bash -> bash -> helper) |
| 47 | run_bounded 50 "$out" flock -x "$L" bash -c 'bash -c ". \"\$1\"; GB10_LOCK=\$2; gb10_lock_acquire" _ "$1" "$2"' _ "$LIB" "$L"; rc=$? |
| 48 | check $([ $rc = 0 ] && grep -q 'held by a parent process' "$out" && echo 0 || echo 1) "green: a grandparent holder is found (rc $rc)" |
| 49 | |
| 50 | # wait: a sibling holds the lock for 1.5 s; the helper waits, then holds it itself |
| 51 | setsid flock -x "$L" sleep 1.5 & |
| 52 | h=$! |
| 53 | i=0; while ! held_elsewhere "$L" && [ $i -lt 50 ]; do sleep 0.1; i=$((i + 1)); done |
| 54 | t0=$(date +%s%N) |
| 55 | run_bounded 50 "$out" bash -c '. "$1"; GB10_LOCK=$2; gb10_lock_acquire || exit 1; date +%s%N > "$3"; sleep 0.5' _ "$LIB" "$L" "$work/t1"; rc=$? |
| 56 | kill -- -"$h" 2>/dev/null; wait "$h" 2>/dev/null |
| 57 | t1=$(cat "$work/t1" 2>/dev/null || echo "$t0") |
| 58 | waited_ms=$(( (t1 - t0) / 1000000 )) |
| 59 | check $([ $rc = 0 ] && [ $waited_ms -ge 1000 ] && ! grep -q 'held by a parent' "$out" && echo 0 || echo 1) "wait: a non-ancestor holder is waited for (${waited_ms} ms, rc $rc)" |
| 60 | |
| 61 | # own: no holder; the lock is ours while fd 9 is open, and released after |
| 62 | . "$LIB" |
| 63 | GB10_LOCK=$L |
| 64 | gb10_lock_acquire; rc=$? |
| 65 | held_elsewhere "$L"; h1=$? |
| 66 | gb10_lock_release |
| 67 | held_elsewhere "$L"; h2=$? |
| 68 | check $([ $rc = 0 ] && [ $h1 = 0 ] && [ $h2 != 0 ] && echo 0 || echo 1) "own: taken through fd 9 (held $h1), dropped by gb10_lock_release (held $h2)" |
| 69 | |
| 70 | # new: a missing file is created and locked |
| 71 | GB10_LOCK=$work/new.lock |
| 72 | gb10_lock_acquire; rc=$? |
| 73 | check $([ $rc = 0 ] && [ -e "$work/new.lock" ] && held_elsewhere "$work/new.lock" && echo 0 || echo 1) "new: a missing lock file is created and locked" |
| 74 | gb10_lock_release |
| 75 | |
| 76 | # marker: the runner says it holds the lock (AIEN_GB10_LOCK_HELD=1, aien-sovereign-core #580); the helper returns at |
| 77 | # once without opening the file, even with no visible holder (the runner may sit in another PID namespace) |
| 78 | GB10_LOCK=$work/marker.lock |
| 79 | out=$work/marker.out |
| 80 | run_bounded 50 "$out" env AIEN_GB10_LOCK_HELD=1 bash -c '. "$1"; GB10_LOCK=$2; gb10_lock_acquire && echo ACQUIRED; if [ -e /proc/self/fd/9 ]; then echo FD9; fi' _ "$LIB" "$GB10_LOCK"; rc=$? |
| 81 | check $([ $rc = 0 ] && grep -q 'held by the caller' "$out" && grep -q ACQUIRED "$out" && ! grep -q FD9 "$out" && [ ! -e "$GB10_LOCK" ] && echo 0 || echo 1) "marker: AIEN_GB10_LOCK_HELD=1 skips the lock without opening it (rc $rc)" |
| 82 | |
| 83 | # marker under the judge: sandbox-shaped call, a holding parent plus the marker |
| 84 | run_bounded 50 "$out" flock -x "$L" env AIEN_GB10_LOCK_HELD=1 bash -c '. "$1"; GB10_LOCK=$2; gb10_lock_acquire && echo ACQUIRED' _ "$LIB" "$L"; rc=$? |
| 85 | check $([ $rc = 0 ] && grep -q ACQUIRED "$out" && echo 0 || echo 1) "marker: under a holding parent with the marker, returns at once (rc $rc)" |
| 86 | |
| 87 | # any other marker value is ignored: with a non-ancestor holder the helper still waits |
| 88 | setsid flock -x "$L" sleep 3 & |
| 89 | h=$! |
| 90 | i=0; while ! held_elsewhere "$L" && [ $i -lt 50 ]; do sleep 0.1; i=$((i + 1)); done |
| 91 | run_bounded 10 "$out" env AIEN_GB10_LOCK_HELD=0 bash -c '. "$1"; GB10_LOCK=$2; gb10_lock_acquire && echo ACQUIRED' _ "$LIB" "$L"; rc=$? |
| 92 | kill -- -"$h" 2>/dev/null; wait "$h" 2>/dev/null |
| 93 | check $([ $rc = 124 ] && ! grep -q ACQUIRED "$out" && echo 0 || echo 1) "marker: AIEN_GB10_LOCK_HELD=0 is not trusted, an unrelated holder is waited for (rc $rc)" |
| 94 | |
| 95 | echo "gb10_lock: $pass passed, $fail failed: $([ $fail = 0 ] && echo PASS || echo FAIL)" |
| 96 | [ $fail = 0 ] |