Git development
 help / color / mirror / Atom feed
From: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Git mailing list <git@vger.kernel.org>, Patrick Steinhardt <ps@pks.im>
Subject: Re: [PATCH v2] test-lib: run EXPENSIVE tests one at a time
Date: Tue, 24 Nov 2026 19:04:41 +0530	[thread overview]
Message-ID: <781ae43f-7831-4aa7-a7a0-aee069950eac@gmail.com> (raw)
In-Reply-To: <xmqqpkw0rp8i.fsf@gitster.g>

On 11/20/26 11:58, Junio C Hamano wrote:
> 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.

>>    - 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.

>> +# 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


  reply	other threads:[~2026-11-24 13:34 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  7:40 [PATCH] test-lib: run EXPENSIVE tests one at a time Kaartic Sivaraam
2026-11-19 10:07 ` Patrick Steinhardt
2026-11-19 17:12   ` Kaartic Sivaraam
2026-11-19 17:29 ` [PATCH v2] " Kaartic Sivaraam
2026-11-19 17:33   ` Kaartic Sivaraam
2026-11-20  6:28   ` Junio C Hamano
2026-11-24 13:34     ` Kaartic Sivaraam [this message]
2026-11-25 20:21       ` Junio C Hamano
2026-11-27 13:59         ` Kaartic Sivaraam
2026-11-27 18:34           ` Junio C Hamano
2026-11-25 19:09   ` Harald Nordgren
2026-11-27 17:23 ` [PATCH v3] " Kaartic Sivaraam
2026-11-27 20:46   ` Junio C Hamano
2026-11-28  8:06     ` Kaartic Sivaraam
2026-11-28 14:57 ` [PATCH v4 0/2] test-lib: serialize EXPENSIVE tests to avoid OOM in CI Kaartic Sivaraam
2026-11-28 14:57   ` [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time Kaartic Sivaraam
2026-11-29 16:55     ` Junio C Hamano
2026-11-30 14:13     ` Christian Couder
2026-11-28 14:58   ` [PATCH v4 2/2] t0000: test taking over and releasing the EXPENSIVE lock Kaartic Sivaraam

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=781ae43f-7831-4aa7-a7a0-aee069950eac@gmail.com \
    --to=kaartic.sivaraam@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox