maintenance: trace when auto maintenance skips due to lock
PATCH 1/1 maintenance: trace when auto maintenance skips due to lock
When git maintenance run --auto encounters an existing maintenance.lock, it silently skips running maintenance and returns successfully.
This makes it difficult to identify cases where automatic maintenance is being skipped repeatedly, for example due to a stale lock left behind by an interrupted maintenance process.
Emit a Trace2 data event when automatic maintenance skips due to an existing lock, allowing users to identify these skips when Trace2 event logging is enabled.
--- builtin/gc.c | 2 ++ t/t7900-maintenance.sh | 8 ++++++++ 2 files changed, 10 insertions(+)
builtin/gc.c +2 -0
@@ -1409,6 +1409,8 @@ 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 && errno ==EEXIST ) +trace2_data_string ("maintenance" , r,"lock/skip" ,"exists" );if (!opts->auto_flag && !opts->quiet)warning (_ ("lock file '%s' exists, skipping maintenance" ), lock_path);
t/t7900-maintenance.sh +8 -0
@@ -387,6 +387,14 @@ test_expect_success 'loose-objects task' ' test_cmp packs-between packs-after ' +test_expect_success 'auto maintenance traces existing lock' ' + test_when_finished "rm -f trace .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + GIT_TRACE2_EVENT="$PWD/trace" git maintenance run --auto && + test_grep "\"event\":\"data\".*\"category\":\"maintenance\".*\"key\":\"lock/skip\".*\"value\":\"exists\"" trace && + 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" \
Signature · 1 line
2.53.0
builtin/gc.c static int maintenance_run_tasks(struct maintenance_run_opts *opts, · line 1413
Quoted 10 lines · Pushkar Singh
builtin/gc.c +2
-0@@ -1409,6 +1409,8 @@ 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 && errno ==EEXIST ) +trace2_data_string ("maintenance" , r,"lock/skip" ,"exists" );
So we now know to report failure when the lock exists already, but in any other case we don't log anything at all. But wouldn't the caller want to know when we failed because of for example EPERM, too, or because of any other error?
builtin/gc.c static int maintenance_run_tasks(struct maintenance_run_opts *opts, · line 1416
Quoted 3 lines
builtin/gc.c +0
-0if (!opts->auto_flag && !opts->quiet)warning (_ ("lock file '%s' exists, skipping maintenance" ), lock_path);
It's quite curious that we only print a warning when git maintenance run has failed to acquire the lock instead of bubbling up an error. I would have expected that we:
- Exit successfully but print a warning in the case where we are executed with "--auto".
- Exit with an error code and print an error in the case where we execute without "--auto".
Silently swallowing the fact that we didn't do anything in the latter case certainly feels wrong to me. And git-gc(1) for example dies right away when the lock exists, so that to me is another indicator that we should follow and do the same for git-maintenance(1).
This is of course outside the scope of your patch series, but if you feel like it I think it could be easily fixed while at it in a second patch.
Thanks!
Patrick
[snip]
Quoted 5 lines
So we now know to report failure when the lock exists already, but in any other case we don't log anything at all. But wouldn't the caller want to know when we failed because of for example EPERM, too, or because of any other error?
At first I only considered EEXIST, but you're right, we should be reporting other lock acquisition failures too. I've included the actual reason in the Trace2 event.
Quoted 8 lines
Quoted 3 lines
if (!opts->auto_flag && !opts->quiet) warning(_("lock file '%s' exists, skipping maintenance"), lock_path);It's quite curious that we only print a warning when
git maintenance runhas failed to acquire the lock instead of bubbling up an error. I would have expected that we:
Quoted 6 lines
- Exit successfully but print a warning in the case where we are executed with "--auto".
- Exit with an error code and print an error in the case where we execute without "--auto".
Agreed. The current behavior for manual invocations is a bit odd, especially since we end up returning success even though we couldn't acquire the lock.
[snip]
Quoted 4 lines
This is of course outside the scope of your patch series, but if you feel like it I think it could be easily fixed while at it in a second patch.
I was thinking of including this in the same patch series since both changes are around the same lock acquisition path. I've included this in the v2 as well, so manual invocations now report an error and return a non-zero status, while --auto still exits successfully but prints a warning.
Thanks for the review :-)
Pushkar
PATCH v2 1/1 maintenance: improve handling of lock acquisition failures
In reply to Pushkar SinghWhen 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.
---
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(-)
builtin/gc.c +24 -4
@@ -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);
t/t7900-maintenance.sh +26 -0
@@ -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" \
Signature · 1 line
2.53.0
Re: [PATCH v2] maintenance: improve handling of lock acquisition failures
Quoted 5 lines · Pushkar Singh
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.
This really should be two patches now:
- One patch that changes how we handle the failure to lock.
- One patch that introduces your trace2 string.
One note though: please wait a bit before rerolling again so that others can chime in first. You should ideally reroll at most once per day.
Quoted 14 lines
builtin/gc.c +2
-0@@ -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. *
builtin/gc.c static int maintenance_run_tasks(struct maintenance_run_opts *opts, · line 1416
Quoted 9 lines
builtin/gc.c +3
-3@@ -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));
Why don't you use strerror(3p) for both cases?
Quoted 6 lines
builtin/gc.c +6
-0+if (!opts->quiet) { +struct strbuf buf =STRBUF_INIT ; +unable_to_lock_message (lock_path, err, &buf); +warning ("%s" , buf.buf); +strbuf_release (&buf); + }
builtin/gc.c static int maintenance_run_tasks(struct maintenance_run_opts *opts, · line 1433
Quoted 10 lines
builtin/gc.c +10
-0+ +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);
There's some duplication here with the auto-flag case. Can't we deduplicate that?
Thanks!
Patrick
Re: [PATCH v2] maintenance: improve handling of lock acquisition failures
[snip]
Quoted 6 lines
This really should be two patches now:
- One patch that changes how we handle the failure to lock.
- One patch that introduces your trace2 string.
Alright, I'll split these into two separate patches.
[snip]
Quoted 2 lines
Why don't you use strerror(3p) for both cases?
Makes sense. I'll use strerror(err) consistently.
[snip]
Quoted 3 lines
There's some duplication here with the auto-flag case. Can't we deduplicate that?
I'll clean that up as well.
[snip]
Quoted 3 lines
One note though: please wait a bit before rerolling again so that others can chime in first. You should ideally reroll at most once per day.
Okay, I'll make sure to give some time for others to review before sending another revision from now on :-)
I'll make these changes and send the revised series tomorrow.
Thanks!
Pushkar
v3 cover maintenance: improve lock acquisition failure handling
In reply to Pushkar SinghGreetings,
This series improves how "git maintenance" handles failures to acquire the maintenance lock.
The first patch changes the handling of lock acquisition failures so that manual invocations report an error and return a non-zero exit status, while automatic invocations print a warning and exit successfully.
The second patch adds a Trace2 data event to make it easier to identify cases where automatic maintenance is repeatedly skipped due to lock acquisition failures.
Changes in v3:
- Split the changes into two separate patches: one for lock failure handling and another for Trace2 instrumentation.
- Use strerror(err) consistently for reporting lock acquisition failures in Trace2.
- Deduplicate the diagnostic message construction while preserving the distinction between warnings and errors.
Thanks,
Pushkar
Pushkar Singh (2):
maintenance: report lock acquisition failures
maintenance: trace lock acquisition failures
PATCH v3 1/2 maintenance: report lock acquisition failures
When git maintenance run fails to acquire the maintenance lock, report the failure instead of silently succeeding.
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.
Respect the --quiet option when reporting the failure.
--- builtin/gc.c | 15 +++++++++++---- t/t7900-maintenance.sh | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-)
builtin/gc.c +11 -4
@@ -1402,6 +1402,7 @@ 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 +1410,17 @@ 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->quiet) { +struct strbuf buf =STRBUF_INIT ; +unable_to_lock_message (lock_path, err, &buf); +if (opts->auto_flag) +warning ("%s" , buf.buf); +else +error ("%s" , buf.buf); +strbuf_release (&buf); + }free (lock_path); -return 0 ; +return opts->auto_flag ?0 :-1 ; }free (lock_path);
t/t7900-maintenance.sh +24 -0
@@ -387,6 +387,30 @@ 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 'maintenance.loose-objects.auto' ' git repack -adk && GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
Signature · 1 line
2.53.0
PATCH v3 2/2 maintenance: trace lock acquisition failures
In reply to Pushkar SinghWhen 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.
--- builtin/gc.c | 4 ++++ t/t7900-maintenance.sh | 9 +++++++++ 2 files changed, 13 insertions(+)
builtin/gc.c +4 -0
@@ -1410,6 +1410,10 @@ 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) +trace2_data_string ("maintenance" , r,"lock/skip" , +strerror (err)); +if (!opts->quiet) {struct strbuf buf =STRBUF_INIT ;unable_to_lock_message (lock_path, err, &buf);
t/t7900-maintenance.sh +9 -0
@@ -411,6 +411,15 @@ test_expect_success 'manual maintenance with --quiet suppresses diagnostic' ' test_path_is_file .git/objects/maintenance.lock ' +test_expect_success 'auto maintenance traces existing lock' ' + test_when_finished "rm -f trace.txt .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git maintenance run --auto --quiet && + test_trace2_data maintenance lock/skip "File exists" <trace.txt && + 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" \
Signature · 1 line
2.53.0
v4 cover maintenance: improve lock acquisition failure handling
In reply to Pushkar SinghGreetings,
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 <[email protected]>
## 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.
## 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.
## 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
Signature · 1 line
2.53.0
PATCH v4 1/2 maintenance: handle lock acquisition failures correctly
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.
--- builtin/gc.c | 23 +++++++++++++++++------ t/t7900-maintenance.sh | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 8 deletions(-)
builtin/gc.c +17 -6
@@ -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);
t/t7900-maintenance.sh +38 -2
@@ -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" \
Signature · 1 line
2.53.0
PATCH v4 2/2 maintenance: trace lock acquisition failures
In reply to Pushkar SinghWhen automatic or scheduled maintenance fails to acquire the maintenance lock, emit a Trace2 data event describing the reason for the failure.
The event uses the "maintenance" category and "lock/skip" key, with the OS error reported via strerror(err).
This makes it easier to identify cases where automatic or scheduled maintenance is repeatedly skipped due to lock acquisition failures.
--- builtin/gc.c | 4 ++++ t/t7900-maintenance.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+)
builtin/gc.c +4 -0
@@ -1413,6 +1413,10 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,* another scheduled run is already running. Do not * report an error in either case. */ +if (opts->auto_flag || opts->schedule) +trace2_data_string ("maintenance" , r,"lock/skip" , +strerror (err)); +if (!opts->quiet) {struct strbuf buf =STRBUF_INIT ;
t/t7900-maintenance.sh +29 -0
@@ -423,6 +423,35 @@ test_expect_success 'scheduled maintenance skips with existing lock' ' test_path_is_file .git/objects/maintenance.lock ' +test_expect_success 'auto maintenance traces existing lock' ' + test_when_finished "rm -f trace.txt .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git maintenance run --auto --quiet && + 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 && GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
Signature · 1 line
2.53.0
Re: [PATCH v2] maintenance: improve handling of lock acquisition failures
In reply to Pushkar SinghHi Pushkar,
I tried v2 while another maintenance run was holding the lock. The scheduled background runs, the ones set up by "git maintenance start", now exit with an error instead of 0. Two overlapping runs end in a failure where master just skips them and the docs say it is normal.
Also, running the prefetch task by hand now prints a lock warning and I think it comes from the fetch it starts internally.
Thanks,
Aniket