threads / patch / 66877

patchtest-lib: run EXPENSIVE tests one at a time

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

## tl;dr

Sanitizer jobs on GitLab and GitHub runners have hung when several multi-gigabyte tests run in parallel. Read the story.

replies: 30people: 5as markdown or json

Kaartic Sivaraam· Nov 19, 2026, 07:40 UTC · lore

Recently, we've acquired quite a few expensive test in our test suite. For instance, these commits add several tests that require multi-gigabytes:

  - 9048b48fb (t1600-index: add expensive tests for over-long
    tree paths, 2026-10-16) added tests to t1600
  - 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
We also have the memory hungry 5GB filtered hash-object test in t1007.

When GIT_TEST_LONG is set, "prove --jobs" happily runs several of these at once. On the 16GB GitLab CI runners this exhausts memory: the sanitizer jobs have been hanging in t1007 until they hit the job timeout ever since the new EXPENSIVE tests in t1600 started running alongside it. Running the two under a 16GB memory cap reproduces it, with the t1007 git process getting OOM-killed.

GitHub CI runners are no exception and observe the same behaviour.

Take a lock shared by all test scripts around each EXPENSIVE test, so only one of them runs at a time. The lock is a directory in TEST_RESULTS_DIR, created with mkdir(1) like the existing valgrind symlink lock. It records the holder's PID so that a lock left behind by a script that was killed with SIGKILL gets taken over by the next waiter. It is released after the test and its cleanup finish, and from the EXIT trap when a script dies or is interrupted while holding it.

Cheap tests in the same scripts keep running in parallel. Only the expensive ones wait for each other.

Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
In recent times, we've been noticing the sanitizer jobs (linux-leaks,
linux-reftable-leaks, linux-asan-ubsan) hang and fail with timeout in
our GitLab CI runners. On some digging, it turns out that this is
because of multiple memory hungry tests running in parallel.

So, I thought of suggesting this patch to serialize the expensive tests at least to avoid the putting the CI runners under memory pressure.

The GitHub CI runners also observed the same behaviour.
Both CI runners are happy with this change.
GitHub: https://github.com/sivaraam/git/actions/runs/43281285339
GitLab: https://gitlab.com/gitlab-org/git/-/pipelines/3079929506
---
 t/README      |  4 +++-
 t/test-lib.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
Show changes to 2 files +47 −1

t/README, t/test-lib.sh

diff --git a/t/README b/t/README
index fd7e8504b5..c156dd3af6 100644
--- a/t/README
+++ b/t/README
@@ -117,7 +117,9 @@ appropriately before running "make". Short options can be bundled, i.e.
 -l::
 --long-tests::
 	This causes additional long-running tests to be run (where
-	available), for more exhaustive testing.
+	available), for more exhaustive testing. These tests can use a
+	lot of memory and disk, so only one of them runs at a time, even
+	when several test scripts run in parallel (e.g. "prove --jobs").
 
 -r::
 --run=<test-selector>::
diff --git a/t/test-lib.sh b/t/test-lib.sh
index c56654b7c5..4d29172c9d 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -784,6 +784,7 @@ die () {
 	# test script run with '--immediate' fails, or when the user hits
 	# ctrl-C, i.e. when 'test_done' is not invoked at all.
 	test_atexit_handler || code=$?
+	release_expensive_lock_
 	if test -n "$GIT_EXIT_OK"
 	then
 		exit $code
@@ -1089,6 +1090,42 @@ fail_117 () {
 	return 117
 }
 
+# EXPENSIVE tests can need many gigabytes of memory or disk, so two of
+# them running at the same time under "prove --jobs" 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_ () {
+	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 "$expensive_lock_waited_"
+		then
+			say >&3 "waiting for another EXPENSIVE test to finish"
+			expensive_lock_waited_=t
+		fi
+		sleep 1
+	done
+	expensive_lock_waited_=
+	echo $$ >"$expensive_lock_/pid"
+	have_expensive_lock_=t
+}
+
+release_expensive_lock_ () {
+	test -n "$have_expensive_lock_" || return 0
+	rm -rf "$expensive_lock_"
+	have_expensive_lock_=
+}
+
 test_run_ () {
 	test_cleanup=:
 	expecting_failure=$2
@@ -1103,6 +1140,12 @@ test_run_ () {
 		fi
 	fi
 
+	case ",$test_prereq," in
+	*,EXPENSIVE,*)
+		acquire_expensive_lock_
+		;;
+	esac
+
 	setup_malloc_check
 	test_eval_ "$1"
 	eval_ret=$?
@@ -1115,6 +1158,7 @@ test_run_ () {
 		test_eval_ "$test_cleanup"
 		teardown_malloc_check
 	fi
+	release_expensive_lock_
 	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
 	then
 		echo ""

---
base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4
change-id: 20261117-ks-ci-long-test-hang-fix-008e72fd40c6
Patrick Steinhardt· Nov 19, 2026, 10:07 UTC · re: Kaartic Sivaraam · lore

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

On Thu, Nov 19, 2026 at 01:10:31PM +0530, Kaartic Sivaraam wrote:
> Recently, we've acquired quite a few expensive test in our test suite.
s/test/tests/
> For instance, these commits add several tests that require
> multi-gigabytes:
Multi-gigabytes what?
[snip]
> In recent times, we've been noticing the sanitizer jobs (linux-leaks,
> linux-reftable-leaks, linux-asan-ubsan) hang and fail with timeout in
> our GitLab CI runners. On some digging, it turns out that this is
> because of multiple memory hungry tests running in parallel.

Yeah, our pipelines are currently a mess because of this, so I'd be really happy to have this resolved soonish.

Show 9 quoted lines
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index c56654b7c5..4d29172c9d 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -1089,6 +1090,42 @@ fail_117 () {
>  	return 117
>  }
>  
> +# EXPENSIVE tests can need many gigabytes of memory or disk, so two of
s/can/may/
> +# them running at the same time under "prove --jobs" can exhaust the

I wouldn't single out `prove --jobs` here, as we know to run concurrently without prove either via our plain Makefile or via Meson.

Show 15 quoted lines
> +# 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_ () {
> +	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

Okay, if the previous owner does not exist anymore we prune the lock. Two racing processes which try to acquire the lock may race in a way that the "/pid" file does not exist anymore because the other process has already deleted it. But you protect against that by checking whether we have been able to read a PID in the first place via `test -n`.

> +		fi
> +		if test -z "$expensive_lock_waited_"
> +		then
> +			say >&3 "waiting for another EXPENSIVE test to finish"

Hmm. So file descriptor 3 points to stderr or to the test output file. And if I remember correctly, we're indeed free to write to stderr whatever data we want without breaking the TAP format?

Show 5 quoted lines
> +			expensive_lock_waited_=t
> +		fi
> +		sleep 1
> +	done
> +	expensive_lock_waited_=
Should this be declared as a local variable?

Overall this is a rather pragmatic approach to solve the issue. I was thinking whether it would make sense to distinguish e.g. EXPENISVE_MEM from EXPENSIVE_DISK, and have different locks for each of those. But I doubt adding this kind of complexity would really be worth it in the end.

So I'm fine with the approach. Onward to green pipelines again! Well, until the next thing breaks next week...

Thanks!
Patrick
Kaartic Sivaraam· Nov 19, 2026, 17:12 UTC · re: Patrick Steinhardt · lore

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

On 11/19/26 15:37, Patrick Steinhardt wrote:
> On Thu, Nov 19, 2026 at 01:10:31PM +0530, Kaartic Sivaraam wrote:
>> Recently, we've acquired quite a few expensive test in our test suite.
> > s/test/tests/
Corrected it.
> >> For instance, these commits add several tests that require
>> multi-gigabytes:
> > Multi-gigabytes what?
>
Fixed it!
Show 10 quoted lines
>> diff --git a/t/test-lib.sh b/t/test-lib.sh
>> index c56654b7c5..4d29172c9d 100644
>> --- a/t/test-lib.sh
>> +++ b/t/test-lib.sh
>> @@ -1089,6 +1090,42 @@ fail_117 () {
>>   	return 117
>>   }
>>   >> +# EXPENSIVE tests can need many gigabytes of memory or disk, so two of
> > s/can/may/
> Done.
>> +# them running at the same time under "prove --jobs" can exhaust the
> > I wouldn't single out `prove --jobs` here, as we know to run
> concurrently without prove either via our plain Makefile or via Meson.
>
Makes sense. Will remove the part mentioning 'prove --jobs' specifically.
Show 20 quoted lines
>> +# 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_ () {
>> +	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
> > Okay, if the previous owner does not exist anymore we prune the lock.
> Two racing processes which try to acquire the lock may race in a way
> that the "/pid" file does not exist anymore because the other process
> has already deleted it. But you protect against that by checking whether
> we have been able to read a PID in the first place via `test -n`.
Indeed. There might still be a race in the case when two racing processes (A and B) both see the same dead PID before either acts. A would have deleted the lock, created a new one, written its PID. B which still holds the stale PID would proceed to delete the lock which is actually the live lock held by A, create a new one and write its PID. This might happen if a holder was SIGKILLed and two or more scripts were waiting at that exact moment, so it's rare. So, I wouldn't complicate the code to handle this. In the worst case, we'd have 2 expensive test cases running in place of the one we intend.
Show 8 quoted lines
> >> +		fi
>> +		if test -z "$expensive_lock_waited_"
>> +		then
>> +			say >&3 "waiting for another EXPENSIVE test to finish"
> > Hmm. So file descriptor 3 points to stderr or to the test output file.
> And if I remember correctly, we're indeed free to write to stderr
> whatever data we want without breaking the TAP format?
> Yep. With no '--verbose' or '--verbose-log' flags this message would just go to /dev/null. I suppose that will only happen when someone runs this manually. So, hopefully that's fine.
Show 7 quoted lines
>> +			expensive_lock_waited_=t
>> +		fi
>> +		sleep 1
>> +	done
>> +	expensive_lock_waited_=
> > Should this be declared as a local variable?
>
Sure. Will make 'owner' local too.
Show 6 quoted lines
> Overall this is a rather pragmatic approach to solve the issue. I was
> thinking whether it would make sense to distinguish e.g. EXPENISVE_MEM
> from EXPENSIVE_DISK, and have different locks for each of those. But I
> doubt adding this kind of complexity would really be worth it in the
> end.
> Got it. Thank you for sharing your thoughts!
> So I'm fine with the approach. Onward to green pipelines again! Well,
> until the next thing breaks next week...
> Totally. It feels is like we're constantly fire-fighting in the CI these days ...
-- 
Sivaraam
Kaartic Sivaraam· Nov 19, 2026, 17:29 UTC · re: Kaartic Sivaraam · lore

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

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
  - 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
We also have the memory hungry 5GB filtered hash-object test in t1007.

When GIT_TEST_LONG is set, "prove --jobs" happily runs several of these at once. On the 16GB GitLab CI runners this exhausts memory: the sanitizer jobs have been hanging in t1007 until they hit the job timeout ever since the new EXPENSIVE tests in t1600 started running alongside it. Running the two under a 16GB memory cap reproduces it, with the t1007 git process getting OOM-killed.

GitHub CI runners are no exception and observe the same behaviour.

Take a lock shared by all test scripts around each EXPENSIVE test, so only one of them runs at a time. The lock is a directory in TEST_RESULTS_DIR, created with mkdir(1) like the existing valgrind symlink lock. It records the holder's PID so that a lock left behind by a script that was killed with SIGKILL gets taken over by the next waiter. It is released after the test and its cleanup finish, and from the EXIT trap when a script dies or is interrupted while holding it.

Cheap tests in the same scripts keep running in parallel. Only the expensive ones wait for each other.

Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
In recent times, we've been noticing the sanitizer jobs (linux-leaks,
linux-reftable-leaks, linux-asan-ubsan) hang and fail with timeout in
our GitLab CI runners. On some digging, it turns out that this is
because of multiple memory hungry tests running in parallel.

