From: Pushkar Singh <pushkarkumarsingh1970@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, mail@thomasbachem.com, ps@pks.im,
ttaylorr@openai.com,
Pushkar Singh <pushkarkumarsingh1970@gmail.com>
Subject: [PATCH v4 0/2] maintenance: improve lock acquisition failure handling
Date: Wed, 18 Nov 2026 19:07:33 +0000 [thread overview]
Message-ID: <20261118190735.3928-1-pushkarkumarsingh1970@gmail.com> (raw)
In-Reply-To: <20261104173422.20725-1-pushkarkumarsingh1970@gmail.com>
Greetings,
This series improves how "git maintenance" handles failures to
acquire the maintenance lock.
The first patch makes lock acquisition failures behave differently
depending on how maintenance was invoked. Manual invocations report
an error and return a non-zero exit status, while automatic and
scheduled invocations skip non-fatally. It also prevents the prefetch
task from triggering nested automatic maintenance through its child
fetch process.
The second patch adds Trace2 instrumentation for automatic and
scheduled maintenance skips, making it easier to identify repeated
lock acquisition failures.
Changes in v4:
- Keep scheduled maintenance non-fatal when another process holds
the maintenance lock.
- Prevent nested automatic maintenance in the prefetch task by
passing --no-auto-maintenance to child fetch processes.
- Extend Trace2 instrumentation to cover scheduled maintenance skips.
- Add regression tests for scheduled runs and nested prefetch
maintenance.
Thanks,
Pushkar
Pushkar Singh (2):
maintenance: handle lock acquisition failures correctly
maintenance: trace lock acquisition failures
builtin/gc.c | 27 +++++++++++++----
t/t7900-maintenance.sh | 69 ++++++++++++++++++++++++++++++++++++++++--
2 files changed, 88 insertions(+), 8 deletions(-)
Range-diff against v3:
1: ab33f1b432 ! 1: dfebf3bea5 maintenance: report lock acquisition failures
@@ Metadata
Author: Pushkar Singh <pushkarkumarsingh1970@gmail.com>
## Commit message ##
- maintenance: report lock acquisition failures
+ maintenance: handle lock acquisition failures correctly
- When git maintenance run fails to acquire the maintenance
- lock, report the failure instead of silently succeeding.
+ When git maintenance run fails to acquire objects/maintenance.lock,
+ the failure should be handled appropriately depending on the invocation:
- For automatic invocations, print a warning while preserving
- the successful exit status. For manual invocations, report
- the failure as an error and return a non-zero exit status.
+ - 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.
- Respect the --quiet option when reporting the failure.
+ 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 ##
+@@ builtin/gc.c: 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)
@@ builtin/gc.c: 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;
++ int err = errno;
++
/*
* Another maintenance command is running.
*
-@@ builtin/gc.c: static int maintenance_run_tasks(struct maintenance_run_opts *opts,
- * recursive process stack. Do not report an error in
- * that case.
+ * 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;
++ struct strbuf buf = STRBUF_INIT;
++
+ unable_to_lock_message(lock_path, err, &buf);
+ if (opts->auto_flag)
+ warning("%s", buf.buf);
-+ else
++ else if (!opts->schedule)
+ error("%s", buf.buf);
+ strbuf_release(&buf);
+ }
free(lock_path);
- return 0;
-+ return opts->auto_flag ? 0 : -1;
++ return (opts->auto_flag || opts->schedule) ? 0 : -1;
}
free(lock_path);
## t/t7900-maintenance.sh ##
+@@ t/t7900-maintenance.sh: 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 &&
+@@ t/t7900-maintenance.sh: 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' '
@@ t/t7900-maintenance.sh: test_expect_success 'loose-objects task' '
test_cmp packs-between packs-after
'
@@ t/t7900-maintenance.sh: test_expect_success 'loose-objects task' '
+ 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 &&
2: b451682115 ! 2: a1491d0123 maintenance: trace lock acquisition failures
@@ Metadata
## Commit message ##
maintenance: trace lock acquisition failures
- When git maintenance run --auto fails to acquire the
- maintenance lock, emit a Trace2 data event describing
- the reason for the failure.
+ When git maintenance run fails to acquire the maintenance lock during
+ automatic or scheduled execution, it skips running maintenance tasks
+ non-fatally and exits successfully.
- This makes it easier to identify cases where automatic
- maintenance is repeatedly skipped.
+ Emit a Trace2 data event with category "maintenance" and key
+ "lock/skip" describing the OS error via strerror(err) when automatic or
+ scheduled maintenance skips due to a lock acquisition failure. This
+ makes it easier to identify situations where automatic or scheduled
+ background maintenance is repeatedly skipped due to lock contention or
+ stale locks, while preserving standard non-zero exit and error reporting
+ for manual invocations.
+
+ Add tests in t/t7900-maintenance.sh to verify that automatic and
+ scheduled maintenance emit the expected Trace2 event upon encountering
+ an existing lock, and that explicit manual maintenance does not emit
+ this skip event.
Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>
## builtin/gc.c ##
@@ builtin/gc.c: static int maintenance_run_tasks(struct maintenance_run_opts *opts,
- * recursive process stack. Do not report an error in
- * that case.
+ * another scheduled run is already running. Do not
+ * report an error in either case.
*/
-+ if (opts->auto_flag)
++ if (opts->auto_flag || opts->schedule)
+ trace2_data_string("maintenance", r, "lock/skip",
-+ strerror(err));
++ strerror(err));
+
if (!opts->quiet) {
- struct strbuf buf = STRBUF_INIT;
- unable_to_lock_message(lock_path, err, &buf);
+ struct strbuf buf = STRBUF_INIT;
+
## t/t7900-maintenance.sh ##
-@@ t/t7900-maintenance.sh: test_expect_success 'manual maintenance with --quiet suppresses diagnostic' '
+@@ t/t7900-maintenance.sh: test_expect_success 'scheduled maintenance skips with existing lock' '
test_path_is_file .git/objects/maintenance.lock
'
@@ t/t7900-maintenance.sh: test_expect_success 'manual maintenance with --quiet sup
+ test_trace2_data maintenance lock/skip "File exists" <trace.txt &&
+ test_path_is_file .git/objects/maintenance.lock
+'
++
++test_expect_success 'scheduled maintenance 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 --schedule=hourly 2>err &&
++ test_must_be_empty err &&
++ test_trace2_data maintenance lock/skip "File exists" <trace.txt &&
++ test_path_is_file .git/objects/maintenance.lock
++'
++
++test_expect_success 'manual maintenance does not trace lock/skip' '
++ test_when_finished "rm -f trace.txt err .git/objects/maintenance.lock" &&
++ : >.git/objects/maintenance.lock &&
++ GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
++ test_must_fail git maintenance run --quiet 2>err &&
++ test_must_be_empty err &&
++ ! grep -e "\"category\":\"maintenance\",\"key\":\"lock/skip\"" trace.txt &&
++ test_path_is_file .git/objects/maintenance.lock
++'
+
test_expect_success 'maintenance.loose-objects.auto' '
git repack -adk &&
base-commit: 3ef89b7020c1f4add19622af30f40ecc937af610
--
2.53.0
next prev parent reply other threads:[~2026-11-18 19:08 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-01 10:59 [PATCH] maintenance: trace when auto maintenance skips due to lock Pushkar Singh
2026-11-02 8:29 ` Patrick Steinhardt
2026-11-02 12:23 ` Pushkar Singh
2026-11-02 12:29 ` [PATCH v2] maintenance: improve handling of lock acquisition failures Pushkar Singh
2026-11-02 12:51 ` Patrick Steinhardt
2026-11-03 17:34 ` Pushkar Singh
2026-11-04 17:34 ` [PATCH v3 0/2] maintenance: improve lock acquisition failure handling Pushkar Singh
2026-11-04 17:34 ` [PATCH v3 1/2] maintenance: report lock acquisition failures Pushkar Singh
2026-11-04 17:34 ` [PATCH v3 2/2] maintenance: trace " Pushkar Singh
2026-11-18 19:07 ` Pushkar Singh [this message]
2026-11-18 19:07 ` [PATCH v4 1/2] maintenance: handle lock acquisition failures correctly Pushkar Singh
2026-11-18 19:07 ` [PATCH v4 2/2] maintenance: trace lock acquisition failures Pushkar Singh
2026-11-04 18:00 ` [PATCH v2] maintenance: improve handling of " Aniket Gupta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118190735.3928-1-pushkarkumarsingh1970@gmail.com \
--to=pushkarkumarsingh1970@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=mail@thomasbachem.com \
--cc=ps@pks.im \
--cc=ttaylorr@openai.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox