git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] test-lib: run EXPENSIVE tests one at a time

From
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Date
Nov 24, 2026, 13:34 UTC
Message-ID
<781ae43f-7831-4aa7-a7a0-aee069950eac@gmail.com>
In-Reply-To
<xmqqpkw0rp8i.fsf@gitster.g>
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
Previous: Junio C HamanoNext: Junio C Hamano
Message 7 of 32 in “test-lib: run EXPENSIVE tests one at a time”
  1. test-lib: run EXPENSIVE tests one at a timeKaartic Sivaraam, Nov 19, 2026
  2. Patrick SteinhardtNov 19, 2026
  3. Kaartic SivaraamNov 19, 2026
  4. test-lib: run EXPENSIVE tests one at a timeKaartic Sivaraam, Nov 19, 2026
  5. Kaartic SivaraamNov 19, 2026
  6. Junio C HamanoNov 20, 2026
  7. Kaartic SivaraamNov 24, 2026
  8. Junio C HamanoNov 25, 2026
  9. Kaartic SivaraamNov 27, 2026
  10. Junio C HamanoNov 27, 2026
  11. Harald NordgrenNov 25, 2026
  12. test-lib: run EXPENSIVE tests one at a timeKaartic Sivaraam, Nov 27, 2026
  13. Junio C HamanoNov 27, 2026
  14. Kaartic SivaraamNov 28, 2026
  15. 0/2 test-lib: serialize EXPENSIVE tests to avoid OOM in CIKaartic Sivaraam, Nov 28, 2026
  16. 1/2 test-lib: run EXPENSIVE tests one at a timeKaartic Sivaraam, Nov 28, 2026
  17. Junio C HamanoNov 29, 2026
  18. Christian CouderNov 30, 2026
  19. Kaartic SivaraamDec 1, 2026
  20. Christian CouderDec 1, 2026
  21. Kaartic SivaraamDec 1, 2026
  22. Patrick SteinhardtDec 1, 2026
  23. Kaartic SivaraamDec 1, 2026
  24. Patrick SteinhardtDec 1, 2026
  25. Kaartic SivaraamDec 1, 2026
  26. Patrick SteinhardtDec 1, 2026
  27. 2/2 t0000: test taking over and releasing the EXPENSIVE lockKaartic Sivaraam, Nov 28, 2026
  28. Patrick SteinhardtDec 1, 2026
  29. Kaartic SivaraamDec 1, 2026
  30. test-lib: run EXPENSIVE tests one at a timeKaartic Sivaraam, Dec 1, 2026
  31. Patrick SteinhardtDec 3, 2026
  32. Kaartic SivaraamDec 3, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.