[PATCH v2] maintenance: improve handling of lock acquisition failures
- From
Pushkar Singh <pushkarkumarsingh1970@gmail.com>
- Date
- Nov 2, 2026, 12:29 UTC
- Message-ID
- <20261102122955.143018-2-pushkarkumarsingh1970@gmail.com>
- In-Reply-To
- <20261101105859.25579-2-pushkarkumarsingh1970@gmail.com>
When git maintenance run --auto fails to acquire the maintenance lock, emit a Trace2 data event describing the reason for the failure. This makes it easier to identify cases where automatic maintenance is repeatedly skipped.
For manual invocations, report lock acquisition failures as errors and return a non-zero exit status instead of silently succeeding. Respect the --quiet option when reporting the error.
Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com> --- Changes in v2: - Report lock acquisition failures for manual invocations as errors. - Print a warning for automatic invocations while preserving successful exit status. - Trace the reason for all automatic lock acquisition failures, not just EEXIST. - Add tests for automatic and manual lock failure behavior.
builtin/gc.c | 28 ++++++++++++++++++++++++---- t/t7900-maintenance.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-)
diff --git a/builtin/gc.c b/builtin/gc.c index 81cecf6b6c..9223409750 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -1402,6 +1402,8 @@ 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. * @@ -1409,11 +1411,29 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts, * recursive process stack. Do not report an error in * that case. */ - if (!opts->auto_flag && !opts->quiet) - warning(_("lock file '%s' exists, skipping maintenance"), - lock_path); + if (opts->auto_flag) { + trace2_data_string("maintenance", r, "lock/skip", + (err == EEXIST) ? "exists" : strerror(err)); + + if (!opts->quiet) { + struct strbuf buf = STRBUF_INIT; + unable_to_lock_message(lock_path, err, &buf); + warning("%s", buf.buf); + strbuf_release(&buf); + } + + free(lock_path); + return 0; + } + + if (!opts->quiet) { + struct strbuf buf = STRBUF_INIT; + unable_to_lock_message(lock_path, err, &buf); + error("%s", buf.buf); + strbuf_release(&buf); + } free(lock_path); - return 0; + return -1; } free(lock_path); diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh index 9d610a29fe..c0763cb6e2 100755 --- a/t/t7900-maintenance.sh +++ b/t/t7900-maintenance.sh @@ -387,6 +387,32 @@ test_expect_success 'loose-objects task' ' test_cmp packs-between packs-after ' +test_expect_success 'auto maintenance warns and traces existing lock' ' + test_when_finished "rm -f trace.txt err .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git maintenance run --auto --no-quiet 2>err && + test_grep "Unable to create.*maintenance\.lock" err && + test_trace2_data maintenance lock/skip exists <trace.txt && + 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 'maintenance.loose-objects.auto' ' git repack -adk && GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
-- 2.53.0