So, I thought of suggesting this patch to serialize the expensive tests at least to avoid the putting the CI runners under memory pressure.

The GitHub CI runners also observed the same behaviour.
Both CI runners are happy with this change.
GitHub: https://github.com/sivaraam/git/actions/runs/43281285339
GitLab: https://gitlab.com/gitlab-org/git/-/pipelines/3079929506
---
Changes in v2:
- Corrected commit messages as per Patrick's suggestions
- Tweaked the shell function comment to avoid mentioning 'prove --jobs'
- Made the owner and waiting variables local within acquire_expensive_lock_
Link to v1: https://patch.msgid.link/20261119-ks-ci-long-test-hang-fix-v1-1-45c2fff6d9f0@gmail.com
---
 t/README      |  4 +++-
 t/test-lib.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)
Show changes to 2 files +48 −1

t/README, t/test-lib.sh

diff --git a/t/README b/t/README
index fd7e8504b5..c156dd3af6 100644
--- a/t/README
+++ b/t/README
@@ -117,7 +117,9 @@ appropriately before running "make". Short options can be bundled, i.e.
 -l::
 --long-tests::
 	This causes additional long-running tests to be run (where
-	available), for more exhaustive testing.
+	available), for more exhaustive testing. These tests can use a
+	lot of memory and disk, so only one of them runs at a time, even
+	when several test scripts run in parallel (e.g. "prove --jobs").
 
 -r::
 --run=<test-selector>::
diff --git a/t/test-lib.sh b/t/test-lib.sh
index c56654b7c5..193bf06aea 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -784,6 +784,7 @@ die () {
 	# test script run with '--immediate' fails, or when the user hits
 	# ctrl-C, i.e. when 'test_done' is not invoked at all.
 	test_atexit_handler || code=$?
+	release_expensive_lock_
 	if test -n "$GIT_EXIT_OK"
 	then
 		exit $code
@@ -1089,6 +1090,43 @@ fail_117 () {
 	return 117
 }
 
+# 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
+}
+
+release_expensive_lock_ () {
+	test -n "$have_expensive_lock_" || return 0
+	rm -rf "$expensive_lock_"
+	have_expensive_lock_=
+}
+
 test_run_ () {
 	test_cleanup=:
 	expecting_failure=$2
@@ -1103,6 +1141,12 @@ test_run_ () {
 		fi
 	fi
 
+	case ",$test_prereq," in
+	*,EXPENSIVE,*)
+		acquire_expensive_lock_
+		;;
+	esac
+
 	setup_malloc_check
 	test_eval_ "$1"
 	eval_ret=$?
@@ -1115,6 +1159,7 @@ test_run_ () {
 		test_eval_ "$test_cleanup"
 		teardown_malloc_check
 	fi
+	release_expensive_lock_
 	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
 	then
 		echo ""

---
base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4
change-id: 20261117-ks-ci-long-test-hang-fix-008e72fd40c6
Kaartic Sivaraam· Nov 19, 2026, 17:33 UTC · re: Kaartic Sivaraam · lore

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

On 11/19/26 22:59, Kaartic Sivaraam wrote:
Show 5 quoted lines
> Changes in v2:
> - Corrected commit messages as per Patrick's suggestions
> - Tweaked the shell function comment to avoid mentioning 'prove --jobs'
> - Made the owner and waiting variables local within acquire_expensive_lock_
> > Link to v1: https://patch.msgid.link/20261119-ks-ci-long-test-hang-fix-v1-1-45c2fff6d9f0@gmail.com
I thought b4 would've included it by itself. Anyways, here's the range-diff:
1:  f0664ff4ee ! 1:  fbf773f39a test-lib: run EXPENSIVE tests one at a time
    @@ Metadata
      ## Commit message ##
         test-lib: run EXPENSIVE tests one at a time
    -    Recently, we've acquired quite a few expensive test in our test suite.
    +    Recently, we've acquired quite a few expensive tests in our test suite.
         For instance, these commits add several tests that require
    -    multi-gigabytes:
    +    multi-gigabytes of memory:
           - 9048b48fb (t1600-index: add expensive tests for over-long
             tree paths, 2026-10-16) added tests to t1600
    @@ t/test-lib.sh: fail_117 () {
      	return 117
      }
    -+# EXPENSIVE tests can need many gigabytes of memory or disk, so two of
    -+# them running at the same time under "prove --jobs" can exhaust the
    -+# machine. Allow only one at a time across all test scripts that share
    ++# 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
    @@ t/test-lib.sh: fail_117 () {
     +			rm -rf "$expensive_lock_"
     +			continue
     +		fi
    -+		if test -z "$expensive_lock_waited_"
    ++		if test -z "$waited"
     +		then
     +			say >&3 "waiting for another EXPENSIVE test to finish"
    -+			expensive_lock_waited_=t
    ++			waited=t
     +		fi
     +		sleep 1
     +	done
    -+	expensive_lock_waited_=
     +	echo $$ >"$expensive_lock_/pid"
     +	have_expensive_lock_=t
     +}
-- 
Sivaraam
Junio C Hamano· Nov 20, 2026, 06:28 UTC · re: Kaartic Sivaraam · lore

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

Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:
Show 6 quoted lines
> 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).

Show 5 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).
Show 30 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.

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?

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.

Show 5 quoted lines
> +release_expensive_lock_ () {
> +	test -n "$have_expensive_lock_" || return 0
> +	rm -rf "$expensive_lock_"
> +	have_expensive_lock_=
> +}
Kaartic Sivaraam· Nov 24, 2026, 13:34 UTC · re: Junio C Hamano · lore

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:
Show changes to t/test-lib.sh +11 −4
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
Junio C Hamano· Nov 25, 2026, 20:21 UTC · re: Kaartic Sivaraam · lore

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

Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:
Show 10 quoted lines
> 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.
> ...
> 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.

If we open ourselves to a possible deadlock, then it will be a serious regression. If our locking is leaky or buggy and allows multiple processes to run simultaneously, that is the same as the status quo, so as long as we kill the deadlock this proposed version introduces in the updated version, I will be happy.

Thanks.
Kaartic Sivaraam· Nov 27, 2026, 13:59 UTC · re: Junio C Hamano · lore

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

On 11/26/26 01:51, Junio C Hamano wrote:
Show 17 quoted lines
> Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:
> >> 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.
>> ...
>> 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.
> > If we open ourselves to a possible deadlock, then it will be a serious
> regression.  If our locking is leaky or buggy and allows multiple
> processes to run simultaneously, that is the same as the status quo, so
> as long as we kill the deadlock this proposed version introduces in the
> updated version, I will be happy.
>
Okay. I'll try to send a v3 with another variant of this which tries to avoid deadlocks as much as possible by using a different mechanism for the locking. I suppose we may have to trade-off on the "only one test runs" guarantee as a consequence.
-- 
Sivaraam
Junio C Hamano· Nov 27, 2026, 18:34 UTC · re: Kaartic Sivaraam · lore

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

Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:
> Okay. I'll try to send a v3 with another variant of this which tries to 
> avoid deadlocks as much as possible by using a different mechanism for 
> the locking. I suppose we may have to trade-off on the "only one test 
> runs" guarantee as a consequence.

Yeah, in other words, there is no need for such guarantee. We never deadlock must be a strong guarantee, and it is sufficient if we make sure that most of the time the expensive ones try not to run at the same time.

Thanks.
Harald Nordgren· Nov 25, 2026, 19:09 UTC · re: Kaartic Sivaraam · lore

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

Great idea! Hopefully we can get this merged soon to unbreak the GitHub CI pipeline.

Harald

On Wed, Nov 25, 2026 at 8:08 PM Kaartic Sivaraam <kaartic.sivaraam@gmail.com> wrote:

