Re: [PATCH v2] test-lib: run EXPENSIVE tests one at a time
On 11/20/26 11:58, Junio C Hamano wrote:
Show 10 quoted lines
> Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:
> >> Recently, we've acquired quite a few expensive tests in our test suite.
>> For instance, these commits add several tests that require
>> multi-gigabytes of memory:
>>
>> - 9048b48fb (t1600-index: add expensive tests for over-long
>> tree paths, 2026-10-16) added tests to t1600
> > No such commit X-<. Perhaps 89048b48fb (t1600-index: add expensive
> tests for over-long tree paths, 2026-10-16).
>
Indeed. Sorry about the paste-error.
Show 7 quoted lines
>> - 288266c11d (xdiff: use size_t for merge buffer size computations,
>> 2026-10-16) added tests to t4300
>>
>> - 55c54fd375 (apply: reject binary patches greater than MAX_APPLY_SIZE,
>> 2026-03-25), added tests to t4141
> > Inconsistent use of "," after the (close parentheses).
> Good catch. I've fixed it.
Show 46 quoted lines
>> +# EXPENSIVE tests may need many gigabytes of memory or disk, so two of
>> +# them running at the same time can exhaust the machine.
>> +#
>> +# Allow only one at a time across all test scripts that share
>> +# this TEST_OUTPUT_DIRECTORY.
>> +expensive_lock_="$TEST_RESULTS_DIR/expensive.lock"
>> +have_expensive_lock_=
>> +
>> +acquire_expensive_lock_ () {
>> + local owner waited=
>> + mkdir -p "$TEST_RESULTS_DIR" || return 1
>> + while ! mkdir "$expensive_lock_" 2>/dev/null
>> + do
>> + owner=$(cat "$expensive_lock_/pid" 2>/dev/null)
>> + if test -n "$owner" && ! kill -0 "$owner" 2>/dev/null
>> + then
>> + # The holder was killed without releasing the lock.
>> + rm -rf "$expensive_lock_"
>> + continue
>> + fi
>> + if test -z "$waited"
>> + then
>> + say >&3 "waiting for another EXPENSIVE test to finish"
>> + waited=t
>> + fi
>> + sleep 1
>> + done
>> + echo $$ >"$expensive_lock_/pid"
>> + have_expensive_lock_=t
>> +}
> > Hmph, is the above safe when multiple processes are working with
> expensive tests during normal (i.e., not involving SIGKILL)
> operation, or does it still have TOCTOU race on the lock file?
> > * 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.
> Indeed. That is possible. I think we could tackle this by re-verifying the owner before the removal of the leftover lockfile. Additionally wrapping it within an additional break lock will help us to avoid the race where two waiter threads T2 and T3 both read T1's pid just before T1 removes it. Without the break lock, both would proceed to run the expensive test.Something like this on top:
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 193bf06aea..a769e10303 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -1104,10 +1104,18 @@ acquire_expensive_lock_ () {
while ! mkdir "$expensive_lock_" 2>/dev/null
do
owner=$(cat "$expensive_lock_/pid" 2>/dev/null)
- if test -n "$owner" && ! kill -0 "$owner" 2>/dev/null
+ if test -n "$owner" && ! kill -0 "$owner" 2>/dev/null &&
+ mkdir "$expensive_lock_.break" 2>/dev/null
then
# The holder was killed without releasing the lock.
- rm -rf "$expensive_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_/pid" 2>/dev/null)" = "$owner"
+ then
+ rm -rf "$expensive_lock_"
+ fi
+ rmdir "$expensive_lock_.break" 2>/dev/null
continue
fi
if test -z "$waited"
> Also, between the time you successfully mkdir the lock directory and
> the time you leave this function, you can be killed by a signal with
> INT, TERM, or HUP, for which t/test-lib.sh installs a hander to call
> exit, and there is a "trap die EXIT".
> > You may manage to create $expensive_lock_ directory, open pid file
> for writing, and then get killed before you echo $$ to the pid file
> or assign 't' to have_expensive_lock_ variable.
> > When your die handler calls release_expensive_lock_, you would leave
> with the directory behind and exit. You may not have pid file, or
> the pid file may be empty. The waiting processes will not run the
> kill -0 "$owner" part (because they do not know who you are that held
> the lock and left before writing $$ in pid), the waiters will wait
> for the mkdir to successfully create the directory that already
> exists and nobody will ever remove, no?
>
That's a good catch. I suppose setting the have_expensive_lock_ variable before writing the pid to the file, the issue should be sufficiently averted. That's because release_expensive_lock_ would proceed with removal of the directory even if we fail to write the pid file.
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 193bf06aea..a769e10303 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -1117,8 +1125,8 @@ acquire_expensive_lock_ () {
fi
sleep 1
done
- echo $$ >"$expensive_lock_/pid"
have_expensive_lock_=t
+ echo $$ >"$expensive_lock_/pid"
}
Of course, even with these the code is not foolproof and would miss the following cases:
- kill -9 before the pid is written: a holder killed after creating
the lock but before writing its pid leaves a lock that nobody treats
as stale, so every waiter hangs.
- Signal before the flag is set: an INT, TERM or HUP landing in the
microseconds between creating the lock and setting the flag does the
same thing.
I'm not sure if we need to go down the rabbit hold of fixing those too without actually facing those issues. I suppose it may be better to get the jobs running with a version of the patch that includes the above changes and look into further changes as we notice any more issues. I would love to hear your thoughts.
> Between the TOCTOU race allowing multiple processes entering the
> expensive region and deadlocking denying everybody from entering the
> expensive region, the latter is far worse, of course.
>
Indeed.
-- Sivaraam