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 <kaar…@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…@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/2026…@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/2026…@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/2026…@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/2026…@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 <kaar…@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(-)
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
[PATCH v5] test-lib: run EXPENSIVE tests one at a time
- From
- kaar…@gmail.com (Kaartic Sivaraam)
- Organization
- Usenet Network
- Message-ID
- <2026…@gmail.com>
- Date
- Newsgroups
- linux.kernel.git