Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time
On 12/1/26 12:52, Patrick Steinhardt wrote:
Show 14 quoted lines
>> +
>> +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"
> > Nit: it is expected that in most cases this won't do anything, so in the
> expected common case we'll see an error message. Do we want to silence
> this?
>I guess that was mostly done to keep things consistent as we pretty much silence everything in this function. So, I tend to prefer keeping the '-f' on this one too.
Show 7 quoted lines
>> + echo $$ >"$tmp" ||
>> + { expensive_lock_unavailable_; return 0; }
> > Nit: It would have been nice to maybe have empty lines in this function
> to make it easier to follow. Right now the reader is presented one huge
> chunk, and they have to figure out by themselves what the logical blocks
> are.
> Indeed. This is a good one. I've tried to improve things by throwing newlines in places where it seemed logically better to place them.Show 20 quoted lines
>> + 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
> > I was first thinking that this would trigger after three retries
> unconditionally, so essentially after three seconds. And that would have
> been overly aggressive, as we should expect that expensive tests run
> more than 3 seconds.
> > But this is only detecting the case where we fail to create the lock due
> to whatever reason. So it's a rather uncommon case.
> Yep. We won't be retrying when the lock already exists. The comment should've conveyed this. If you think we could improve it in someway, do let me know.
Show 22 quoted lines
>> + 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
> > I feel like re-checking the lock owner is kind of pointless here. The
> check doesn't fix the race, but it suggests it to the reader. And that
> is overall fine, because the intent now is to fail in a way that we
> prefer to run the test instead of blocking forever.
> > But it raises the question why we even bother to check again. To me it
> only causes puzzlement.
> The re-check is present in order to prevent a specific race scenario pointed out by Junio:Show 12 quoted lines
> * T1 holds $expensive_lock_, finishes its last EXPENSIVE test,
> calls release_expensive_lock_ (rm -rf "$expensive_lock_"), and
> exits.
> > * Waiter T2 read owner=$(cat "$expensive_lock_/pid" 2>/dev/null)
> (obtaining T1's PID) just before T1 removed the directory.
> > * Waiter T3 runs mkdir "$expensive_lock_", succeeds, writes T3's
> PID to pid, and begins its EXPENSIVE test.
> > * T2 now checks test -n "$owner" && ! kill -0 "$owner" 2>/dev/null
> against T1's exited PID, finds that T1 is gone, runs rm -rf
> "$expensive_lock_", which deletes T3's active lock. It loops,
> succeeds on mkdir "$expensive_lock_", and runs its EXPENSIVE test
> concurrently with T3.
It talks about the variant that created directories but it applies for the current linking variant also.
If we could improve the comment above the relevant code in someway, do let me know. It tries to convey it but I unfortunately couldn't get what detail it is missing.
Show 11 quoted lines
>> @@ -1115,6 +1197,7 @@ test_run_ () {
>> test_eval_ "$test_cleanup"
>> teardown_malloc_check
>> fi
>> + release_expensive_lock_
> > In the other block where we call this function we have this one:
> >> + release_expensive_lock_
>> + test -z "$expensive_lock_" || rm -f "$expensive_lock_.$$"
> > And as far as I can see, `release_expensive_lock_` does not also prune
> the PID-file. Do we have to do that here?
> We ideally reach this only after a normal flow of acquire_expensive_lock_. We do have 'rm -r "$tmp"' at the end of it which is intended to do exactly that. So, I think it is not necessary here.--
Sivaraam