magisk-zapret2/tests/release/build-lock.sh
root 378947743b
All checks were successful
Background release validation / Prepare Version (push) Successful in 3s
Background release validation / Fetch latest upstream zapret2 release (push) Successful in 4s
Magisk source guards / guards (push) Successful in 5s
Background release validation / Shell integration tests (push) Successful in 1m18s
Background release validation / Build Android APK (push) Successful in 5m14s
Background release validation / Package Magisk Module (push) Successful in 10s
Build lock: also wait for the server-wide lock shared with the CI runner
Owner request 2026-11-16: one heavy build at a time across CT 210 and the
Android CI jobs of the Forgejo runner in CT 206 (same US2-7 disks). Where the
machine provides us2-build-lock (CT 210), build.sh and the release builder
now also take the server-wide lock after this machine's lock: they wait
(showing who holds it), refuse on timeout and only warn when the runner
cannot be reached. Elsewhere (CI, other machines) nothing changes.

The helper process closes fd 9 so it never keeps this machine's lock after
the build has exited. tests/release/build-lock.sh stubs the client and
checks every answer (HELD, WAIT, TIMEOUT, UNAVAILABLE, no answer, a helper
that lingers, a refused build in a live shell); 6/6 mutations caught.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:02:47 +00:00

116 lines
4.7 KiB
Shell
Executable file

#!/usr/bin/env bash
# Behaviour of tools/build-lock.sh: a second build waits, then gives up;
# builds started by the lock holder pass; the lock frees when the build exits;
# the server-wide lock (us2-build-lock, stubbed here) is waited for, refuses on
# timeout and only warns when it cannot be reached.
set -euo pipefail
ROOT="$(cd -- "${1:-$(dirname "$0")/../..}" && pwd -P)"
HELPER="$ROOT/tools/build-lock.sh"
fail() {
printf 'build-lock test: %s\n' "$*" >&2
exit 1
}
[[ -f "$HELPER" && ! -L "$HELPER" ]] || fail "tools/build-lock.sh is missing"
command -v flock >/dev/null 2>&1 || {
printf 'build-lock test: skipped (flock is unavailable)\n'
exit 0
}
for entry in build.sh .codex/skills/build-zapret2-locally/scripts/build-local-release.sh; do
grep -Fq 'zapret2_build_lock' "$ROOT/$entry" ||
fail "$entry does not take the one-build lock"
done
grep -Fq 'sudo env ZAPRET2_BUILD_LOCK_HELD=1 bash -c' \
"$ROOT/.codex/skills/build-zapret2-locally/scripts/build-local-release.sh" ||
fail "the release builder would wait for its own lock in build.sh"
TMP="$(mktemp -d)"
holder=""
cleanup() {
if [[ -n "$holder" ]]; then kill "$holder" 2>/dev/null || true; fi
rm -rf "$TMP"
}
trap cleanup EXIT
export ZAPRET2_BUILD_LOCK_FILE="$TMP/build.lock"
unset ZAPRET2_BUILD_LOCK_HELD
# A stand-in for the server-wide lock client (us2-build-lock on CT 210), so the
# test never touches a real server lock; STUB_ANSWER picks its answer.
mkdir "$TMP/bin"
cat >"$TMP/bin/us2-build-lock" <<'STUB'
#!/usr/bin/env bash
[ "$1" = hold ] || exit 2
case "${STUB_ANSWER:-HELD}" in
HELD) echo HELD ;;
WAIT) echo "WAIT CI job demo"; echo HELD ;;
TIMEOUT) echo "WAIT CI job demo"; echo TIMEOUT ;;
UNAVAILABLE) echo "UNAVAILABLE ssh to demo failed" ;;
SILENT) ;;
LINGER) echo HELD; exec 0<&- 1>&-; sleep 3 ;;
esac
STUB
chmod +x "$TMP/bin/us2-build-lock"
export PATH="$TMP/bin:$PATH"
take_lock() {
bash -c '. "$1"; zapret2_build_lock' _ "$HELPER"
}
take_lock || fail "a free lock was not taken"
# The holder keeps running after its child (the trailing exit stops bash
# from exec-ing the child in its place and handing the lock over).
bash -c '. "$1"; zapret2_build_lock || exit 1
ZAPRET2_BUILD_LOCK_WAIT=1 bash -c ". \"\$1\"; zapret2_build_lock" _ "$1"
rc=$?; exit "$rc"' _ "$HELPER" ||
fail "a child build of the lock holder waited for its own lock"
# One process that owns the lock itself, so killing it frees the lock
# (`flock FILE sleep` would leave the sleeping child holding it).
bash -c 'exec 9<"$1"; flock 9; exec sleep 30' _ "$ZAPRET2_BUILD_LOCK_FILE" &
holder=$!
for _ in $(seq 50); do
flock -n "$ZAPRET2_BUILD_LOCK_FILE" true 2>/dev/null || break
sleep 0.1
done
if ZAPRET2_BUILD_LOCK_WAIT=1 take_lock 2>"$TMP/err"; then
fail "a second build started while another one held the lock"
fi
grep -Fq 'not starting a second one' "$TMP/err" || fail "the refusal does not say why"
ZAPRET2_BUILD_LOCK_HELD=1 ZAPRET2_BUILD_LOCK_WAIT=1 take_lock ||
fail "a build started by the lock holder waited for its own lock"
kill "$holder"
wait "$holder" 2>/dev/null || true
holder=""
ZAPRET2_BUILD_LOCK_WAIT=5 take_lock || fail "the lock did not free when the holder exited"
# Server-wide lock answers (the local lock is free here).
STUB_ANSWER=HELD take_lock 2>"$TMP/err" || fail "a free server-wide lock was not taken"
[[ ! -s "$TMP/err" ]] || fail "a free server-wide lock printed: $(cat "$TMP/err")"
STUB_ANSWER=WAIT take_lock 2>"$TMP/err" || fail "the build did not go on after waiting for the server-wide lock"
grep -Fq 'Another heavy build on this server is running (CI job demo)' "$TMP/err" ||
fail "waiting for the server-wide lock is not shown"
if STUB_ANSWER=TIMEOUT take_lock 2>"$TMP/err"; then
fail "a build started while the server-wide lock stayed busy"
fi
grep -Fq 'not starting a second one' "$TMP/err" || fail "the server-wide refusal does not say why"
STUB_ANSWER=TIMEOUT bash -c '. "$1"; zapret2_build_lock 2>/dev/null && exit 9
flock -n "$ZAPRET2_BUILD_LOCK_FILE" true' _ "$HELPER" ||
fail "a refused build kept this machine's lock"
for answer in UNAVAILABLE SILENT; do
STUB_ANSWER=$answer take_lock 2>"$TMP/err" ||
fail "an unreachable server-wide lock ($answer) stopped the build"
grep -Fq 'server-wide build lock is unavailable' "$TMP/err" ||
fail "an unreachable server-wide lock ($answer) is not reported"
done
STUB_ANSWER=LINGER take_lock || fail "a lingering server-wide helper stopped the build"
flock -n "$ZAPRET2_BUILD_LOCK_FILE" true ||
fail "the server-wide helper kept this machine's lock after the build ended"
STUB_ANSWER=TIMEOUT ZAPRET2_BUILD_LOCK_HELD=1 take_lock ||
fail "a child build of the lock holder waited for the server-wide lock"
printf 'build-lock contract: passed\n'