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.<other>' -
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.
This commit is contained in:
Mistercoolertyper committed 2026-11-04 09:25:17 +01:00
1 parent 6ddb1d251a
commit 4f1a4530aa
1 file changed
+38
+38
View File
@@ -41,6 +41,22 @@ SANDBOX_DIR="$TMP_DIR/sandbox"
LIST_FILE="/etc/apt/sources.list.d/zz-vdi-test-$$.list" LIST_FILE="/etc/apt/sources.list.d/zz-vdi-test-$$.list"
KEYRING_DIR=/etc/apt/keyrings 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.<other>"
# - 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 # Never let Python write __pycache__ into the repo: py_compile would otherwise
# leave an untracked directory behind and dirty the working tree. # leave an untracked directory behind and dirty the working tree.
export PYTHONPYCACHEPREFIX="$TMP_DIR/pycache" export PYTHONPYCACHEPREFIX="$TMP_DIR/pycache"
@@ -97,6 +113,16 @@ purge_apt_lists_for() {
return 0 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() { cleanup() {
local status=$? local status=$?
@@ -145,6 +171,9 @@ for tool in apt-ftparchive apt-get apt-cache dpkg-deb python3 gzip; do
fi fi
done done
# Clear indexes from any earlier run before doing anything else.
purge_all_harness_lists
CODENAME="" CODENAME=""
if [ -r /etc/os-release ]; then if [ -r /etc/os-release ]; then
# shellcheck disable=SC1091 # shellcheck disable=SC1091
@@ -401,7 +430,15 @@ fi
if apt-get install --simulate -y icaclient >"$TMP_DIR/simulate.log" 2>&1; then 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)" pass "T07 apt-get install --simulate -y icaclient resolves (no missing deps)"
else else
# 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" fail "T07 apt-get install --simulate -y icaclient did not resolve"
fi
tail -15 "$TMP_DIR/simulate.log" | sed 's/^/ /' tail -15 "$TMP_DIR/simulate.log" | sed 's/^/ /'
fi fi
@@ -956,6 +993,7 @@ rmdir "$KEYRING_DIR" 2>/dev/null
purge_apt_lists_for "$REPO_DIR" purge_apt_lists_for "$REPO_DIR"
purge_apt_lists_for "$TAMPER_DIR" purge_apt_lists_for "$TAMPER_DIR"
purge_apt_lists_for "$SANDBOX_DIR" purge_apt_lists_for "$SANDBOX_DIR"
purge_all_harness_lists
rm -rf "$TMP_DIR" 2>/dev/null rm -rf "$TMP_DIR" 2>/dev/null
apt-get update -qq >/dev/null 2>&1 apt-get update -qq >/dev/null 2>&1