Show 162 quoted lines
>
> 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
>
>   - 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
>
> We also have the memory hungry 5GB filtered hash-object test in t1007.
>
> When GIT_TEST_LONG is set, "prove --jobs" happily runs several of these
> at once. On the 16GB GitLab CI runners this exhausts memory: the
> sanitizer jobs have been hanging in t1007 until they hit the job
> timeout ever since the new EXPENSIVE tests in t1600 started running
> alongside it. Running the two under a 16GB memory cap reproduces it,
> with the t1007 git process getting OOM-killed.
>
> GitHub CI runners are no exception and observe the same behaviour.
>
> Take a lock shared by all test scripts around each EXPENSIVE test, so
> only one of them runs at a time. The lock is a directory in
> TEST_RESULTS_DIR, created with mkdir(1) like the existing valgrind
> symlink lock. It records the holder's PID so that a lock left behind by
> a script that was killed with SIGKILL gets taken over by the next
> waiter. It is released after the test and its cleanup finish, and from
> the EXIT trap when a script dies or is interrupted while holding it.
>
> Cheap tests in the same scripts keep running in parallel. Only the
> expensive ones wait for each other.
>
> Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
> ---
> In recent times, we've been noticing the sanitizer jobs (linux-leaks,
> linux-reftable-leaks, linux-asan-ubsan) hang and fail with timeout in
> our GitLab CI runners. On some digging, it turns out that this is
> because of multiple memory hungry tests running in parallel.
>
> So, I thought of suggesting this patch to serialize the expensive tests
> at least to avoid the putting the CI runners under memory pressure.
>
> The GitHub CI runners also observed the same behaviour.
>
> Both CI runners are happy with this change.
>
> GitHub: https://github.com/sivaraam/git/actions/runs/43281285339
> GitLab: https://gitlab.com/gitlab-org/git/-/pipelines/3079929506
> ---
> Changes in v2:
> - Corrected commit messages as per Patrick's suggestions
> - Tweaked the shell function comment to avoid mentioning 'prove --jobs'
> - Made the owner and waiting variables local within acquire_expensive_lock_
>
> Link to v1: https://patch.msgid.link/20261119-ks-ci-long-test-hang-fix-v1-1-45c2fff6d9f0@gmail.com
> ---
>  t/README      |  4 +++-
>  t/test-lib.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 48 insertions(+), 1 deletion(-)
>
> diff --git a/t/README b/t/README
> index fd7e8504b5..c156dd3af6 100644
> --- a/t/README
> +++ b/t/README
> @@ -117,7 +117,9 @@ appropriately before running "make". Short options can be bundled, i.e.
>  -l::
>  --long-tests::
>         This causes additional long-running tests to be run (where
> -       available), for more exhaustive testing.
> +       available), for more exhaustive testing. These tests can use a
> +       lot of memory and disk, so only one of them runs at a time, even
> +       when several test scripts run in parallel (e.g. "prove --jobs").
>
>  -r::
>  --run=<test-selector>::
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index c56654b7c5..193bf06aea 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -784,6 +784,7 @@ die () {
>         # test script run with '--immediate' fails, or when the user hits
>         # ctrl-C, i.e. when 'test_done' is not invoked at all.
>         test_atexit_handler || code=$?
> +       release_expensive_lock_
>         if test -n "$GIT_EXIT_OK"
>         then
>                 exit $code
> @@ -1089,6 +1090,43 @@ fail_117 () {
>         return 117
>  }
>
> +# 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
> +}
> +
> +release_expensive_lock_ () {
> +       test -n "$have_expensive_lock_" || return 0
> +       rm -rf "$expensive_lock_"
> +       have_expensive_lock_=
> +}
> +
>  test_run_ () {
>         test_cleanup=:
>         expecting_failure=$2
> @@ -1103,6 +1141,12 @@ test_run_ () {
>                 fi
>         fi
>
> +       case ",$test_prereq," in
> +       *,EXPENSIVE,*)
> +               acquire_expensive_lock_
> +               ;;
> +       esac
> +
>         setup_malloc_check
>         test_eval_ "$1"
>         eval_ret=$?
> @@ -1115,6 +1159,7 @@ test_run_ () {
>                 test_eval_ "$test_cleanup"
>                 teardown_malloc_check
>         fi
> +       release_expensive_lock_
>         if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
>         then
>                 echo ""
>
> ---
> base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4
> change-id: 20261117-ks-ci-long-test-hang-fix-008e72fd40c6
>
>
Kaartic Sivaraam· Nov 27, 2026, 17:23 UTC · re: Kaartic Sivaraam · lore

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

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:

  - 89048b48fb (t1600-index: add expensive tests for over-long
    tree paths, 2026-10-16) added tests to t1600
  - 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
We also have the memory hungry 5GB filtered hash-object test in t1007.

When GIT_TEST_LONG is set, "prove --jobs" happily runs several of these at once. On the 16GB GitLab CI runners this exhausts memory: the sanitizer jobs have been hanging in t1007 until they hit the job timeout ever since the new EXPENSIVE tests in t1600 started running alongside it. Running the two under a 16GB memory cap reproduces it, with the t1007 git process getting OOM-killed.

GitHub CI runners are no exception and observe the same behaviour.

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.

The lock is a file in TEST_RESULTS_DIR holding its owner's PID. A script writes its PID to a file of its own and hard links it into place with ln(1). Like mkdir(1), that is atomic and fails if the lock exists, but the lock never exists without its owner recorded in it.

The lock is released after the test and its cleanup finish, and from the EXIT trap. A lock whose owner is gone was left by a killed script, and a waiter removes it after re-reading it, so that a lock another waiter has just taken is left alone. Two waiters clearing the same stale lock at once may let two expensive tests run together, which is rare and better than hanging. If the lock cannot be created although nobody holds it, e.g. without hard link support, the test runs anyway.

Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
In recent times, we've been noticing the sanitizer jobs (linux-leaks,
linux-reftable-leaks, linux-asan-ubsan) hang and fail with timeout in
our GitLab CI runners. On some digging, it turns out that this is
because of multiple memory hungry tests running in parallel.

So, I thought of suggesting this patch to serialize the expensive tests at least to avoid the putting the CI runners under memory pressure.

The GitHub CI runners also observed the same behaviour.
Both CI runners are happy with this change.
GitHub: https://github.com/sivaraam/git/actions/runs/43281285339
GitLab: https://gitlab.com/gitlab-org/git/-/pipelines/3079929506
---
Changes in v3:
- Fixed issues in the commit message as pointed out by Junio.
- Fixed a race, pointed out in review, where a waiter could remove a
  lock another script had just taken, letting two EXPENSIVE tests run
  at once. A waiter now re-reads the owner and only removes the lock
  if it still names the same dead process.
- Fixed a deadlock where a script killed between creating the lock and
  recording its PID left a lock that nobody would ever remove. The lock
  is now a hard link to a file that already holds the PID, so it never
  exists without an owner.
- A lock that names the waiter's own PID (left by a dead holder whose
  PID was reused) is now treated as stale instead of waited on forever.
- If the lock cannot be created although nobody holds it (no hard
  links, read-only or full disk), the test runs without it instead of
  waiting forever.
