On Sat, Nov 28, 2026 at 3:59 PM Kaartic Sivaraam <kaar…@gmail.com> wrote: [...]
> Take a lock shared by all test scripts around each EXPENSIVE test, so > only one of them runs at a time. Cheap tests in the same scripts keep > running in parallel. Only the expensive ones wait for each other.
This looks good to me and we might want to merge this soon to unblock CI, but here are still some optional suggestions for a v5 or a separate hardening or polishing series.
> +# This function takes care of cleaning up the lock we > +# acquire in acquire_expensive_lock_. It is defined > +# up here as we refer to it in die(). > +release_expensive_lock_ () { > + test -n "$have_expensive_lock_" || return 0 > + rm -f "$expensive_lock_" > + have_expensive_lock_= > +}
Something like the following would check whose lock it is before releasing it: ------------ # Remove the lock only if it still names $1 as its owner, so that # a lock someone else has just taken is left alone. remove_expensive_lock_owned_by_ () { test "$(cat "$expensive_lock_" 2>/dev/null)" = "$1" && rm -f "$expensive_lock_" } release_expensive_lock_ () { test -n "$have_expensive_lock_" || return 0 remove_expensive_lock_owned_by_ $$ have_expensive_lock_= } ------------ This would limit the damage in the case (mentioned in the commit message) where two waiters clearing the same stale lock at once let two expensive tests run together: - waiters A and B both see a stale lock, - A removes it and takes the lock, then B, which re-checked before A removed it, removes A's lock and takes it too, - both run, and when A finishes it deletes B's lock, so C starts while B is still running. With the ownership check, A leaves B's lock alone, so the damage stays at two tests instead of growing. [...]
> +acquire_expensive_lock_ () { > + local owner waited= tmp="$expensive_lock_.$$" misses=0 > + mkdir -p "$TEST_RESULTS_DIR" || > + { expensive_lock_unavailable_; return 0; } > + # The lock is a hard link to a file that already holds our PID, so > + # it never exists without its owner recorded in it. Remove any > + # $tmp left by a dead process with our PID first: it may still be > + # linked to that process's lock, which writing to it would empty. > + rm -f "$tmp" > + echo $$ >"$tmp" || > + { expensive_lock_unavailable_; return 0; } > + while ! ln "$tmp" "$expensive_lock_" 2>/dev/null > + do > + if ! test -f "$expensive_lock_" > + then > + # Nobody holds the lock, yet we cannot create it (no > + # hard links, disk full, ...). Retry in case it was > + # just released, then run anyway rather than wait > + # forever. > + misses=$((misses + 1)) > + test $misses -lt 3 && continue > + expensive_lock_unavailable_ > + return 0 > + fi > + misses=0 > + owner=$(cat "$expensive_lock_" 2>/dev/null) > + # A lock carrying our own PID was left by a dead holder whose > + # PID we were given again; we know we do not hold it. > + if test -n "$owner" && > + { test "$owner" = $$ || ! kill -0 "$owner" 2>/dev/null; } > + then > + # The holder was killed without releasing the lock. > + # > + # Another waiter may have taken it over since we > + # looked, so only remove it if it is still the same. > + if test "$(cat "$expensive_lock_" 2>/dev/null)" = "$owner" > + then > + rm -f "$expensive_lock_" > + fi
Here we could both reuse the remove_expensive_lock_owned_by_() helper and be a bit more verbose, for example: # Another waiter may have taken it over since we # looked, so only remove it if it is still the same. if remove_expensive_lock_owned_by_ "$owner" then say >&3 "removing stale EXPENSIVE lock left by PID $owner" fi The extra message might help when reading the logs of a run where a script was killed.
> + continue > + fi > + if test -z "$waited" > + then > + say >&3 "waiting for another EXPENSIVE test to finish"
This and the message in expensive_lock_unavailable_() use `say >&3` which prints to /dev/null when `-v` or `--verbose-log` is not passed. This is not an issue for CI runs as `--verbose-log` is used then. But someone running tests locally without `-v` might wonder why their local run is stuck. With Meson, or when running scripts or `prove` directly, nothing cleans test-results/. So a lock left behind by SIGKILL (including the OOM killer) can still be there days later. The same PID could then be reused by a long-running process of the same user. This would result in the script silently waiting forever. In such a case, printing something even if `-v` or `--verbose-log` is not used, would likely make it much easier to understand what's going on. So I would suggest sending this message, as well as the "running anyway" and "removing stale" messages, to stderr instead. There are only a few dozen EXPENSIVE tests, so this should not add much output. The subtests in 2/2 that go through the stale lock removal would then need to expect that message on stderr though. The message would also help even more if it included the holder's PID, for example like "waiting for EXPENSIVE lock held by PID 1234".
> + waited=t > + fi > + sleep 1 > + done > + have_expensive_lock_=t > + rm -f "$tmp" > +}
Thanks.