[PATCH v4 1/2] maintenance: handle lock acquisition failures correctly
- From
Pushkar Singh <pushkarkumarsingh1970@gmail.com>
- Date
- Nov 18, 2026, 19:07 UTC
- Message-ID
- <20261118190735.3928-2-pushkarkumarsingh1970@gmail.com>
- In-Reply-To
- <20261118190735.3928-1-pushkarkumarsingh1970@gmail.com>
When git maintenance run fails to acquire objects/maintenance.lock, the failure should be handled appropriately depending on the invocation:
- Explicit manual maintenance should report an error and return a non-zero exit status, or suppress the diagnostic when --quiet is set. - Automatic maintenance (--auto) and scheduled maintenance (--schedule) should remain non-fatal and exit successfully (0) to avoid failing routine background runs when another maintenance process holds the lock. - For automatic maintenance, print a warning diagnostic unless --quiet is set, while scheduled maintenance remains silent.
Use unable_to_lock_message() to format error and warning diagnostics, and check opts->schedule to distinguish scheduled invocations from manual runs.
In addition, pass --no-auto-maintenance to child fetch invocations in fetch_remote() to prevent the prefetch task from triggering nested automatic maintenance while the parent process holds the lock.
Add tests in t/t7900-maintenance.sh covering manual, quiet, automatic, and scheduled lock acquisition handling, as well as prefetch child fetch arguments.
Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com> --- builtin/gc.c | 23 +++++++++++++++++------ t/t7900-maintenance.sh | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 8 deletions(-)
diff --git a/builtin/gc.c b/builtin/gc.c index 81cecf6b6c..d423b68cce 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -902,6 +902,7 @@ static int fetch_remote(struct remote *remote, void *cbdata) strvec_pushl(&child.args, "fetch", remote->name, "--prefetch", "--prune", "--no-tags", "--no-write-fetch-head", "--recurse-submodules=no", + "--no-auto-maintenance", NULL); if (opts->quiet) @@ -1402,18 +1403,28 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts, enum auto_gc_hook_result auto_gc_hook_result = AUTO_GC_HOOK_UNDECIDED; if (repo_hold_lock_file_for_update(r, &lk, lock_path, LOCK_NO_DEREF) < 0) { + int err = errno; + /* * Another maintenance command is running. * * If --auto was provided, then it is likely due to a - * recursive process stack. Do not report an error in - * that case. + * recursive process stack. If --schedule was provided, + * another scheduled run is already running. Do not + * report an error in either case. */ - if (!opts->auto_flag && !opts->quiet) - warning(_("lock file '%s' exists, skipping maintenance"), - lock_path); + if (!opts->quiet) { + struct strbuf buf = STRBUF_INIT; + + unable_to_lock_message(lock_path, err, &buf); + if (opts->auto_flag) + warning("%s", buf.buf); + else if (!opts->schedule) + error("%s", buf.buf); + strbuf_release(&buf); + } free(lock_path); - return 0; + return (opts->auto_flag || opts->schedule) ? 0 : -1; } free(lock_path); diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh index 9d610a29fe..4e519e4a5d 100755 --- a/t/t7900-maintenance.sh +++ b/t/t7900-maintenance.sh @@ -327,9 +327,10 @@ test_expect_success 'prefetch multiple remotes' ' test_commit -C clone1 one && test_commit -C clone2 two && GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null && - fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" && + fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --no-auto-maintenance --quiet" && test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt && test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt && + test_subcommand ! git maintenance run --auto <run-prefetch.txt && git for-each-ref refs/remotes >actual && test_must_be_empty actual && git log prefetch/remotes/remote1/one && @@ -345,7 +346,10 @@ test_expect_success 'prefetch multiple remotes' ' git config remote.remote1.skipFetchAll true && GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null && test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt && - test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt + test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt && + + git maintenance run --task=prefetch --no-quiet 2>err && + test_must_be_empty err ' test_expect_success 'loose-objects task' ' @@ -387,6 +391,38 @@ test_expect_success 'loose-objects task' ' test_cmp packs-between packs-after ' +test_expect_success 'auto maintenance warns when lock exists' ' + test_when_finished "rm -f err .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + git maintenance run --auto --no-quiet 2>err && + test_grep "Unable to create.*maintenance\.lock" err && + test_path_is_file .git/objects/maintenance.lock +' + +test_expect_success 'manual maintenance fails with existing lock' ' + test_when_finished "rm -f err .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + test_must_fail git maintenance run --no-quiet 2>err && + test_grep "maintenance\.lock" err && + test_path_is_file .git/objects/maintenance.lock +' + +test_expect_success 'manual maintenance with --quiet suppresses diagnostic' ' + test_when_finished "rm -f err .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + test_must_fail git maintenance run --quiet 2>err && + test_must_be_empty err && + test_path_is_file .git/objects/maintenance.lock +' + +test_expect_success 'scheduled maintenance skips with existing lock' ' + test_when_finished "rm -f err .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + git maintenance run --schedule=hourly 2>err && + test_must_be_empty err && + test_path_is_file .git/objects/maintenance.lock +' + test_expect_success 'maintenance.loose-objects.auto' ' git repack -adk && GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
-- 2.53.0