- Rewrote the lock part of the commit message for the new scheme.
- Link to v2: https://patch.msgid.link/20261119-ks-ci-long-test-hang-fix-v2-1-37011c08380d@gmail.com
Changes in v2:
- Corrected commit messages as per Patrick's suggestions
- Tweaked the shell function comment to avoid mentioning 'prove --jobs'
- Made the owner and waiting variables local within acquire_expensive_lock_
- Link to v1: https://patch.msgid.link/20261119-ks-ci-long-test-hang-fix-v1-1-45c2fff6d9f0@gmail.com
Range-diff vs. v2
1:  7152a81bdc ! 1:  fd87caa359 test-lib: run EXPENSIVE tests one at a time
    @@ Commit message
         For instance, these commits add several tests that require
         multi-gigabytes of memory:
     
    -      - 9048b48fb (t1600-index: add expensive tests for over-long
    +      - 89048b48fb (t1600-index: add expensive tests for over-long
             tree paths, 2026-10-16) added tests to t1600
     
           - 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
    +        2026-03-25) added tests to t4141
     
         We also have the memory hungry 5GB filtered hash-object test in t1007.
     
    @@ Commit message
         GitHub CI runners are no exception and observe the same behaviour.
     
         Take a lock shared by all test scripts around each EXPENSIVE test, so
    -    only one of them runs at a time. The lock is a directory in
    -    TEST_RESULTS_DIR, created with mkdir(1) like the existing valgrind
    -    symlink lock. It records the holder's PID so that a lock left behind by
    -    a script that was killed with SIGKILL gets taken over by the next
    -    waiter. It is released after the test and its cleanup finish, and from
    -    the EXIT trap when a script dies or is interrupted while holding it.
    +    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.
     
    -    Cheap tests in the same scripts keep running in parallel. Only the
    -    expensive ones wait for each other.
    +    The lock is a file in TEST_RESULTS_DIR holding its owner's PID. A
    +    script writes its PID to a file of its own and hard links it into place
    +    with ln(1). Like mkdir(1), that is atomic and fails if the lock exists,
    +    but the lock never exists without its owner recorded in it.
    +
    +    The lock is released after the test and its cleanup finish, and from
    +    the EXIT trap. A lock whose owner is gone was left by a killed script,
    +    and a waiter removes it after re-reading it, so that a lock another
    +    waiter has just taken is left alone. Two waiters clearing the same
    +    stale lock at once may let two expensive tests run together, which is
    +    rare and better than hanging. If the lock cannot be created although
    +    nobody holds it, e.g. without hard link support, the test runs anyway.
     
         Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
     
    @@ t/README: appropriately before running "make". Short options can be bundled, i.e
      	This causes additional long-running tests to be run (where
     -	available), for more exhaustive testing.
     +	available), for more exhaustive testing. These tests can use a
    -+	lot of memory and disk, so only one of them runs at a time, even
    ++	lot of memory, so only one of them runs at a time, even
     +	when several test scripts run in parallel (e.g. "prove --jobs").
    ++	They take turns through a lock file, "expensive.lock" in the
    ++	test-results directory. A lock left behind by a killed test
    ++	script is cleared by the next one that needs it. If the lock
    ++	cannot be created at all, the test runs anyway.
      
      -r::
      --run=<test-selector>::
    @@ t/test-lib.sh: die () {
      	# ctrl-C, i.e. when 'test_done' is not invoked at all.
      	test_atexit_handler || code=$?
     +	release_expensive_lock_
    ++	test -z "$expensive_lock_" || rm -f "$expensive_lock_.$$"
      	if test -n "$GIT_EXIT_OK"
      	then
      		exit $code
    @@ t/test-lib.sh: fail_117 () {
      	return 117
      }
      
    -+# EXPENSIVE tests may need many gigabytes of memory or disk, so two of
    -+# them running at the same time can exhaust the machine.
    ++# EXPENSIVE tests may need many gigabytes of memory, 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.
    @@ t/test-lib.sh: fail_117 () {
     +have_expensive_lock_=
     +
     +acquire_expensive_lock_ () {
    -+	local owner waited=
    ++	local owner waited= tmp="$expensive_lock_.$$" misses=0
     +	mkdir -p "$TEST_RESULTS_DIR" || return 1
    -+	while ! mkdir "$expensive_lock_" 2>/dev/null
    ++	# 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" || return 1
    ++	while ! ln "$tmp" "$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 -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
    ++			say >&3 "cannot take the EXPENSIVE lock, running anyway"
    ++			rm -f "$tmp"
    ++			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.
    -+			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_" 2>/dev/null)" = "$owner"
    ++			then
    ++				rm -f "$expensive_lock_"
    ++			fi
     +			continue
     +		fi
     +		if test -z "$waited"
    @@ t/test-lib.sh: fail_117 () {
     +		fi
     +		sleep 1
     +	done
    -+	echo $$ >"$expensive_lock_/pid"
     +	have_expensive_lock_=t
    ++	rm -f "$tmp"
     +}
     +
     +release_expensive_lock_ () {
     +	test -n "$have_expensive_lock_" || return 0
    -+	rm -rf "$expensive_lock_"
    ++	rm -f "$expensive_lock_"
     +	have_expensive_lock_=
     +}
     +
---
 t/README      |  8 ++++++-
 t/test-lib.sh | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 1 deletion(-)
Show changes to 2 files +81 −1

t/README, t/test-lib.sh

diff --git a/t/README b/t/README
index fd7e8504b5..824783acfa 100644
--- a/t/README
+++ b/t/README
@@ -117,7 +117,13 @@ appropriately before running "make". Short options can be bundled, i.e.
 -l::
 --long-tests::
 	This causes additional long-running tests to be run (where
-	available), for more exhaustive testing.
+	available), for more exhaustive testing. These tests can use a
+	lot of memory, so only one of them runs at a time, even
+	when several test scripts run in parallel (e.g. "prove --jobs").
+	They take turns through a lock file, "expensive.lock" in the
+	test-results directory. A lock left behind by a killed test
+	script is cleared by the next one that needs it. If the lock
+	cannot be created at all, the test runs anyway.
 
 -r::
 --run=<test-selector>::
diff --git a/t/test-lib.sh b/t/test-lib.sh
index c56654b7c5..916ec2d7d1 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -784,6 +784,8 @@ die () {
 	# test script run with '--immediate' fails, or when the user hits
 	# ctrl-C, i.e. when 'test_done' is not invoked at all.
 	test_atexit_handler || code=$?
+	release_expensive_lock_
+	test -z "$expensive_lock_" || rm -f "$expensive_lock_.$$"
 	if test -n "$GIT_EXIT_OK"
 	then
 		exit $code
@@ -1089,6 +1091,71 @@ fail_117 () {
 	return 117
 }
 
+# EXPENSIVE tests may need many gigabytes of memory, 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= tmp="$expensive_lock_.$$" misses=0
+	mkdir -p "$TEST_RESULTS_DIR" || return 1
+	# 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" || return 1
+	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
+			say >&3 "cannot take the EXPENSIVE lock, running anyway"
+			rm -f "$tmp"
+			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
+			continue
+		fi
+		if test -z "$waited"
+		then
+			say >&3 "waiting for another EXPENSIVE test to finish"
+			waited=t
+		fi
+		sleep 1
+	done
+	have_expensive_lock_=t
+	rm -f "$tmp"
+}
+
+release_expensive_lock_ () {
+	test -n "$have_expensive_lock_" || return 0
+	rm -f "$expensive_lock_"
+	have_expensive_lock_=
+}
+
 test_run_ () {
 	test_cleanup=:
 	expecting_failure=$2
@@ -1103,6 +1170,12 @@ test_run_ () {
 		fi
 	fi
 
+	case ",$test_prereq," in
+	*,EXPENSIVE,*)
+		acquire_expensive_lock_
+		;;
+	esac
+
 	setup_malloc_check
 	test_eval_ "$1"
 	eval_ret=$?
@@ -1115,6 +1188,7 @@ test_run_ () {
 		test_eval_ "$test_cleanup"
 		teardown_malloc_check
 	fi
+	release_expensive_lock_
 	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
 	then
 		echo ""

---
base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe
change-id: 20261117-ks-ci-long-test-hang-fix-008e72fd40c6
Junio C Hamano· Nov 27, 2026, 20:46 UTC · re: Kaartic Sivaraam · lore

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

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:
Either "multi-gigabyte" or "multiple gigabytes"?
> GitHub CI runners are no exception and observe the same behaviour.

Pedantic. You "observe" that runners "experience/suffer from/trigger". They do not observe themselves ;-).

Show 10 quoted lines
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index c56654b7c5..916ec2d7d1 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -784,6 +784,8 @@ die () {
>  	# test script run with '--immediate' fails, or when the user hits
>  	# ctrl-C, i.e. when 'test_done' is not invoked at all.
>  	test_atexit_handler || code=$?
> +	release_expensive_lock_
> +	test -z "$expensive_lock_" || rm -f "$expensive_lock_.$$"

Minor ordering observation. Because "trap die EXIT" is registered right after die(), well before fail_117() where release_expensive_lock_() is defined, an early failure while sourcing test-lib-functions.sh under set -e would enter die() while release_expensive_lock_() is still undefined.

I notice that "$expensive_lock_" already has a test -z guard here, and that would help avoid such issues to some degree. Defining the lock variables and helpers above die() (or guarding the call) would close that small window.

Show 9 quoted lines
> +acquire_expensive_lock_ () {
> +	local owner waited= tmp="$expensive_lock_.$$" misses=0
> +	mkdir -p "$TEST_RESULTS_DIR" || return 1
> +	# 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" || return 1

This pre-cleanup and hard-link scheme is neat. Removing any leftover "$tmp" before writing "$$" keeps us from truncating the inode of a stale lock still hard-linked to an old "$tmp" from a recycled PID, and hard-linking after writing "$$" guarantees $expensive_lock_ never exists empty.

One minor consistency note. When ln(1) fails three times with no lock present, acquire_expensive_lock_() prints "cannot take the EXPENSIVE lock, running anyway" to FD 3, removes "$tmp", and returns 0.

If mkdir or redirecting to "$tmp" fails instead (say, a read-only TEST_RESULTS_DIR or a full disk), it returns 1 silently and leaves an empty "$tmp" behind on write failure until die() runs. Because test_run_() ignores the return value, the test still runs anyway as documented, though sharing the same cleanup, message, and return 0 would keep all fallback paths consistent.

Show 9 quoted lines
> @@ -1103,6 +1170,12 @@ test_run_ () {
>  		fi
>  	fi
>  
> +	case ",$test_prereq," in
> +	*,EXPENSIVE,*)
> +		acquire_expensive_lock_
> +		;;
> +	esac

Because test_expect_success() and test_expect_failure() call test_skip() first, skipped tests never enter test_run_(), and the chainlint dry-run above this block runs before we take the lock. Placing the lock around test_eval_() and outside its set -x block also keeps the polling loop out of -x trace logs under CI --verbose-log -x, while still releasing the lock before _error_exit() on --immediate failures.

Optional thought. A small subtest in t/t0000-basic.sh seeding a stale expensive.lock with a dead PID or "$$" and checking that it is cleared and released (including under --immediate) could give quick regression coverage without any sleep delay.

Overall this is clean and well thought out.
Very nice.
Kaartic Sivaraam· Nov 28, 2026, 08:06 UTC · re: Junio C Hamano · lore

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

On 11/28/26 02:16, Junio C Hamano wrote:
Show 6 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:
> > Either "multi-gigabyte" or "multiple gigabytes"?
>
Good catch. Will fix it.
>> GitHub CI runners are no exception and observe the same behaviour.
> > Pedantic.  You "observe" that runners "experience/suffer from/trigger".
> They do not observe themselves ;-).
> Ah. Thanks for correcting my grammar. Will fix this too :-)
Show 9 quoted lines
> Minor ordering observation.  Because "trap die EXIT" is registered right
> after die(), well before fail_117() where release_expensive_lock_() is
> defined, an early failure while sourcing test-lib-functions.sh under set
> -e would enter die() while release_expensive_lock_() is still undefined.
> > I notice that "$expensive_lock_" already has a test -z guard here, and
> that would help avoid such issues to some degree.  Defining the lock
> variables and helpers above die() (or guarding the call) would close
> that small window.
>
That's an interesting one. I've defined it above die() per your suggestion.
Show 24 quoted lines
>> +acquire_expensive_lock_ () {
>> +	local owner waited= tmp="$expensive_lock_.$$" misses=0
>> +	mkdir -p "$TEST_RESULTS_DIR" || return 1
>> +	# 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" || return 1
> > This pre-cleanup and hard-link scheme is neat.  Removing any leftover
> "$tmp" before writing "$$" keeps us from truncating the inode of a stale
> lock still hard-linked to an old "$tmp" from a recycled PID, and
> hard-linking after writing "$$" guarantees $expensive_lock_ never exists
> empty.
> > One minor consistency note.  When ln(1) fails three times with no lock
> present, acquire_expensive_lock_() prints "cannot take the EXPENSIVE
> lock, running anyway" to FD 3, removes "$tmp", and returns 0.
> > If mkdir or redirecting to "$tmp" fails instead (say, a read-only
> TEST_RESULTS_DIR or a full disk), it returns 1 silently and leaves an
> empty "$tmp" behind on write failure until die() runs.  Because
> test_run_() ignores the return value, the test still runs anyway as
> documented, though sharing the same cleanup, message, and return 0 would
> keep all fallback paths consistent.
> That's a good point. Will make the fallback paths consistent.
Show 19 quoted lines
>> @@ -1103,6 +1170,12 @@ test_run_ () {
>>   		fi
>>   	fi
>>   >> +	case ",$test_prereq," in
>> +	*,EXPENSIVE,*)
>> +		acquire_expensive_lock_
>> +		;;
>> +	esac
> > Because test_expect_success() and test_expect_failure() call test_skip()
> first, skipped tests never enter test_run_(), and the chainlint dry-run
> above this block runs before we take the lock.  Placing the lock around
> test_eval_() and outside its set -x block also keeps the polling loop
> out of -x trace logs under CI --verbose-log -x, while still releasing
> the lock before _error_exit() on --immediate failures.
> > Optional thought.  A small subtest in t/t0000-basic.sh seeding a stale
> expensive.lock with a dead PID or "$$" and checking that it is cleared
> and released (including under --immediate) could give quick regression
> coverage without any sleep delay.
>
Let me give this a shot. I'll keep it in a separate patch so that we could discuss it separately.
> Overall this is clean and well thought out.
> > Very nice.
That's good to hear :-)
-- 
Sivaraam
Kaartic Sivaraam· Nov 28, 2026, 14:57 UTC · re: Kaartic Sivaraam · lore

[PATCH v4 0/2] test-lib: serialize EXPENSIVE tests to avoid OOM in CI

In recent times, we've been noticing the sanitizer jobs (linux-leaks, linux-reftable-leaks, linux-asan-ubsan) hang and fail with timeout in our GitLab CI runners. On some digging, it turns out that this is because of multiple memory hungry tests running in parallel.

So, I thought of suggesting this patch to serialize the expensive tests at least to avoid the putting the CI runners under memory pressure.

The GitHub CI runners also observed the same behaviour.
Both CI runners are happy with this change.
GitHub: https://github.com/sivaraam/git/actions/runs/46125210187/job/140918221115
GitLab: https://gitlab.com/gitlab-org/git/-/pipelines/3126154287
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
Changes in v4:
- Made a few more commit messages tweaks pointed out by Junio.
- Moved release_expensive_lock_ above die() to ensure it is always
  defined before die() is invoked.
- All ways of failing to take the lock now print the same message,
  clean up, and run the test anyway.
- 2/2 is new. It adds a few subtests to ensure the lock cleanup
  happens appropriately.
- Link to v3: https://patch.msgid.link/20261127-ks-ci-long-test-hang-fix-v3-1-91b2aac82a1c@gmail.com
Changes in v3:
- Fixed issues in the commit message as pointed out by Junio.
- Fixed a race, pointed out in review, where a waiter could remove a
  lock another script had just taken, letting two EXPENSIVE tests run
  at once. A waiter now re-reads the owner and only removes the lock
  if it still names the same dead process.
- Fixed a deadlock where a script killed between creating the lock and
  recording its PID left a lock that nobody would ever remove. The lock
  is now a hard link to a file that already holds the PID, so it never
  exists without an owner.
- A lock that names the waiter's own PID (left by a dead holder whose
  PID was reused) is now treated as stale instead of waited on forever.
- If the lock cannot be created although nobody holds it (no hard
  links, read-only or full disk), the test runs without it instead of
  waiting forever.
- Rewrote the lock part of the commit message for the new scheme.
- Link to v2: https://patch.msgid.link/20261119-ks-ci-long-test-hang-fix-v2-1-37011c08380d@gmail.com
Changes in v2:
- Corrected commit messages as per Patrick's suggestions
- Tweaked the shell function comment to avoid mentioning 'prove --jobs'
- Made the owner and waiting variables local within acquire_expensive_lock_
- Link to v1: https://patch.msgid.link/20261119-ks-ci-long-test-hang-fix-v1-1-45c2fff6d9f0@gmail.com
---
Kaartic Sivaraam (2):
      test-lib: run EXPENSIVE tests one at a time
      t0000: test taking over and releasing the EXPENSIVE lock
 t/README         |  8 +++++-
 t/t0000-basic.sh | 48 ++++++++++++++++++++++++++++++++
 t/test-lib.sh    | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+), 1 deletion(-)
Range-diff versus v3:
1:  0d7635ef9f ! 1:  71b765784d test-lib: run EXPENSIVE tests one at a time
    @@ Commit message
     
         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:
    +    multiple gigabytes of memory:
     
           - 89048b48fb (t1600-index: add expensive tests for over-long
             tree paths, 2026-10-16) added tests to t1600
    @@ Commit message
         alongside it. Running the two under a 16GB memory cap reproduces it,
         with the t1007 git process getting OOM-killed.
     
    -    GitHub CI runners are no exception and observe the same behaviour.
    +    GitHub CI runners are no exception and experience the same behaviour.
     
         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
    @@ Commit message
         and a waiter removes it after re-reading it, so that a lock another
         waiter has just taken is left alone. Two waiters clearing the same
         stale lock at once may let two expensive tests run together, which is
    -    rare and better than hanging. If the lock cannot be created although
    -    nobody holds it, e.g. without hard link support, the test runs anyway.
    +    rare and better than hanging. If the lock cannot be taken although
    +    nobody holds it, e.g. without hard link support or on a read-only or
    +    full disk, the test runs anyway.
     
         Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
     
    @@ t/README: appropriately before running "make". Short options can be bundled, i.e
      --run=<test-selector>::
     
      ## t/test-lib.sh ##
    -@@ t/test-lib.sh: die () {
    +@@ t/test-lib.sh: test_missing_prereq=
    + 
    + test_external_has_tap=0
    + 
    ++# 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_=
    ++}
    ++
    + die () {
    + 	code=$?
    + 	# This is responsible for running the atexit commands even when a
      	# test script run with '--immediate' fails, or when the user hits
      	# ctrl-C, i.e. when 'test_done' is not invoked at all.
      	test_atexit_handler || code=$?
    @@ t/test-lib.sh: fail_117 () {
     +expensive_lock_="$TEST_RESULTS_DIR/expensive.lock"
     +have_expensive_lock_=
     +
    ++expensive_lock_unavailable_ () {
    ++	say >&3 "cannot take the EXPENSIVE lock, running anyway"
    ++	rm -f "$expensive_lock_.$$"
    ++}
    ++
     +acquire_expensive_lock_ () {
     +	local owner waited= tmp="$expensive_lock_.$$" misses=0
    -+	mkdir -p "$TEST_RESULTS_DIR" || return 1
    ++	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" || return 1
    ++	echo $$ >"$tmp" ||
    ++	{ expensive_lock_unavailable_; return 0; }
     +	while ! ln "$tmp" "$expensive_lock_" 2>/dev/null
     +	do
     +		if ! test -f "$expensive_lock_"
    @@ t/test-lib.sh: fail_117 () {
     +			# forever.
     +			misses=$((misses + 1))
     +			test $misses -lt 3 && continue
    -+			say >&3 "cannot take the EXPENSIVE lock, running anyway"
    -+			rm -f "$tmp"
    ++			expensive_lock_unavailable_
     +			return 0
     +		fi
     +		misses=0
    @@ t/test-lib.sh: fail_117 () {
     +	have_expensive_lock_=t
     +	rm -f "$tmp"
     +}
    -+
    -+release_expensive_lock_ () {
    -+	test -n "$have_expensive_lock_" || return 0
    -+	rm -f "$expensive_lock_"
    -+	have_expensive_lock_=
    -+}
     +
      test_run_ () {
      	test_cleanup=:
-:  ---------- > 2:  6ca7f04310 t0000: test taking over and releasing the EXPENSIVE lock

--- base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe change-id: 20261117-ks-ci-long-test-hang-fix-008e72fd40c6

Kaartic Sivaraam· Nov 28, 2026, 14:57 UTC · re: Kaartic Sivaraam · lore

[PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

Recently, we've acquired quite a few expensive tests in our test suite. For instance, these commits add several tests that require multiple gigabytes of memory:

  - 89048b48fb (t1600-index: add expensive tests for over-long
    tree paths, 2026-10-16) added tests to t1600
  - 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
We also have the memory hungry 5GB filtered hash-object test in t1007.

When GIT_TEST_LONG is set, "prove --jobs" happily runs several of these at once. On the 16GB GitLab CI runners this exhausts memory: the sanitizer jobs have been hanging in t1007 until they hit the job timeout ever since the new EXPENSIVE tests in t1600 started running alongside it. Running the two under a 16GB memory cap reproduces it, with the t1007 git process getting OOM-killed.

GitHub CI runners are no exception and experience the same behaviour.

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.

The lock is a file in TEST_RESULTS_DIR holding its owner's PID. A script writes its PID to a file of its own and hard links it into place with ln(1). Like mkdir(1), that is atomic and fails if the lock exists, but the lock never exists without its owner recorded in it.

The lock is released after the test and its cleanup finish, and from the EXIT trap. A lock whose owner is gone was left by a killed script, and a waiter removes it after re-reading it, so that a lock another waiter has just taken is left alone. Two waiters clearing the same stale lock at once may let two expensive tests run together, which is rare and better than hanging. If the lock cannot be taken although nobody holds it, e.g. without hard link support or on a read-only or full disk, the test runs anyway.

Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
 t/README      |  8 +++++-
 t/test-lib.sh | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 1 deletion(-)
Show changes to 2 files +90 −1

t/README, t/test-lib.sh

diff --git a/t/README b/t/README
index fd7e8504b5..824783acfa 100644
--- a/t/README
+++ b/t/README
@@ -117,7 +117,13 @@ appropriately before running "make". Short options can be bundled, i.e.
 -l::
 --long-tests::
 	This causes additional long-running tests to be run (where
-	available), for more exhaustive testing.
+	available), for more exhaustive testing. These tests can use a
+	lot of memory, so only one of them runs at a time, even
+	when several test scripts run in parallel (e.g. "prove --jobs").
+	They take turns through a lock file, "expensive.lock" in the
+	test-results directory. A lock left behind by a killed test
+	script is cleared by the next one that needs it. If the lock
+	cannot be created at all, the test runs anyway.
 
 -r::
 --run=<test-selector>::
diff --git a/t/test-lib.sh b/t/test-lib.sh
index c56654b7c5..bfd2fc8374 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -778,12 +778,23 @@ test_missing_prereq=
 
 test_external_has_tap=0
 
+# 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_=
+}
+
 die () {
 	code=$?
 	# This is responsible for running the atexit commands even when a
 	# test script run with '--immediate' fails, or when the user hits
 	# ctrl-C, i.e. when 'test_done' is not invoked at all.
 	test_atexit_handler || code=$?
+	release_expensive_lock_
+	test -z "$expensive_lock_" || rm -f "$expensive_lock_.$$"
 	if test -n "$GIT_EXIT_OK"
 	then
 		exit $code
@@ -1089,6 +1100,71 @@ fail_117 () {
 	return 117
 }
 
+# EXPENSIVE tests may need many gigabytes of memory, 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_=
+
+expensive_lock_unavailable_ () {
+	say >&3 "cannot take the EXPENSIVE lock, running anyway"
+	rm -f "$expensive_lock_.$$"
+}
+
+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
+			continue
+		fi
+		if test -z "$waited"
+		then
+			say >&3 "waiting for another EXPENSIVE test to finish"
+			waited=t
+		fi
+		sleep 1
+	done
+	have_expensive_lock_=t
+	rm -f "$tmp"
+}
+
 test_run_ () {
 	test_cleanup=:
 	expecting_failure=$2
@@ -1103,6 +1179,12 @@ test_run_ () {
 		fi
 	fi
 
+	case ",$test_prereq," in
+	*,EXPENSIVE,*)
+		acquire_expensive_lock_
+		;;
+	esac
+
 	setup_malloc_check
 	test_eval_ "$1"
 	eval_ret=$?
@@ -1115,6 +1197,7 @@ test_run_ () {
 		test_eval_ "$test_cleanup"
 		teardown_malloc_check
 	fi
+	release_expensive_lock_
 	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
 	then
 		echo ""
-- 
2.56.1.911.g883d8bcb20
Junio C Hamano· Nov 29, 2026, 16:55 UTC · re: Kaartic Sivaraam · lore

Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:
Show 13 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"
> +	echo $$ >"$tmp" ||
> +	{ expensive_lock_unavailable_; return 0; }
> +	while ! ln "$tmp" "$expensive_lock_" 2>/dev/null
> +	do

Neat-o. We can try to hardlink ours, fail, notice the owner is gone, unlink it, and try again. Another waiter may interfere while we are doing so and take the lock, in which case we will fail to hardlink and wait one more time, which isn't too bad.

Show 38 quoted lines
> +		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
> +			continue
> +		fi
> +		if test -z "$waited"
> +		then
> +			say >&3 "waiting for another EXPENSIVE test to finish"
> +			waited=t
> +		fi
> +		sleep 1
> +	done
> +	have_expensive_lock_=t
> +	rm -f "$tmp"
> +}
Very nicely done.
Christian Couder· Nov 30, 2026, 14:13 UTC · re: Kaartic Sivaraam · lore

Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

On Sat, Nov 28, 2026 at 3:59 PM Kaartic Sivaraam <kaartic.sivaraam@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.

Show 8 quoted lines
> +# 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.

[...]
Show 39 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"
> +       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.

Show 5 quoted lines
> +                       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".

Show 7 quoted lines
> +                       waited=t
> +               fi
> +               sleep 1
> +       done
> +       have_expensive_lock_=t
> +       rm -f "$tmp"
> +}
Thanks.
Kaartic Sivaraam· Dec 1, 2026, 10:14 UTC · re: Christian Couder · lore

Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

On 11/30/26 19:43, Christian Couder wrote:
Show 33 quoted lines
> >> +# 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.
> Interesting. That seems sensible. Will include this.
Show 17 quoted lines
>> +               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:
>
Indeed.
Show 9 quoted lines
>         # 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.
> Makes sense. Will include this too.
Show 11 quoted lines
>> +                       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.
> Indeed. But I kind of assumed anyone wanting more diagnostics would run with '-v' anyways.
Show 16 quoted lines
> 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".
> Okay. I guess sending to stderr doesn't do us much harm either. I'll prefix it with a '#' to ensure it doesn't interfere with TAP.
-- 
Sivaraam
Christian Couder· Dec 1, 2026, 11:10 UTC · re: Kaartic Sivaraam · lore

Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

On Tue, Dec 1, 2026 at 11:14 AM Kaartic Sivaraam <kaartic.sivaraam@gmail.com> wrote:

Show 15 quoted lines
> >> +                       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.
>
> Indeed. But I kind of assumed anyone wanting more diagnostics would run
> with '-v' anyways.

People might not know they need diagnostics as a script waiting on the lock looks just like a slow EXPENSIVE test. And, when using `-v`, the output is around 20 times (60 times if `-x` is also used) more verbose than without it, so there are good reasons not to use `-v`.

Also under `prove`, `-v` is not even allowed ("verbose mode forbidden under TAP harness; try --verbose-log"), and not sure a lot of people use `--verbose-log` with it.

Show 22 quoted lines
> > 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".
> >
>
> Okay. I guess sending to stderr doesn't do us much harm either. I'll
> prefix it with a '#' to ensure it doesn't interfere with TAP.

Great, but don't TAP parsers only read stdout? If sending to stderr, a '#' might not be needed.

Anyway thanks for working on this.
Kaartic Sivaraam· Dec 1, 2026, 12:34 UTC · re: Christian Couder · lore

Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

On 12/1/26 16:40, Christian Couder wrote:
Show 8 quoted lines
> > People might not know they need diagnostics as a script waiting on the
> lock looks just like a slow EXPENSIVE test. And, when using `-v`, the
> output is around 20 times (60 times if `-x` is also used) more verbose
> than without it, so there are good reasons not to use `-v`.
> > Also under `prove`, `-v` is not even allowed ("verbose mode forbidden
> under TAP harness; try --verbose-log"), and not sure a lot of people
> use `--verbose-log` with it.
> Got it.
Show 25 quoted lines
>>> 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".
>>>
>>
>> Okay. I guess sending to stderr doesn't do us much harm either. I'll
>> prefix it with a '#' to ensure it doesn't interfere with TAP.
> > Great, but don't TAP parsers only read stdout? If sending to stderr, a
> '#' might not be needed.
>
TAP parsers do read only stdout, but with --tee or --verbose-log test-lib merges the script's stderr into stdout, so these lines reach the TAP stream. That's also why the existing -x warning in t/test-lib.sh has the '#'. It keeps them valid TAP comments.
-- 
Sivaraam
Patrick Steinhardt· Dec 1, 2026, 07:22 UTC · re: Kaartic Sivaraam · lore

Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

On Sat, Nov 28, 2026 at 08:27:59PM +0530, Kaartic Sivaraam wrote:
Show 30 quoted lines
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index c56654b7c5..bfd2fc8374 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -1089,6 +1100,71 @@ fail_117 () {
>  	return 117
>  }
>  
> +# EXPENSIVE tests may need many gigabytes of memory, 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_=
> +
> +expensive_lock_unavailable_ () {
> +	say >&3 "cannot take the EXPENSIVE lock, running anyway"
> +	rm -f "$expensive_lock_.$$"
> +}
> +
> +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?
> +	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.
Show 13 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.

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

Show 5 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?

Thanks!
Patrick
Kaartic Sivaraam· Dec 1, 2026, 08:47 UTC · re: Patrick Steinhardt · lore

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
Patrick Steinhardt· Dec 1, 2026, 11:07 UTC · re: Kaartic Sivaraam · lore

Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

On Tue, Dec 01, 2026 at 02:17:48PM +0530, Kaartic Sivaraam wrote:
Show 20 quoted lines
> On 12/1/26 12:52, Patrick Steinhardt wrote:
> > > +
> > > +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.
Oh, the "-f" should be kept indeed. But shouldn't it be `2>/dev/null`?

Ah, no, rm(1) doesn't actually complain in case the target path does not exist. I thought it did. One week off and I've already forgotten everything.

Show 51 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:
> 
> >  * 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.

But the race isn't fully fixed, is it? You have simply narrowed the race window, but the race itself still exists between `test $(cat "$expensive_lock_" = "$owner")` and the `rm -f`, doesn't it?

Patrick
Kaartic Sivaraam· Dec 1, 2026, 12:50 UTC · re: Patrick Steinhardt · lore

Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

On 12/1/26 16:37, Patrick Steinhardt wrote:
Show 26 quoted lines
> On Tue, Dec 01, 2026 at 02:17:48PM +0530, Kaartic Sivaraam wrote:
>> On 12/1/26 12:52, Patrick Steinhardt wrote:
>>>> +
>>>> +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.
> > Oh, the "-f" should be kept indeed. But shouldn't it be `2>/dev/null`?
> > Ah, no, rm(1) doesn't actually complain in case the target path does not
> exist. I thought it did. One week off and I've already forgotten
> everything.
>
I guess you're just warming up from the time off ;-)
Show 39 quoted lines
>>> 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:
>>
>>>   * 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.
> > But the race isn't fully fixed, is it? You have simply narrowed the race
> window, but the race itself still exists between `test $(cat
> "$expensive_lock_" = "$owner")` and the `rm -f`, doesn't it?
>
Ah. That is true indeed. Though, without the re-check, makes overlaps after a SIGKILL more common: in a small stress test they went from about 3 per run to about 15. So, I kind of lean towards keeping it since it doesn't cost us much.
-- 
Sivaraam
Patrick Steinhardt· Dec 1, 2026, 13:19 UTC · re: Kaartic Sivaraam · lore

Re: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time

On Tue, Dec 01, 2026 at 06:20:52PM +0530, Kaartic Sivaraam wrote:
Show 48 quoted lines
> On 12/1/26 16:37, Patrick Steinhardt wrote:
> > On Tue, Dec 01, 2026 at 02:17:48PM +0530, Kaartic Sivaraam wrote:
> > > On 12/1/26 12:52, Patrick Steinhardt wrote:
> > > > 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:
> > > 
> > > >   * 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.
> > 
> > But the race isn't fully fixed, is it? You have simply narrowed the race
> > window, but the race itself still exists between `test $(cat
> > "$expensive_lock_" = "$owner")` and the `rm -f`, doesn't it?
> > 
> 
> Ah. That is true indeed. Though, without the re-check, makes overlaps after
> a SIGKILL more common: in a small stress test they went from about 3 per run
> to about 15. So, I kind of lean towards keeping it since it doesn't cost us
> much.

That's fine with me, but we should at least add a comment there that mentions that this is _not_ a full fix for the race, and why that is acceptable.

Patrick
Kaartic Sivaraam· Nov 28, 2026, 14:58 UTC · re: Kaartic Sivaraam · lore

[PATCH v4 2/2] t0000: test taking over and releasing the EXPENSIVE lock

The previous commit serializes EXPENSIVE tests with a lock that a waiter takes over when its holder was killed, and that the EXIT trap releases when a script dies while holding it.

Add subtests checking that:
  - a lock left by a dead process is taken over, held while the test
    runs, and released afterwards;
  - a lock carrying the script's own PID, which can only be left by a
    dead holder whose PID was reused, is taken over;
  - a script exiting in the middle of an EXPENSIVE test does not leave
    the lock behind.
None of them has to wait, as a stale lock is cleared without polling.
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
 t/t0000-basic.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
Show changes to t/t0000-basic.sh +48 −0
diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
index 7bd1f4e7b7..1535b7aa7e 100755
--- a/t/t0000-basic.sh
+++ b/t/t0000-basic.sh
@@ -746,6 +746,54 @@ test_expect_success 'subtest: lazy prereqs do not turn off tracing' '
 	test_grep "echo trace" lazy-prereq-and-tracing/err
 '
 
+test_expect_success 'subtest: EXPENSIVE lock of a dead holder is taken over' '
+	write_sub_test_lib_test expensive-lock-dead <<-\EOF &&
+	test_expect_success EXPENSIVE "holds the lock" "
+		echo $$ >expect &&
+		test_cmp expect \"$TEST_RESULTS_DIR/expensive.lock\"
+	"
+	test_done
+	EOF
+	mkdir expensive-lock-dead/test-results &&
+	sh -c "echo \$\$" >expensive-lock-dead/test-results/expensive.lock &&
+	run_sub_test_lib_test expensive-lock-dead --long &&
+	check_sub_test_lib_test expensive-lock-dead <<-\EOF &&
+	> ok 1 - holds the lock
+	> # passed all 1 test(s)
+	> 1..1
+	EOF
+	test_path_is_missing expensive-lock-dead/test-results/expensive.lock
+'
+
+test_expect_success 'subtest: EXPENSIVE lock carrying our own PID is taken over' '
+	write_sub_test_lib_test expensive-lock-own-pid <<-\EOF &&
+	mkdir -p "$TEST_RESULTS_DIR" &&
+	echo $$ >"$TEST_RESULTS_DIR/expensive.lock"
+	test_expect_success EXPENSIVE "takes the lock" "true"
+	test_done
+	EOF
+	run_sub_test_lib_test expensive-lock-own-pid --long &&
+	check_sub_test_lib_test expensive-lock-own-pid <<-\EOF &&
+	> ok 1 - takes the lock
+	> # passed all 1 test(s)
+	> 1..1
+	EOF
+	test_path_is_missing expensive-lock-own-pid/test-results/expensive.lock
+'
+
+test_expect_success 'subtest: EXPENSIVE lock is released when exiting mid-test' '
+	write_sub_test_lib_test expensive-lock-exit <<-\EOF &&
+	test_expect_success EXPENSIVE "exits holding the lock" "
+		test_path_is_file \"$TEST_RESULTS_DIR/expensive.lock\" &&
+		exit 1
+	"
+	test_done
+	EOF
+	run_sub_test_lib_test_err expensive-lock-exit --long &&
+	test_grep "FATAL: Unexpected exit" expensive-lock-exit/out &&
+	test_path_is_missing expensive-lock-exit/test-results/expensive.lock
+'
+
 test_expect_success 'subtest: tests clean up after themselves' '
 	write_and_run_sub_test_lib_test cleanup <<-\EOF &&
 	clean=no
-- 
2.56.1.911.g883d8bcb20
Patrick Steinhardt· Dec 1, 2026, 07:23 UTC · re: Kaartic Sivaraam · lore

Re: [PATCH v4 2/2] t0000: test taking over and releasing the EXPENSIVE lock

On Sat, Nov 28, 2026 at 08:28:00PM +0530, Kaartic Sivaraam wrote:
Show 16 quoted lines
> The previous commit serializes EXPENSIVE tests with a lock that a
> waiter takes over when its holder was killed, and that the EXIT trap
> releases when a script dies while holding it.
> 
> Add subtests checking that:
> 
>   - a lock left by a dead process is taken over, held while the test
>     runs, and released afterwards;
> 
>   - a lock carrying the script's own PID, which can only be left by a
>     dead holder whose PID was reused, is taken over;
> 
>   - a script exiting in the middle of an EXPENSIVE test does not leave
>     the lock behind.
> 
> None of them has to wait, as a stale lock is cleared without polling.

I would have folded this into the preceding commit, so that it's easier for the reader to connect expected behaviour with the new logic that you're adding.

No strong blocker though from my side.
Patrick
Kaartic Sivaraam· Dec 1, 2026, 08:49 UTC · re: Patrick Steinhardt · lore

Re: [PATCH v4 2/2] t0000: test taking over and releasing the EXPENSIVE lock

On 12/1/26 12:53, Patrick Steinhardt wrote:
Show 21 quoted lines
> On Sat, Nov 28, 2026 at 08:28:00PM +0530, Kaartic Sivaraam wrote:
>> The previous commit serializes EXPENSIVE tests with a lock that a
>> waiter takes over when its holder was killed, and that the EXIT trap
>> releases when a script dies while holding it.
>>
>> Add subtests checking that:
>>
>>    - a lock left by a dead process is taken over, held while the test
>>      runs, and released afterwards;
>>
>>    - a lock carrying the script's own PID, which can only be left by a
>>      dead holder whose PID was reused, is taken over;
>>
>>    - a script exiting in the middle of an EXPENSIVE test does not leave
>>      the lock behind.
>>
>> None of them has to wait, as a stale lock is cleared without polling.
> > I would have folded this into the preceding commit, so that it's easier
> for the reader to connect expected behaviour with the new logic that
> you're adding.
> I mostly kept it separate to avoid blocking the main patch in case we get into some side discussion related to this test patch. I haven't seen much thoughts / suggestions on this. So, I think I can squash it in now.
-- 
Sivaraam
Kaartic Sivaraam· Dec 1, 2026, 19:53 UTC · re: Kaartic Sivaraam · lore

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

Recently, we've acquired quite a few expensive tests in our test suite. For instance, these commits add several tests that require multiple gigabytes of memory:

  - 89048b48fb (t1600-index: add expensive tests for over-long
    tree paths, 2026-10-16) added tests to t1600
  - 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
We also have the memory hungry 5GB filtered hash-object test in t1007.

When GIT_TEST_LONG is set, "prove --jobs" happily runs several of these at once. On the 16GB GitLab CI runners this exhausts memory: the sanitizer jobs have been hanging in t1007 until they hit the job timeout ever since the new EXPENSIVE tests in t1600 started running alongside it. Running the two under a 16GB memory cap reproduces it, with the t1007 git process getting OOM-killed.

GitHub CI runners are no exception and experience the same behaviour.

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.

The lock is a file in TEST_RESULTS_DIR holding its owner's PID. A script writes its PID to a file of its own and hard links it into place with ln(1). Like mkdir(1), that is atomic and fails if the lock exists, but the lock never exists without its owner recorded in it.

The lock is released after the test and its cleanup finish, and from the EXIT trap. A lock whose owner is gone was left by a killed script, and a waiter removes it after re-reading it, so that a lock another waiter has just taken is left alone. Two waiters clearing the same stale lock at once may let two expensive tests run together, which is rare and better than hanging. If the lock cannot be taken although nobody holds it, e.g. without hard link support or on a read-only or full disk, the test runs anyway.

We add a few subtests to t0000 to ensure that the lock is taken over in scenarios where it is not cleared up normally.

Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
In recent times, we've been noticing the sanitizer jobs (linux-leaks,
linux-reftable-leaks, linux-asan-ubsan) hang and fail with timeout in
our GitLab CI runners. On some digging, it turns out that this is
because of multiple memory hungry tests running in parallel.

So, I thought of suggesting this patch to serialize the expensive tests at least to avoid the putting the CI runners under memory pressure.

The GitHub CI runners also observed the same behaviour.
Both CI runners are happy with this change.
GitHub: https://github.com/sivaraam/git/actions/runs/46125210187/job/140918221115
GitLab: https://gitlab.com/gitlab-org/git/-/pipelines/3139440521
(note: The GitHub CI run is from an older iteration. The GitLab CI run 
 is with the current patch and the following fix included to make CI green:
 https://lore.kernel.org/git/pull.2342.git.1796049772272.gitgitgadget@gmail.com/ )
---
Changes in v5:
- Introduce remove_expensive_lock_owned_by_ function as suggested by
  Christian to reduce the unintentional removal of the lock-file.
- Redirect the messages to stderr and format them as comments so TAP
  ignores them in cases where we redirect stderr to stdout.
- Sprinkle acquire_expensive_lock_ with a few newlines to hopefully
  improve the readability of it.
- Acknowledge in the comment that the owner recheck isn't a complete
  fix for the race it tries to address.
- Squashed the two patches into one as the tests introduced in 2/2
  weren't as controversial as expected.
- Link to v4: https://patch.msgid.link/20261128-ks-ci-long-test-hang-fix-v4-0-f117badd2e13@gmail.com
Changes in v4:
- Made a few more commit messages tweaks pointed out by Junio.
- Moved release_expensive_lock_ above die() to ensure it is always
  defined before die() is invoked.
- All ways of failing to take the lock now print the same message,
  clean up, and run the test anyway.
- 2/2 is new. It adds a few subtests to ensure the lock cleanup
  happens appropriately.
- Link to v3: https://patch.msgid.link/20261127-ks-ci-long-test-hang-fix-v3-1-91b2aac82a1c@gmail.com
Changes in v3:
- Fixed issues in the commit message as pointed out by Junio.
- Fixed a race, pointed out in review, where a waiter could remove a
  lock another script had just taken, letting two EXPENSIVE tests run
  at once. A waiter now re-reads the owner and only removes the lock
  if it still names the same dead process.
- Fixed a deadlock where a script killed between creating the lock and
  recording its PID left a lock that nobody would ever remove. The lock
  is now a hard link to a file that already holds the PID, so it never
  exists without an owner.
- A lock that names the waiter's own PID (left by a dead holder whose
  PID was reused) is now treated as stale instead of waited on forever.
- If the lock cannot be created although nobody holds it (no hard
  links, read-only or full disk), the test runs without it instead of
  waiting forever.
- Rewrote the lock part of the commit message for the new scheme.
- Link to v2: https://patch.msgid.link/20261119-ks-ci-long-test-hang-fix-v2-1-37011c08380d@gmail.com
Changes in v2:
- Corrected commit messages as per Patrick's suggestions
- Tweaked the shell function comment to avoid mentioning 'prove --jobs'
- Made the owner and waiting variables local within acquire_expensive_lock_
- Link to v1: https://patch.msgid.link/20261119-ks-ci-long-test-hang-fix-v1-1-45c2fff6d9f0@gmail.com
Range-diff vs. v4:
1:  ad8fa8d872 ! 1:  2fcdeefff3 test-lib: run EXPENSIVE tests one at a time
    @@ Commit message
         nobody holds it, e.g. without hard link support or on a read-only or
         full disk, the test runs anyway.
    +    We add a few subtests to t0000 to ensure that the lock is taken over
    +    in scenarios where it is not cleared up normally.
    +
         Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
      ## t/README ##
    @@ t/README: appropriately before running "make". Short options can be bundled, i.e
      -r::
      --run=<test-selector>::
    + ## t/t0000-basic.sh ##
    +@@ t/t0000-basic.sh: test_expect_success 'subtest: lazy prereqs do not turn off tracing' '
    + 	test_grep "echo trace" lazy-prereq-and-tracing/err
    + '
    +
    ++test_expect_success 'subtest: EXPENSIVE lock of a dead holder is taken over' '
    ++	write_sub_test_lib_test expensive-lock-dead <<-\EOF &&
    ++	test_expect_success EXPENSIVE "holds the lock" "
    ++		echo $$ >expect &&
    ++		test_cmp expect \"$TEST_RESULTS_DIR/expensive.lock\"
    ++	"
    ++	test_done
    ++	EOF
    ++	mkdir expensive-lock-dead/test-results &&
    ++	pid=$(sh -c "echo \$\$") &&
    ++	echo $pid >expensive-lock-dead/test-results/expensive.lock &&
    ++	run_sub_test_lib_test expensive-lock-dead --long &&
    ++	check_sub_test_lib_test_err expensive-lock-dead \
    ++		<<-\EOF_OUT 3<<-EOF_ERR &&
    ++	> ok 1 - holds the lock
    ++	> # passed all 1 test(s)
    ++	> 1..1
    ++	EOF_OUT
    ++	> # removing stale EXPENSIVE lock left by PID $pid
    ++	EOF_ERR
    ++	test_path_is_missing expensive-lock-dead/test-results/expensive.lock
    ++'
    ++
    ++test_expect_success 'subtest: EXPENSIVE lock carrying our own PID is taken over' '
    ++	write_sub_test_lib_test expensive-lock-own-pid <<-\EOF &&
    ++	mkdir -p "$TEST_RESULTS_DIR" &&
    ++	echo $$ >"$TEST_RESULTS_DIR/expensive.lock" &&
    ++	echo $$ >"$TEST_RESULTS_DIR/pid"
    ++	test_expect_success EXPENSIVE "takes the lock" "true"
    ++	test_done
    ++	EOF
    ++	run_sub_test_lib_test expensive-lock-own-pid --long &&
    ++	pid=$(cat expensive-lock-own-pid/test-results/pid) &&
    ++	check_sub_test_lib_test_err expensive-lock-own-pid \
    ++		<<-\EOF_OUT 3<<-EOF_ERR &&
    ++	> ok 1 - takes the lock
    ++	> # passed all 1 test(s)
    ++	> 1..1
    ++	EOF_OUT
    ++	> # removing stale EXPENSIVE lock left by PID $pid
    ++	EOF_ERR
    ++	test_path_is_missing expensive-lock-own-pid/test-results/expensive.lock
    ++'
    ++
    ++test_expect_success 'subtest: EXPENSIVE lock is released when exiting mid-test' '
    ++	write_sub_test_lib_test expensive-lock-exit <<-\EOF &&
    ++	test_expect_success EXPENSIVE "exits holding the lock" "
    ++		test_path_is_file \"$TEST_RESULTS_DIR/expensive.lock\" &&
    ++		exit 1
    ++	"
    ++	test_done
    ++	EOF
    ++	run_sub_test_lib_test_err expensive-lock-exit --long &&
    ++	test_grep "FATAL: Unexpected exit" expensive-lock-exit/out &&
    ++	test_path_is_missing expensive-lock-exit/test-results/expensive.lock
    ++'
    ++
    + test_expect_success 'subtest: tests clean up after themselves' '
    + 	write_and_run_sub_test_lib_test cleanup <<-\EOF &&
    + 	clean=no
    +
      ## t/test-lib.sh ##
     @@ t/test-lib.sh: test_missing_prereq=
      test_external_has_tap=0
    ++# 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_"
    ++}
    ++
     +# 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_"
    ++	remove_expensive_lock_owned_by_ $$
     +	have_expensive_lock_=
     +}
     +
    @@ t/test-lib.sh: fail_117 () {
     +have_expensive_lock_=
     +
     +expensive_lock_unavailable_ () {
    -+	say >&3 "cannot take the EXPENSIVE lock, running anyway"
    ++	echo >&2 "# cannot take the EXPENSIVE lock, running anyway"
     +	rm -f "$expensive_lock_.$$"
     +}
     +
     +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
    @@ t/test-lib.sh: fail_117 () {
     +	rm -f "$tmp"
     +	echo $$ >"$tmp" ||
     +	{ expensive_lock_unavailable_; return 0; }
    ++
     +	while ! ln "$tmp" "$expensive_lock_" 2>/dev/null
     +	do
     +		if ! test -f "$expensive_lock_"
    @@ t/test-lib.sh: fail_117 () {
     +			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.
    @@ t/test-lib.sh: fail_117 () {
     +			#
     +			# 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"
    ++			#
    ++			# Rarely, after a holder is killed, two waiters can
    ++			# both get past this check and run together.
    ++			if remove_expensive_lock_owned_by_ "$owner"
     +			then
    -+				rm -f "$expensive_lock_"
    ++				echo >&2 "# removing stale EXPENSIVE lock left by PID $owner"
     +			fi
     +			continue
     +		fi
    ++
     +		if test -z "$waited"
     +		then
    -+			say >&3 "waiting for another EXPENSIVE test to finish"
    ++			echo >&2 "# waiting for EXPENSIVE lock held by PID $owner"
     +			waited=t
     +		fi
    ++
     +		sleep 1
     +	done
    ++
     +	have_expensive_lock_=t
     +	rm -f "$tmp"
     +}
2:  d5216ec6f8 < -:  ---------- t0000: test taking over and releasing the EXPENSIVE lock
---
 t/README         |   8 ++++-
 t/t0000-basic.sh |  57 +++++++++++++++++++++++++++++++
 t/test-lib.sh    | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 165 insertions(+), 1 deletion(-)
Show changes to 3 files +165 −1

t/README, t/t0000-basic.sh, t/test-lib.sh

diff --git a/t/README b/t/README
index fd7e8504b5..824783acfa 100644
--- a/t/README
+++ b/t/README
@@ -117,7 +117,13 @@ appropriately before running "make". Short options can be bundled, i.e.
 -l::
 --long-tests::
 	This causes additional long-running tests to be run (where
-	available), for more exhaustive testing.
+	available), for more exhaustive testing. These tests can use a
+	lot of memory, so only one of them runs at a time, even
+	when several test scripts run in parallel (e.g. "prove --jobs").
+	They take turns through a lock file, "expensive.lock" in the
+	test-results directory. A lock left behind by a killed test
+	script is cleared by the next one that needs it. If the lock
+	cannot be created at all, the test runs anyway.
 
 -r::
 --run=<test-selector>::
diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
index 7bd1f4e7b7..c78805e9a2 100755
--- a/t/t0000-basic.sh
+++ b/t/t0000-basic.sh
@@ -746,6 +746,63 @@ test_expect_success 'subtest: lazy prereqs do not turn off tracing' '
 	test_grep "echo trace" lazy-prereq-and-tracing/err
 '
 
+test_expect_success 'subtest: EXPENSIVE lock of a dead holder is taken over' '
+	write_sub_test_lib_test expensive-lock-dead <<-\EOF &&
+	test_expect_success EXPENSIVE "holds the lock" "
+		echo $$ >expect &&
+		test_cmp expect \"$TEST_RESULTS_DIR/expensive.lock\"
+	"
+	test_done
+	EOF
+	mkdir expensive-lock-dead/test-results &&
+	pid=$(sh -c "echo \$\$") &&
+	echo $pid >expensive-lock-dead/test-results/expensive.lock &&
+	run_sub_test_lib_test expensive-lock-dead --long &&
+	check_sub_test_lib_test_err expensive-lock-dead \
+		<<-\EOF_OUT 3<<-EOF_ERR &&
+	> ok 1 - holds the lock
+	> # passed all 1 test(s)
+	> 1..1
+	EOF_OUT
+	> # removing stale EXPENSIVE lock left by PID $pid
+	EOF_ERR
+	test_path_is_missing expensive-lock-dead/test-results/expensive.lock
+'
+
+test_expect_success 'subtest: EXPENSIVE lock carrying our own PID is taken over' '
+	write_sub_test_lib_test expensive-lock-own-pid <<-\EOF &&
+	mkdir -p "$TEST_RESULTS_DIR" &&
+	echo $$ >"$TEST_RESULTS_DIR/expensive.lock" &&
+	echo $$ >"$TEST_RESULTS_DIR/pid"
+	test_expect_success EXPENSIVE "takes the lock" "true"
+	test_done
+	EOF
+	run_sub_test_lib_test expensive-lock-own-pid --long &&
+	pid=$(cat expensive-lock-own-pid/test-results/pid) &&
+	check_sub_test_lib_test_err expensive-lock-own-pid \
+		<<-\EOF_OUT 3<<-EOF_ERR &&
+	> ok 1 - takes the lock
+	> # passed all 1 test(s)
+	> 1..1
+	EOF_OUT
+	> # removing stale EXPENSIVE lock left by PID $pid
+	EOF_ERR
+	test_path_is_missing expensive-lock-own-pid/test-results/expensive.lock
+'
+
+test_expect_success 'subtest: EXPENSIVE lock is released when exiting mid-test' '
+	write_sub_test_lib_test expensive-lock-exit <<-\EOF &&
+	test_expect_success EXPENSIVE "exits holding the lock" "
+		test_path_is_file \"$TEST_RESULTS_DIR/expensive.lock\" &&
+		exit 1
+	"
+	test_done
+	EOF
+	run_sub_test_lib_test_err expensive-lock-exit --long &&
+	test_grep "FATAL: Unexpected exit" expensive-lock-exit/out &&
+	test_path_is_missing expensive-lock-exit/test-results/expensive.lock
+'
+
 test_expect_success 'subtest: tests clean up after themselves' '
 	write_and_run_sub_test_lib_test cleanup <<-\EOF &&
 	clean=no
diff --git a/t/test-lib.sh b/t/test-lib.sh
index c56654b7c5..c332e50ec0 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -778,12 +778,30 @@ test_missing_prereq=
 
 test_external_has_tap=0
 
+# 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_"
+}
+
+# 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
+	remove_expensive_lock_owned_by_ $$
+	have_expensive_lock_=
+}
+
 die () {
 	code=$?
 	# This is responsible for running the atexit commands even when a
 	# test script run with '--immediate' fails, or when the user hits
 	# ctrl-C, i.e. when 'test_done' is not invoked at all.
 	test_atexit_handler || code=$?
+	release_expensive_lock_
+	test -z "$expensive_lock_" || rm -f "$expensive_lock_.$$"
 	if test -n "$GIT_EXIT_OK"
 	then
 		exit $code
@@ -1089,6 +1107,82 @@ fail_117 () {
 	return 117
 }
 
+# EXPENSIVE tests may need many gigabytes of memory, 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_=
+
+expensive_lock_unavailable_ () {
+	echo >&2 "# cannot take the EXPENSIVE lock, running anyway"
+	rm -f "$expensive_lock_.$$"
+}
+
+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.
+			#
+			# Rarely, after a holder is killed, two waiters can
+			# both get past this check and run together.
+			if remove_expensive_lock_owned_by_ "$owner"
+			then
+				echo >&2 "# removing stale EXPENSIVE lock left by PID $owner"
+			fi
+			continue
+		fi
+
+		if test -z "$waited"
+		then
+			echo >&2 "# waiting for EXPENSIVE lock held by PID $owner"
+			waited=t
+		fi
+
+		sleep 1
+	done
+
+	have_expensive_lock_=t
+	rm -f "$tmp"
+}
+
 test_run_ () {
 	test_cleanup=:
 	expecting_failure=$2
@@ -1103,6 +1197,12 @@ test_run_ () {
 		fi
 	fi
 
+	case ",$test_prereq," in
+	*,EXPENSIVE,*)
+		acquire_expensive_lock_
+		;;
+	esac
+
 	setup_malloc_check
 	test_eval_ "$1"
 	eval_ret=$?
@@ -1115,6 +1215,7 @@ test_run_ () {
 		test_eval_ "$test_cleanup"
 		teardown_malloc_check
 	fi
+	release_expensive_lock_
 	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
 	then
 		echo ""

---
base-commit: 566b6997dd4710b625c0882cf10a7443bf52d183
change-id: 20261117-ks-ci-long-test-hang-fix-008e72fd40c6
Patrick Steinhardt· Dec 3, 2026, 09:38 UTC · re: Kaartic Sivaraam · lore

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

On Wed, Dec 02, 2026 at 01:23:30AM +0530, Kaartic Sivaraam wrote:
Show 9 quoted lines
> Range-diff vs. v4:
> 
> 1:  ad8fa8d872 ! 1:  2fcdeefff3 test-lib: run EXPENSIVE tests one at a time
>      @@ t/test-lib.sh: test_missing_prereq=
> 
>       test_external_has_tap=0
> 
>     ++# Remove the lock only if it still names $1 as its owner, so that
>     ++# a lock someone else has just taken is left alone.

I feel like this comment is a bit on the verbose side as it doesn't really provide much value over what the code already trivially tells you, but okay.

>     ++remove_expensive_lock_owned_by_ () {
>     ++	test "$(cat "$expensive_lock_" 2>/dev/null)" = "$1" &&
>     ++	rm -f "$expensive_lock_"
>     ++}
So this is now wrapped in its own function. Makes sense, I guess.
Show 8 quoted lines
>     @@ t/test-lib.sh: fail_117 () {
>      +have_expensive_lock_=
>      +
>      +expensive_lock_unavailable_ () {
>     -+	say >&3 "cannot take the EXPENSIVE lock, running anyway"
>     ++	echo >&2 "# cannot take the EXPENSIVE lock, running anyway"
>      +	rm -f "$expensive_lock_.$$"
>      +}

Here we now output to stderr, and we cater towards strict TAP parser by prefixing it with "#". I think this change makes sense, as this should make it more discoverable when this scenario kicks in.

Show 8 quoted lines
>     @@ t/test-lib.sh: fail_117 () {
>      +			#
>      +			# 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"
>     ++			#
>     ++			# Rarely, after a holder is killed, two waiters can
>     ++			# both get past this check and run together.

Heh, it takes courage to claim that a race happens rarely. My experience is that no matter how unlikely a race, CI systems know to trigger that race often enough to annoy the hell out of you.

Show 7 quoted lines
>     ++			if remove_expensive_lock_owned_by_ "$owner"
>      +			then
>     -+				rm -f "$expensive_lock_"
>     ++				echo >&2 "# removing stale EXPENSIVE lock left by PID $owner"
>      +			fi
>      +			continue
>      +		fi

It's a bit funny to say "removing" after you have removed the lock already. But providing that context can definitely be helpful, I'd assume.

Anyway, my comments are really only just nits, so please feel free to ignore them. Other than that I'm happy with this version, thanks!

Patrick

← back to recent threads