{"thread":{"id":"66736","subject":"[PATCH] maintenance: trace when auto maintenance skips due to lock","startedAt":"2026-11-01T10:59:00Z","lastAt":"2026-11-18T19:07:35Z","messageCount":13,"participants":["Pushkar Singh","Patrick Steinhardt","Aniket Gupta"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"557075","messageId":"20261101105859.25579-2-pushkarkumarsingh1970@gmail.com","threadId":"66736","inReplyTo":null,"subject":"[PATCH] maintenance: trace when auto maintenance skips due to lock","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-01T10:59:00Z","receivedAt":"2026-11-01T10:59:00Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"When git maintenance run --auto encounters an existing\nmaintenance.lock, it silently skips running maintenance and\nreturns successfully.\n\nThis makes it difficult to identify cases where automatic\nmaintenance is being skipped repeatedly, for example due to\na stale lock left behind by an interrupted maintenance\nprocess.\n\nEmit a Trace2 data event when automatic maintenance skips\ndue to an existing lock, allowing users to identify these\nskips when Trace2 event logging is enabled.\n\nSigned-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>\n---\n builtin/gc.c           | 2 ++\n t/t7900-maintenance.sh | 8 ++++++++\n 2 files changed, 10 insertions(+)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 81cecf6b6c..e73d1f1d49 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1409,6 +1409,8 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n \t\t * recursive process stack. Do not report an error in\n \t\t * that case.\n \t\t */\n+\t\tif (opts->auto_flag && errno == EEXIST)\n+\t\t\ttrace2_data_string(\"maintenance\", r, \"lock/skip\", \"exists\");\n \t\tif (!opts->auto_flag && !opts->quiet)\n \t\t\twarning(_(\"lock file '%s' exists, skipping maintenance\"),\n \t\t\t\tlock_path);\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 9d610a29fe..440eeb956d 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -387,6 +387,14 @@ test_expect_success 'loose-objects task' '\n \ttest_cmp packs-between packs-after\n '\n \n+test_expect_success 'auto maintenance traces existing lock' '\n+\ttest_when_finished \"rm -f trace .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\tGIT_TRACE2_EVENT=\"$PWD/trace\" git maintenance run --auto &&\n+\ttest_grep \"\\\"event\\\":\\\"data\\\".*\\\"category\\\":\\\"maintenance\\\".*\\\"key\\\":\\\"lock/skip\\\".*\\\"value\\\":\\\"exists\\\"\" trace &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n test_expect_success 'maintenance.loose-objects.auto' '\n \tgit repack -adk &&\n \tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n-- \n2.53.0\n\n\n"},{"id":"557120","messageId":"auhJ0B_1__9z3DEC@pks.im","threadId":"66736","inReplyTo":"20261101105859.25579-2-pushkarkumarsingh1970@gmail.com","subject":"Re: [PATCH] maintenance: trace when auto maintenance skips due to lock","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-02T08:29:33Z","receivedAt":"2026-11-02T08:29:33Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sun, Nov 01, 2026 at 10:59:00AM +0000, Pushkar Singh wrote:\n> diff --git a/builtin/gc.c b/builtin/gc.c\n> index 81cecf6b6c..e73d1f1d49 100644\n> --- a/builtin/gc.c\n> +++ b/builtin/gc.c\n> @@ -1409,6 +1409,8 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n>  \t\t * recursive process stack. Do not report an error in\n>  \t\t * that case.\n>  \t\t */\n> +\t\tif (opts->auto_flag && errno == EEXIST)\n> +\t\t\ttrace2_data_string(\"maintenance\", r, \"lock/skip\", \"exists\");\n\nSo we now know to report failure when the lock exists already, but in\nany other case we don't log anything at all. But wouldn't the caller\nwant to know when we failed because of for example EPERM, too, or\nbecause of any other error?\n\n>  \t\tif (!opts->auto_flag && !opts->quiet)\n>  \t\t\twarning(_(\"lock file '%s' exists, skipping maintenance\"),\n>  \t\t\t\tlock_path);\n\nIt's quite curious that we only print a warning when `git maintenance\nrun` has failed to acquire the lock instead of bubbling up an error. I\nwould have expected that we:\n\n  - Exit successfully but print a warning in the case where we are\n    executed with \"--auto\".\n\n  - Exit with an error code and print an error in the case where we\n    execute without \"--auto\".\n\nSilently swallowing the fact that we didn't do anything in the latter\ncase certainly feels wrong to me. And git-gc(1) for example dies right\naway when the lock exists, so that to me is another indicator that we\nshould follow and do the same for git-maintenance(1).\n\nThis is of course outside the scope of your patch series, but if you\nfeel like it I think it could be easily fixed while at it in a second\npatch.\n\nThanks!\n\nPatrick\n\n"},{"id":"557168","messageId":"CALE2CrSNCpjLJcfqU6=qwQZbWwV4Vn5bgfEmp8piCy7CR3JxOA@mail.gmail.com","threadId":"66736","inReplyTo":"auhJ0B_1__9z3DEC@pks.im","subject":"Re: [PATCH] maintenance: trace when auto maintenance skips due to lock","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-02T12:23:53Z","receivedAt":"2026-11-02T12:23:53Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"[snip]\n> So we now know to report failure when the lock exists already, but in\n> any other case we don't log anything at all. But wouldn't the caller\n> want to know when we failed because of for example EPERM, too, or\n> because of any other error?\n>\nAt first I only considered EEXIST, but you're right, we should be\nreporting other lock acquisition failures too. I've included the actual\nreason in the Trace2 event.\n\n> >               if (!opts->auto_flag && !opts->quiet)\n> >                       warning(_(\"lock file '%s' exists, skipping maintenance\"),\n> >                               lock_path);\n>\n> It's quite curious that we only print a warning when `git maintenance\n> run` has failed to acquire the lock instead of bubbling up an error. I\n> would have expected that we:\n>\n>   - Exit successfully but print a warning in the case where we are\n>     executed with \"--auto\".\n>\n>   - Exit with an error code and print an error in the case where we\n>     execute without \"--auto\".\n>\nAgreed. The current behavior for manual invocations is a bit odd,\nespecially since we end up returning success even though we couldn't\nacquire the lock.\n\n[snip]\n> This is of course outside the scope of your patch series, but if you\n> feel like it I think it could be easily fixed while at it in a second\n> patch.\n>\nI was thinking of including this in the same patch series since both\nchanges are around the same lock acquisition path. I've included this\nin the v2 as well, so manual invocations now report an error and return\na non-zero status, while --auto still exits successfully but prints\na warning.\n\nThanks for the review :-)\n\nPushkar\n\n"},{"id":"557170","messageId":"20261102122955.143018-2-pushkarkumarsingh1970@gmail.com","threadId":"66736","inReplyTo":"20261101105859.25579-2-pushkarkumarsingh1970@gmail.com","subject":"[PATCH v2] maintenance: improve handling of lock acquisition failures","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-02T12:29:56Z","receivedAt":"2026-11-02T12:29:56Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"When git maintenance run --auto fails to acquire the maintenance\nlock, emit a Trace2 data event describing the reason for the\nfailure. This makes it easier to identify cases where automatic\nmaintenance is repeatedly skipped.\n\nFor manual invocations, report lock acquisition failures as\nerrors and return a non-zero exit status instead of silently\nsucceeding. Respect the --quiet option when reporting the error.\n\nSigned-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>\n---\nChanges in v2:\n- Report lock acquisition failures for manual invocations as errors.\n- Print a warning for automatic invocations while preserving successful exit status.\n- Trace the reason for all automatic lock acquisition failures, not just EEXIST.\n- Add tests for automatic and manual lock failure behavior.\n\n builtin/gc.c           | 28 ++++++++++++++++++++++++----\n t/t7900-maintenance.sh | 26 ++++++++++++++++++++++++++\n 2 files changed, 50 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 81cecf6b6c..9223409750 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1402,6 +1402,8 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n \tenum auto_gc_hook_result auto_gc_hook_result = AUTO_GC_HOOK_UNDECIDED;\n \n \tif (repo_hold_lock_file_for_update(r, &lk, lock_path, LOCK_NO_DEREF) < 0) {\n+\t\tint err = errno;\n+\n \t\t/*\n \t\t * Another maintenance command is running.\n \t\t *\n@@ -1409,11 +1411,29 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n \t\t * recursive process stack. Do not report an error in\n \t\t * that case.\n \t\t */\n-\t\tif (!opts->auto_flag && !opts->quiet)\n-\t\t\twarning(_(\"lock file '%s' exists, skipping maintenance\"),\n-\t\t\t\tlock_path);\n+\t\tif (opts->auto_flag) {\n+\t\t\ttrace2_data_string(\"maintenance\", r, \"lock/skip\",\n+\t\t\t\t(err == EEXIST) ? \"exists\" : strerror(err));\n+\n+\t\t\tif (!opts->quiet) {\n+\t\t\t\tstruct strbuf buf = STRBUF_INIT;\n+\t\t\t\tunable_to_lock_message(lock_path, err, &buf);\n+\t\t\t\twarning(\"%s\", buf.buf);\n+\t\t\t\tstrbuf_release(&buf);\n+\t\t\t}\n+\n+\t\t\tfree(lock_path);\n+\t\t\treturn 0;\n+\t\t}\n+\n+\t\tif (!opts->quiet) {\n+\t\t\tstruct strbuf buf = STRBUF_INIT;\n+\t\t\tunable_to_lock_message(lock_path, err, &buf);\n+\t\t\terror(\"%s\", buf.buf);\n+\t\t\tstrbuf_release(&buf);\n+\t\t}\n \t\tfree(lock_path);\n-\t\treturn 0;\n+\t\treturn -1;\n \t}\n \tfree(lock_path);\n \ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 9d610a29fe..c0763cb6e2 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -387,6 +387,32 @@ test_expect_success 'loose-objects task' '\n \ttest_cmp packs-between packs-after\n '\n \n+test_expect_success 'auto maintenance warns and traces existing lock' '\n+\ttest_when_finished \"rm -f trace.txt err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace.txt\" \\\n+\t\tgit maintenance run --auto --no-quiet 2>err &&\n+\ttest_grep \"Unable to create.*maintenance\\.lock\" err &&\n+\ttest_trace2_data maintenance lock/skip exists <trace.txt &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n+test_expect_success 'manual maintenance fails with existing lock' '\n+\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\ttest_must_fail git maintenance run --no-quiet 2>err &&\n+\ttest_grep \"maintenance\\.lock\" err &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n+test_expect_success 'manual maintenance with --quiet suppresses diagnostic' '\n+\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\ttest_must_fail git maintenance run --quiet 2>err &&\n+\ttest_must_be_empty err &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n test_expect_success 'maintenance.loose-objects.auto' '\n \tgit repack -adk &&\n \tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n-- \n2.53.0\n\n"},{"id":"557174","messageId":"auiHS_439EeHCGH6@pks.im","threadId":"66736","inReplyTo":"20261102122955.143018-2-pushkarkumarsingh1970@gmail.com","subject":"Re: [PATCH v2] maintenance: improve handling of lock acquisition failures","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-02T12:51:23Z","receivedAt":"2026-11-02T12:51:23Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Nov 02, 2026 at 12:29:56PM +0000, Pushkar Singh wrote:\n> Changes in v2:\n> - Report lock acquisition failures for manual invocations as errors.\n> - Print a warning for automatic invocations while preserving successful exit status.\n> - Trace the reason for all automatic lock acquisition failures, not just EEXIST.\n> - Add tests for automatic and manual lock failure behavior.\n\nThis really should be two patches now:\n\n  - One patch that changes how we handle the failure to lock.\n\n  - One patch that introduces your trace2 string.\n\nOne note though: please wait a bit before rerolling again so that others\ncan chime in first. You should ideally reroll at most once per day.\n\n> diff --git a/builtin/gc.c b/builtin/gc.c\n> index 81cecf6b6c..9223409750 100644\n> --- a/builtin/gc.c\n> +++ b/builtin/gc.c\n> @@ -1402,6 +1402,8 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n>  \tenum auto_gc_hook_result auto_gc_hook_result = AUTO_GC_HOOK_UNDECIDED;\n>  \n>  \tif (repo_hold_lock_file_for_update(r, &lk, lock_path, LOCK_NO_DEREF) < 0) {\n> +\t\tint err = errno;\n> +\n>  \t\t/*\n>  \t\t * Another maintenance command is running.\n>  \t\t *\n> @@ -1409,11 +1411,29 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n>  \t\t * recursive process stack. Do not report an error in\n>  \t\t * that case.\n>  \t\t */\n> -\t\tif (!opts->auto_flag && !opts->quiet)\n> -\t\t\twarning(_(\"lock file '%s' exists, skipping maintenance\"),\n> -\t\t\t\tlock_path);\n> +\t\tif (opts->auto_flag) {\n> +\t\t\ttrace2_data_string(\"maintenance\", r, \"lock/skip\",\n> +\t\t\t\t(err == EEXIST) ? \"exists\" : strerror(err));\n\nWhy don't you use strerror(3p) for both cases?\n\n> +\t\t\tif (!opts->quiet) {\n> +\t\t\t\tstruct strbuf buf = STRBUF_INIT;\n> +\t\t\t\tunable_to_lock_message(lock_path, err, &buf);\n> +\t\t\t\twarning(\"%s\", buf.buf);\n> +\t\t\t\tstrbuf_release(&buf);\n> +\t\t\t}\n> +\n> +\t\t\tfree(lock_path);\n> +\t\t\treturn 0;\n> +\t\t}\n> +\n> +\t\tif (!opts->quiet) {\n> +\t\t\tstruct strbuf buf = STRBUF_INIT;\n> +\t\t\tunable_to_lock_message(lock_path, err, &buf);\n> +\t\t\terror(\"%s\", buf.buf);\n> +\t\t\tstrbuf_release(&buf);\n\nThere's some duplication here with the auto-flag case. Can't we\ndeduplicate that?\n\nThanks!\n\nPatrick\n\n"},{"id":"557344","messageId":"CALE2CrTHzsMFdae6pTgXQWXKVNw3xUw+fwifz4+4RcKRgLV8wg@mail.gmail.com","threadId":"66736","inReplyTo":"auiHS_439EeHCGH6@pks.im","subject":"Re: [PATCH v2] maintenance: improve handling of lock acquisition failures","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-03T17:34:45Z","receivedAt":"2026-11-03T17:34:45Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"[snip]\n> This really should be two patches now:\n>\n>   - One patch that changes how we handle the failure to lock.\n>\n>   - One patch that introduces your trace2 string.\n>\nAlright, I'll split these into two separate patches.\n\n[snip]\n> Why don't you use strerror(3p) for both cases?\n>\nMakes sense. I'll use strerror(err) consistently.\n\n[snip]\n> There's some duplication here with the auto-flag case. Can't we\n> deduplicate that?\n>\nI'll clean that up as well.\n\n[snip]\n> One note though: please wait a bit before rerolling again so that others\n> can chime in first. You should ideally reroll at most once per day.\n>\nOkay, I'll make sure to give some time for others to review before\nsending another revision from now on :-)\n\nI'll make these changes and send the revised series tomorrow.\n\nThanks!\n\nPushkar\n\n"},{"id":"557468","messageId":"20261104173422.20725-1-pushkarkumarsingh1970@gmail.com","threadId":"66736","inReplyTo":"20261102122955.143018-2-pushkarkumarsingh1970@gmail.com","subject":"[PATCH v3 0/2] maintenance: improve lock acquisition failure handling","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-04T17:34:20Z","receivedAt":"2026-11-04T17:34:20Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"Greetings,\n\nThis series improves how \"git maintenance\" handles failures to\nacquire the maintenance lock.\n\nThe first patch changes the handling of lock acquisition failures\nso that manual invocations report an error and return a non-zero\nexit status, while automatic invocations print a warning and exit\nsuccessfully.\n\nThe second patch adds a Trace2 data event to make it easier to\nidentify cases where automatic maintenance is repeatedly skipped\ndue to lock acquisition failures.\n\nChanges in v3:\n- Split the changes into two separate patches: one for lock\n  failure handling and another for Trace2 instrumentation.\n- Use `strerror(err)` consistently for reporting lock acquisition\n  failures in Trace2.\n- Deduplicate the diagnostic message construction while preserving\n  the distinction between warnings and errors.\n\nThanks,\nPushkar\n\nPushkar Singh (2):\n  maintenance: report lock acquisition failures\n  maintenance: trace lock acquisition failures\n\n"},{"id":"557469","messageId":"20261104173422.20725-2-pushkarkumarsingh1970@gmail.com","threadId":"66736","inReplyTo":"20261104173422.20725-1-pushkarkumarsingh1970@gmail.com","subject":"[PATCH v3 1/2] maintenance: report lock acquisition failures","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-04T17:34:21Z","receivedAt":"2026-11-04T17:34:21Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"When git maintenance run fails to acquire the maintenance\nlock, report the failure instead of silently succeeding.\n\nFor automatic invocations, print a warning while preserving\nthe successful exit status. For manual invocations, report\nthe failure as an error and return a non-zero exit status.\n\nRespect the --quiet option when reporting the failure.\n\nSigned-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>\n---\n builtin/gc.c           | 15 +++++++++++----\n t/t7900-maintenance.sh | 24 ++++++++++++++++++++++++\n 2 files changed, 35 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 81cecf6b6c..9ca9a3584b 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1402,6 +1402,7 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n \tenum auto_gc_hook_result auto_gc_hook_result = AUTO_GC_HOOK_UNDECIDED;\n \n \tif (repo_hold_lock_file_for_update(r, &lk, lock_path, LOCK_NO_DEREF) < 0) {\n+\t    int err = errno;\n \t\t/*\n \t\t * Another maintenance command is running.\n \t\t *\n@@ -1409,11 +1410,17 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n \t\t * recursive process stack. Do not report an error in\n \t\t * that case.\n \t\t */\n-\t\tif (!opts->auto_flag && !opts->quiet)\n-\t\t\twarning(_(\"lock file '%s' exists, skipping maintenance\"),\n-\t\t\t\tlock_path);\n+\t\tif (!opts->quiet) {\n+\t\t    struct strbuf buf = STRBUF_INIT;\n+\t\t\tunable_to_lock_message(lock_path, err, &buf);\n+\t\t\tif (opts->auto_flag)\n+\t\t\t\twarning(\"%s\", buf.buf);\n+\t\t\telse\n+\t\t\t\terror(\"%s\", buf.buf);\n+\t\t\tstrbuf_release(&buf);\n+\t\t}\n \t\tfree(lock_path);\n-\t\treturn 0;\n+\t\treturn opts->auto_flag ? 0 : -1;\n \t}\n \tfree(lock_path);\n \ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 9d610a29fe..3288efb391 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -387,6 +387,30 @@ test_expect_success 'loose-objects task' '\n \ttest_cmp packs-between packs-after\n '\n \n+test_expect_success 'auto maintenance warns when lock exists' '\n+\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\tgit maintenance run --auto --no-quiet 2>err &&\n+\ttest_grep \"Unable to create.*maintenance\\.lock\" err &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n+test_expect_success 'manual maintenance fails with existing lock' '\n+\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\ttest_must_fail git maintenance run --no-quiet 2>err &&\n+\ttest_grep \"maintenance\\.lock\" err &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n+test_expect_success 'manual maintenance with --quiet suppresses diagnostic' '\n+\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\ttest_must_fail git maintenance run --quiet 2>err &&\n+\ttest_must_be_empty err &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n test_expect_success 'maintenance.loose-objects.auto' '\n \tgit repack -adk &&\n \tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n-- \n2.53.0\n\n\n"},{"id":"557470","messageId":"20261104173422.20725-3-pushkarkumarsingh1970@gmail.com","threadId":"66736","inReplyTo":"20261104173422.20725-1-pushkarkumarsingh1970@gmail.com","subject":"[PATCH v3 2/2] maintenance: trace lock acquisition failures","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-04T17:34:22Z","receivedAt":"2026-11-04T17:34:22Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"When git maintenance run --auto fails to acquire the\nmaintenance lock, emit a Trace2 data event describing\nthe reason for the failure.\n\nThis makes it easier to identify cases where automatic\nmaintenance is repeatedly skipped.\n\nSigned-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>\n---\n builtin/gc.c           | 4 ++++\n t/t7900-maintenance.sh | 9 +++++++++\n 2 files changed, 13 insertions(+)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 9ca9a3584b..eaa0d71285 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1410,6 +1410,10 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n \t\t * recursive process stack. Do not report an error in\n \t\t * that case.\n \t\t */\n+\t\tif (opts->auto_flag)\n+\t\t\ttrace2_data_string(\"maintenance\", r, \"lock/skip\",\n+\t\t\t    strerror(err));\n+\n \t\tif (!opts->quiet) {\n \t\t    struct strbuf buf = STRBUF_INIT;\n \t\t\tunable_to_lock_message(lock_path, err, &buf);\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 3288efb391..294ab0db75 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -411,6 +411,15 @@ test_expect_success 'manual maintenance with --quiet suppresses diagnostic' '\n \ttest_path_is_file .git/objects/maintenance.lock\n '\n \n+test_expect_success 'auto maintenance traces existing lock' '\n+\ttest_when_finished \"rm -f trace.txt .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace.txt\" \\\n+\t\tgit maintenance run --auto --quiet &&\n+\ttest_trace2_data maintenance lock/skip \"File exists\" <trace.txt &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n test_expect_success 'maintenance.loose-objects.auto' '\n \tgit repack -adk &&\n \tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n-- \n2.53.0\n\n\n"},{"id":"557474","messageId":"20261104180006.404-1-aniketgupta3001@gmail.com","threadId":"66736","inReplyTo":"20261102122955.143018-2-pushkarkumarsingh1970@gmail.com","subject":"Re: [PATCH v2] maintenance: improve handling of lock acquisition failures","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-04T18:00:06Z","receivedAt":"2026-11-04T18:00:06Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"Hi Pushkar,\n\nI tried v2 while another maintenance run was holding the lock. The\nscheduled background runs, the ones set up by \"git maintenance start\",\nnow exit with an error instead of 0. Two overlapping runs end in a\nfailure where master just skips them and the docs say it is normal.\n\nAlso, running the prefetch task by hand now prints a lock warning and I\nthink it comes from the fetch it starts internally.\n\nThanks,\nAniket\n\n"},{"id":"558738","messageId":"20261118190735.3928-1-pushkarkumarsingh1970@gmail.com","threadId":"66736","inReplyTo":"20261104173422.20725-1-pushkarkumarsingh1970@gmail.com","subject":"[PATCH v4 0/2] maintenance: improve lock acquisition failure handling","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-18T19:07:33Z","receivedAt":"2026-11-18T19:07:33Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"Greetings,\n\nThis series improves how \"git maintenance\" handles failures to\nacquire the maintenance lock.\n\nThe first patch makes lock acquisition failures behave differently\ndepending on how maintenance was invoked. Manual invocations report\nan error and return a non-zero exit status, while automatic and\nscheduled invocations skip non-fatally. It also prevents the prefetch\ntask from triggering nested automatic maintenance through its child\nfetch process.\n\nThe second patch adds Trace2 instrumentation for automatic and\nscheduled maintenance skips, making it easier to identify repeated\nlock acquisition failures.\n\nChanges in v4:\n- Keep scheduled maintenance non-fatal when another process holds\n  the maintenance lock.\n- Prevent nested automatic maintenance in the prefetch task by\n  passing --no-auto-maintenance to child fetch processes.\n- Extend Trace2 instrumentation to cover scheduled maintenance skips.\n- Add regression tests for scheduled runs and nested prefetch\n  maintenance.\n\nThanks,\nPushkar\n\nPushkar Singh (2):\n  maintenance: handle lock acquisition failures correctly\n  maintenance: trace lock acquisition failures\n\n builtin/gc.c           | 27 +++++++++++++----\n t/t7900-maintenance.sh | 69 ++++++++++++++++++++++++++++++++++++++++--\n 2 files changed, 88 insertions(+), 8 deletions(-)\n\nRange-diff against v3:\n1:  ab33f1b432 ! 1:  dfebf3bea5 maintenance: report lock acquisition failures\n    @@ Metadata\n     Author: Pushkar Singh <pushkarkumarsingh1970@gmail.com>\n     \n      ## Commit message ##\n    -    maintenance: report lock acquisition failures\n    +    maintenance: handle lock acquisition failures correctly\n     \n    -    When git maintenance run fails to acquire the maintenance\n    -    lock, report the failure instead of silently succeeding.\n    +    When git maintenance run fails to acquire objects/maintenance.lock,\n    +    the failure should be handled appropriately depending on the invocation:\n     \n    -    For automatic invocations, print a warning while preserving\n    -    the successful exit status. For manual invocations, report\n    -    the failure as an error and return a non-zero exit status.\n    +    - Explicit manual maintenance should report an error and return a\n    +      non-zero exit status, or suppress the diagnostic when --quiet is set.\n    +    - Automatic maintenance (--auto) and scheduled maintenance (--schedule)\n    +      should remain non-fatal and exit successfully (0) to avoid failing\n    +      routine background runs when another maintenance process holds the lock.\n    +    - For automatic maintenance, print a warning diagnostic unless --quiet\n    +      is set, while scheduled maintenance remains silent.\n     \n    -    Respect the --quiet option when reporting the failure.\n    +    Use unable_to_lock_message() to format error and warning diagnostics,\n    +    and check opts->schedule to distinguish scheduled invocations from\n    +    manual runs.\n    +\n    +    In addition, pass --no-auto-maintenance to child fetch invocations in\n    +    fetch_remote() to prevent the prefetch task from triggering nested\n    +    automatic maintenance while the parent process holds the lock.\n    +\n    +    Add tests in t/t7900-maintenance.sh covering manual, quiet, automatic,\n    +    and scheduled lock acquisition handling, as well as prefetch child fetch\n    +    arguments.\n     \n         Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>\n     \n      ## builtin/gc.c ##\n    +@@ builtin/gc.c: static int fetch_remote(struct remote *remote, void *cbdata)\n    + \tstrvec_pushl(&child.args, \"fetch\", remote->name,\n    + \t\t     \"--prefetch\", \"--prune\", \"--no-tags\",\n    + \t\t     \"--no-write-fetch-head\", \"--recurse-submodules=no\",\n    ++\t\t     \"--no-auto-maintenance\",\n    + \t\t     NULL);\n    + \n    + \tif (opts->quiet)\n     @@ builtin/gc.c: static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n      \tenum auto_gc_hook_result auto_gc_hook_result = AUTO_GC_HOOK_UNDECIDED;\n      \n      \tif (repo_hold_lock_file_for_update(r, &lk, lock_path, LOCK_NO_DEREF) < 0) {\n    -+\t    int err = errno;\n    ++\t\tint err = errno;\n    ++\n      \t\t/*\n      \t\t * Another maintenance command is running.\n      \t\t *\n    -@@ builtin/gc.c: static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n    - \t\t * recursive process stack. Do not report an error in\n    - \t\t * that case.\n    + \t\t * If --auto was provided, then it is likely due to a\n    +-\t\t * recursive process stack. Do not report an error in\n    +-\t\t * that case.\n    ++\t\t * recursive process stack. If --schedule was provided,\n    ++\t\t * another scheduled run is already running. Do not\n    ++\t\t * report an error in either case.\n      \t\t */\n     -\t\tif (!opts->auto_flag && !opts->quiet)\n     -\t\t\twarning(_(\"lock file '%s' exists, skipping maintenance\"),\n     -\t\t\t\tlock_path);\n     +\t\tif (!opts->quiet) {\n    -+\t\t    struct strbuf buf = STRBUF_INIT;\n    ++\t\t\tstruct strbuf buf = STRBUF_INIT;\n    ++\n     +\t\t\tunable_to_lock_message(lock_path, err, &buf);\n     +\t\t\tif (opts->auto_flag)\n     +\t\t\t\twarning(\"%s\", buf.buf);\n    -+\t\t\telse\n    ++\t\t\telse if (!opts->schedule)\n     +\t\t\t\terror(\"%s\", buf.buf);\n     +\t\t\tstrbuf_release(&buf);\n     +\t\t}\n      \t\tfree(lock_path);\n     -\t\treturn 0;\n    -+\t\treturn opts->auto_flag ? 0 : -1;\n    ++\t\treturn (opts->auto_flag || opts->schedule) ? 0 : -1;\n      \t}\n      \tfree(lock_path);\n      \n     \n      ## t/t7900-maintenance.sh ##\n    +@@ t/t7900-maintenance.sh: test_expect_success 'prefetch multiple remotes' '\n    + \ttest_commit -C clone1 one &&\n    + \ttest_commit -C clone2 two &&\n    + \tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n    +-\tfetchargs=\"--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet\" &&\n    ++\tfetchargs=\"--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --no-auto-maintenance --quiet\" &&\n    + \ttest_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&\n    + \ttest_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&\n    ++\ttest_subcommand ! git maintenance run --auto <run-prefetch.txt &&\n    + \tgit for-each-ref refs/remotes >actual &&\n    + \ttest_must_be_empty actual &&\n    + \tgit log prefetch/remotes/remote1/one &&\n    +@@ t/t7900-maintenance.sh: test_expect_success 'prefetch multiple remotes' '\n    + \tgit config remote.remote1.skipFetchAll true &&\n    + \tGIT_TRACE2_EVENT=\"$(pwd)/skip-remote1.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n    + \ttest_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&\n    +-\ttest_subcommand git fetch remote2 $fetchargs <skip-remote1.txt\n    ++\ttest_subcommand git fetch remote2 $fetchargs <skip-remote1.txt &&\n    ++\n    ++\tgit maintenance run --task=prefetch --no-quiet 2>err &&\n    ++\ttest_must_be_empty err\n    + '\n    + \n    + test_expect_success 'loose-objects task' '\n     @@ t/t7900-maintenance.sh: test_expect_success 'loose-objects task' '\n      \ttest_cmp packs-between packs-after\n      '\n    @@ t/t7900-maintenance.sh: test_expect_success 'loose-objects task' '\n     +\ttest_must_be_empty err &&\n     +\ttest_path_is_file .git/objects/maintenance.lock\n     +'\n    ++\n    ++test_expect_success 'scheduled maintenance skips with existing lock' '\n    ++\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n    ++\t: >.git/objects/maintenance.lock &&\n    ++\tgit maintenance run --schedule=hourly 2>err &&\n    ++\ttest_must_be_empty err &&\n    ++\ttest_path_is_file .git/objects/maintenance.lock\n    ++'\n     +\n      test_expect_success 'maintenance.loose-objects.auto' '\n      \tgit repack -adk &&\n2:  b451682115 ! 2:  a1491d0123 maintenance: trace lock acquisition failures\n    @@ Metadata\n      ## Commit message ##\n         maintenance: trace lock acquisition failures\n     \n    -    When git maintenance run --auto fails to acquire the\n    -    maintenance lock, emit a Trace2 data event describing\n    -    the reason for the failure.\n    +    When git maintenance run fails to acquire the maintenance lock during\n    +    automatic or scheduled execution, it skips running maintenance tasks\n    +    non-fatally and exits successfully.\n     \n    -    This makes it easier to identify cases where automatic\n    -    maintenance is repeatedly skipped.\n    +    Emit a Trace2 data event with category \"maintenance\" and key\n    +    \"lock/skip\" describing the OS error via strerror(err) when automatic or\n    +    scheduled maintenance skips due to a lock acquisition failure. This\n    +    makes it easier to identify situations where automatic or scheduled\n    +    background maintenance is repeatedly skipped due to lock contention or\n    +    stale locks, while preserving standard non-zero exit and error reporting\n    +    for manual invocations.\n    +\n    +    Add tests in t/t7900-maintenance.sh to verify that automatic and\n    +    scheduled maintenance emit the expected Trace2 event upon encountering\n    +    an existing lock, and that explicit manual maintenance does not emit\n    +    this skip event.\n     \n         Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>\n     \n      ## builtin/gc.c ##\n     @@ builtin/gc.c: static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n    - \t\t * recursive process stack. Do not report an error in\n    - \t\t * that case.\n    + \t\t * another scheduled run is already running. Do not\n    + \t\t * report an error in either case.\n      \t\t */\n    -+\t\tif (opts->auto_flag)\n    ++\t\tif (opts->auto_flag || opts->schedule)\n     +\t\t\ttrace2_data_string(\"maintenance\", r, \"lock/skip\",\n    -+\t\t\t    strerror(err));\n    ++\t\t\t\t\t   strerror(err));\n     +\n      \t\tif (!opts->quiet) {\n    - \t\t    struct strbuf buf = STRBUF_INIT;\n    - \t\t\tunable_to_lock_message(lock_path, err, &buf);\n    + \t\t\tstruct strbuf buf = STRBUF_INIT;\n    + \n     \n      ## t/t7900-maintenance.sh ##\n    -@@ t/t7900-maintenance.sh: test_expect_success 'manual maintenance with --quiet suppresses diagnostic' '\n    +@@ t/t7900-maintenance.sh: test_expect_success 'scheduled maintenance skips with existing lock' '\n      \ttest_path_is_file .git/objects/maintenance.lock\n      '\n      \n    @@ t/t7900-maintenance.sh: test_expect_success 'manual maintenance with --quiet sup\n     +\ttest_trace2_data maintenance lock/skip \"File exists\" <trace.txt &&\n     +\ttest_path_is_file .git/objects/maintenance.lock\n     +'\n    ++\n    ++test_expect_success 'scheduled maintenance traces existing lock' '\n    ++\ttest_when_finished \"rm -f trace.txt err .git/objects/maintenance.lock\" &&\n    ++\t: >.git/objects/maintenance.lock &&\n    ++\tGIT_TRACE2_EVENT=\"$(pwd)/trace.txt\" \\\n    ++\t\tgit maintenance run --schedule=hourly 2>err &&\n    ++\ttest_must_be_empty err &&\n    ++\ttest_trace2_data maintenance lock/skip \"File exists\" <trace.txt &&\n    ++\ttest_path_is_file .git/objects/maintenance.lock\n    ++'\n    ++\n    ++test_expect_success 'manual maintenance does not trace lock/skip' '\n    ++\ttest_when_finished \"rm -f trace.txt err .git/objects/maintenance.lock\" &&\n    ++\t: >.git/objects/maintenance.lock &&\n    ++\tGIT_TRACE2_EVENT=\"$(pwd)/trace.txt\" \\\n    ++\t\ttest_must_fail git maintenance run --quiet 2>err &&\n    ++\ttest_must_be_empty err &&\n    ++\t! grep -e \"\\\"category\\\":\\\"maintenance\\\",\\\"key\\\":\\\"lock/skip\\\"\" trace.txt &&\n    ++\ttest_path_is_file .git/objects/maintenance.lock\n    ++'\n     +\n      test_expect_success 'maintenance.loose-objects.auto' '\n      \tgit repack -adk &&\n\nbase-commit: 3ef89b7020c1f4add19622af30f40ecc937af610\n-- \n2.53.0\n\n\n"},{"id":"558739","messageId":"20261118190735.3928-2-pushkarkumarsingh1970@gmail.com","threadId":"66736","inReplyTo":"20261118190735.3928-1-pushkarkumarsingh1970@gmail.com","subject":"[PATCH v4 1/2] maintenance: handle lock acquisition failures correctly","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-18T19:07:34Z","receivedAt":"2026-11-18T19:07:34Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"When git maintenance run fails to acquire objects/maintenance.lock,\nthe failure should be handled appropriately depending on the invocation:\n\n- Explicit manual maintenance should report an error and return a\n  non-zero exit status, or suppress the diagnostic when --quiet is set.\n- Automatic maintenance (--auto) and scheduled maintenance (--schedule)\n  should remain non-fatal and exit successfully (0) to avoid failing\n  routine background runs when another maintenance process holds the lock.\n- For automatic maintenance, print a warning diagnostic unless --quiet\n  is set, while scheduled maintenance remains silent.\n\nUse unable_to_lock_message() to format error and warning diagnostics,\nand check opts->schedule to distinguish scheduled invocations from\nmanual runs.\n\nIn addition, pass --no-auto-maintenance to child fetch invocations in\nfetch_remote() to prevent the prefetch task from triggering nested\nautomatic maintenance while the parent process holds the lock.\n\nAdd tests in t/t7900-maintenance.sh covering manual, quiet, automatic,\nand scheduled lock acquisition handling, as well as prefetch child fetch\narguments.\n\nSigned-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>\n---\n builtin/gc.c           | 23 +++++++++++++++++------\n t/t7900-maintenance.sh | 40 ++++++++++++++++++++++++++++++++++++++--\n 2 files changed, 55 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 81cecf6b6c..d423b68cce 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -902,6 +902,7 @@ static int fetch_remote(struct remote *remote, void *cbdata)\n \tstrvec_pushl(&child.args, \"fetch\", remote->name,\n \t\t     \"--prefetch\", \"--prune\", \"--no-tags\",\n \t\t     \"--no-write-fetch-head\", \"--recurse-submodules=no\",\n+\t\t     \"--no-auto-maintenance\",\n \t\t     NULL);\n \n \tif (opts->quiet)\n@@ -1402,18 +1403,28 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n \tenum auto_gc_hook_result auto_gc_hook_result = AUTO_GC_HOOK_UNDECIDED;\n \n \tif (repo_hold_lock_file_for_update(r, &lk, lock_path, LOCK_NO_DEREF) < 0) {\n+\t\tint err = errno;\n+\n \t\t/*\n \t\t * Another maintenance command is running.\n \t\t *\n \t\t * If --auto was provided, then it is likely due to a\n-\t\t * recursive process stack. Do not report an error in\n-\t\t * that case.\n+\t\t * recursive process stack. If --schedule was provided,\n+\t\t * another scheduled run is already running. Do not\n+\t\t * report an error in either case.\n \t\t */\n-\t\tif (!opts->auto_flag && !opts->quiet)\n-\t\t\twarning(_(\"lock file '%s' exists, skipping maintenance\"),\n-\t\t\t\tlock_path);\n+\t\tif (!opts->quiet) {\n+\t\t\tstruct strbuf buf = STRBUF_INIT;\n+\n+\t\t\tunable_to_lock_message(lock_path, err, &buf);\n+\t\t\tif (opts->auto_flag)\n+\t\t\t\twarning(\"%s\", buf.buf);\n+\t\t\telse if (!opts->schedule)\n+\t\t\t\terror(\"%s\", buf.buf);\n+\t\t\tstrbuf_release(&buf);\n+\t\t}\n \t\tfree(lock_path);\n-\t\treturn 0;\n+\t\treturn (opts->auto_flag || opts->schedule) ? 0 : -1;\n \t}\n \tfree(lock_path);\n \ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 9d610a29fe..4e519e4a5d 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -327,9 +327,10 @@ test_expect_success 'prefetch multiple remotes' '\n \ttest_commit -C clone1 one &&\n \ttest_commit -C clone2 two &&\n \tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n-\tfetchargs=\"--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet\" &&\n+\tfetchargs=\"--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --no-auto-maintenance --quiet\" &&\n \ttest_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&\n \ttest_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&\n+\ttest_subcommand ! git maintenance run --auto <run-prefetch.txt &&\n \tgit for-each-ref refs/remotes >actual &&\n \ttest_must_be_empty actual &&\n \tgit log prefetch/remotes/remote1/one &&\n@@ -345,7 +346,10 @@ test_expect_success 'prefetch multiple remotes' '\n \tgit config remote.remote1.skipFetchAll true &&\n \tGIT_TRACE2_EVENT=\"$(pwd)/skip-remote1.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n \ttest_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&\n-\ttest_subcommand git fetch remote2 $fetchargs <skip-remote1.txt\n+\ttest_subcommand git fetch remote2 $fetchargs <skip-remote1.txt &&\n+\n+\tgit maintenance run --task=prefetch --no-quiet 2>err &&\n+\ttest_must_be_empty err\n '\n \n test_expect_success 'loose-objects task' '\n@@ -387,6 +391,38 @@ test_expect_success 'loose-objects task' '\n \ttest_cmp packs-between packs-after\n '\n \n+test_expect_success 'auto maintenance warns when lock exists' '\n+\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\tgit maintenance run --auto --no-quiet 2>err &&\n+\ttest_grep \"Unable to create.*maintenance\\.lock\" err &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n+test_expect_success 'manual maintenance fails with existing lock' '\n+\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\ttest_must_fail git maintenance run --no-quiet 2>err &&\n+\ttest_grep \"maintenance\\.lock\" err &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n+test_expect_success 'manual maintenance with --quiet suppresses diagnostic' '\n+\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\ttest_must_fail git maintenance run --quiet 2>err &&\n+\ttest_must_be_empty err &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n+test_expect_success 'scheduled maintenance skips with existing lock' '\n+\ttest_when_finished \"rm -f err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\tgit maintenance run --schedule=hourly 2>err &&\n+\ttest_must_be_empty err &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n test_expect_success 'maintenance.loose-objects.auto' '\n \tgit repack -adk &&\n \tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n-- \n2.53.0\n\n\n"},{"id":"558740","messageId":"20261118190735.3928-3-pushkarkumarsingh1970@gmail.com","threadId":"66736","inReplyTo":"20261118190735.3928-1-pushkarkumarsingh1970@gmail.com","subject":"[PATCH v4 2/2] maintenance: trace lock acquisition failures","fromName":"Pushkar Singh","fromEmail":"pushkarkumarsingh1970@gmail.com","sentAt":"2026-11-18T19:07:35Z","receivedAt":"2026-11-18T19:07:35Z","isPatch":true,"sender":{"key":"pushkarkumarsingh1970@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173247767?v=4"},"body":"When automatic or scheduled maintenance fails to acquire the\nmaintenance lock, emit a Trace2 data event describing the reason\nfor the failure.\n\nThe event uses the \"maintenance\" category and \"lock/skip\" key,\nwith the OS error reported via strerror(err).\n\nThis makes it easier to identify cases where automatic or\nscheduled maintenance is repeatedly skipped due to lock\nacquisition failures.\n\nSigned-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com>\n---\n builtin/gc.c           |  4 ++++\n t/t7900-maintenance.sh | 29 +++++++++++++++++++++++++++++\n 2 files changed, 33 insertions(+)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex d423b68cce..32d6f9c473 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1413,6 +1413,10 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,\n \t\t * another scheduled run is already running. Do not\n \t\t * report an error in either case.\n \t\t */\n+\t\tif (opts->auto_flag || opts->schedule)\n+\t\t\ttrace2_data_string(\"maintenance\", r, \"lock/skip\",\n+\t\t\t\t\t   strerror(err));\n+\n \t\tif (!opts->quiet) {\n \t\t\tstruct strbuf buf = STRBUF_INIT;\n \ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 4e519e4a5d..959c99a51f 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -423,6 +423,35 @@ test_expect_success 'scheduled maintenance skips with existing lock' '\n \ttest_path_is_file .git/objects/maintenance.lock\n '\n \n+test_expect_success 'auto maintenance traces existing lock' '\n+\ttest_when_finished \"rm -f trace.txt .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace.txt\" \\\n+\t\tgit maintenance run --auto --quiet &&\n+\ttest_trace2_data maintenance lock/skip \"File exists\" <trace.txt &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n+test_expect_success 'scheduled maintenance traces existing lock' '\n+\ttest_when_finished \"rm -f trace.txt err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace.txt\" \\\n+\t\tgit maintenance run --schedule=hourly 2>err &&\n+\ttest_must_be_empty err &&\n+\ttest_trace2_data maintenance lock/skip \"File exists\" <trace.txt &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n+test_expect_success 'manual maintenance does not trace lock/skip' '\n+\ttest_when_finished \"rm -f trace.txt err .git/objects/maintenance.lock\" &&\n+\t: >.git/objects/maintenance.lock &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace.txt\" \\\n+\t\ttest_must_fail git maintenance run --quiet 2>err &&\n+\ttest_must_be_empty err &&\n+\t! grep -e \"\\\"category\\\":\\\"maintenance\\\",\\\"key\\\":\\\"lock/skip\\\"\" trace.txt &&\n+\ttest_path_is_file .git/objects/maintenance.lock\n+'\n+\n test_expect_success 'maintenance.loose-objects.auto' '\n \tgit repack -adk &&\n \tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n-- \n2.53.0\n\n\n"}]}