From: Kaartic Sivaraam Date: Sat, 28 Nov 2026 14:57:59 GMT Subject: [PATCH v4 1/2] test-lib: run EXPENSIVE tests one at a time Message-ID: <20261128-ks-ci-long-test-hang-fix-v4-1-f117badd2e13@gmail.com> In-Reply-To: <20261128-ks-ci-long-test-hang-fix-v4-0-f117badd2e13@gmail.com> 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 --- t/README | 8 +++++- t/test-lib.sh | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 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=:: 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