From 4f1a4530aae1cce9629096248cb2e39f3b61063a Mon Sep 17 00:00:00 2001 From: Mistercoolertyper <84140706+Mistercoolertyper@users.noreply.github.com> Date: Wed, 4 Nov 2026 09:25:17 +0100 Subject: [PATCH] Serialise concurrent harness runs and purge stale apt indexes Two runs of the harness share /var/lib/apt/lists and /etc/apt/sources.list.d. With two in flight, one run's teardown deleted the other's temp repo while apt was reading it, producing 'Could not open file .../vdi-debian-test.' - a failure that looks like a port bug but is a collision. Take a flock for the duration of the run, and clear indexes left by earlier runs whose temp directory no longer exists. --- debian/test/run-tests.sh | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/debian/test/run-tests.sh b/debian/test/run-tests.sh index 596d81a..1556076 100644 --- a/debian/test/run-tests.sh +++ b/debian/test/run-tests.sh @@ -41,6 +41,22 @@ SANDBOX_DIR="$TMP_DIR/sandbox" LIST_FILE="/etc/apt/sources.list.d/zz-vdi-test-$$.list" KEYRING_DIR=/etc/apt/keyrings +# Two concurrent runs share /var/lib/apt/lists and /etc/apt/sources.list.d. +# Measured: with two runs in flight, one run's teardown deleted the other's +# temp repo while apt was reading it, and `apt-get install --simulate` failed +# with "Could not open file /var/lib/apt/lists/_tmp_vdi-debian-test." +# - a failure that looks like a port bug but is a collision. Serialise instead. +LOCK_FILE="/var/lock/vdi-debian-test.lock" +if ! exec 9>"$LOCK_FILE"; then + printf 'Cannot open lock file %s\n' "$LOCK_FILE" + exit 1 +fi +if ! flock -n 9; then + printf 'Another run of this harness is already in progress (lock: %s).\n' "$LOCK_FILE" + printf 'They share apt state; run them one at a time.\n' + exit 1 +fi + # Never let Python write __pycache__ into the repo: py_compile would otherwise # leave an untracked directory behind and dirty the working tree. export PYTHONPYCACHEPREFIX="$TMP_DIR/pycache" @@ -97,6 +113,16 @@ purge_apt_lists_for() { return 0 } +# Also clear indexes left by any earlier run of this harness, including ones +# whose temp directory no longer exists. A stale index pointing at a deleted +# repo makes `apt-get install --simulate` fail for a reason unrelated to the +# code under test. +purge_all_harness_lists() { + rm -f /var/lib/apt/lists/*vdi-debian-test* 2>/dev/null + rm -f /var/lib/apt/lists/partial/*vdi-debian-test* 2>/dev/null + return 0 +} + cleanup() { local status=$? @@ -145,6 +171,9 @@ for tool in apt-ftparchive apt-get apt-cache dpkg-deb python3 gzip; do fi done +# Clear indexes from any earlier run before doing anything else. +purge_all_harness_lists + CODENAME="" if [ -r /etc/os-release ]; then # shellcheck disable=SC1091 @@ -401,7 +430,15 @@ fi if apt-get install --simulate -y icaclient >"$TMP_DIR/simulate.log" 2>&1; then pass "T07 apt-get install --simulate -y icaclient resolves (no missing deps)" else - fail "T07 apt-get install --simulate -y icaclient did not resolve" + # Distinguish "the repo is broken" from "something else touched apt". + # A stale or vanished index for ANOTHER harness run produces a path we + # never created, and blaming the port for that would be wrong. + if grep -q 'vdi-debian-test' "$TMP_DIR/simulate.log" \ + && ! grep -qF "$(list_fragment "$REPO_DIR")" "$TMP_DIR/simulate.log"; then + fail "T07 simulation failed because another harness run's index vanished (concurrency, not the port)" + else + fail "T07 apt-get install --simulate -y icaclient did not resolve" + fi tail -15 "$TMP_DIR/simulate.log" | sed 's/^/ /' fi @@ -956,6 +993,7 @@ rmdir "$KEYRING_DIR" 2>/dev/null purge_apt_lists_for "$REPO_DIR" purge_apt_lists_for "$TAMPER_DIR" purge_apt_lists_for "$SANDBOX_DIR" +purge_all_harness_lists rm -rf "$TMP_DIR" 2>/dev/null apt-get update -qq >/dev/null 2>&1