{"thread":{"id":"66520","subject":"[PATCH 0/2] Add a way to wait for index lock","startedAt":"2026-10-13T17:23:13Z","lastAt":"2026-10-22T18:49:06Z","messageCount":20,"participants":["Jun Wu","D. Ben Knoble","Junio C Hamano","Jeff King","Ben Knoble","Mihai Cosma"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"554881","messageId":"20261013172315.2997446-1-quark.zju@gmail.com","threadId":"66520","inReplyTo":null,"subject":"[PATCH 0/2] Add a way to wait for index lock","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-13T17:23:13Z","receivedAt":"2026-10-13T17:23:13Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"From: Jun Wu <quark@meta.com>\n\nAn IDE that runs `git status` periodically in the background has 2\nchoices. Both are suboptimal:\n- with `--no-optional-locks`: `status` becomes slower over time.\n- without `--no-optional-locks`: conflicts with other commands like\n  `add`, `commit`, often run by a user from the IDE terminal.\n\nPatch 1 adds index lock timeout so `add` can wait instead of fail.\nPatch 2 adjusts the read/lock order so `commit` can also wait.\n\nTo minimize write race, if the index is read before locking, the\nwait will be skipped, as if timeout is 0. Depending on the read/lock\norder, some commands (e.g. add, rm, mv, checkout, restore, read-tree,\napply, update-index) respect the timeout. Some (e.g. reset, stash,\nmerge, rebase, cherry-pick, am) do not. I started with the simple and\ncommon commands. If there is interest in other commands I can take a\nlook too.\n\nThe default timeout is 1s (reasoning in patch 1). I'm happy to change it\nto 0 if people prefer no behavior change.\n\nRelated context:\n- (2017-9-22) https://lore.kernel.org/git/20170923033425.6ilykxl5pi7unlj6@sigill.intra.peff.net/\n  proposed index lock timeout.\n- (2017-9-27) Commit 27344d6a6c (git: add --no-optional-locks option, 2017-09-27)\n  introduces `--no-optional-locks` for the same issue.\n- (2019-5-3) https://lore.kernel.org/git/CACsJy8BcBk6Fsfy7o1aBTQAPeH-w+rBwLZrzw4jJ-OM_jhkxMQ@mail.gmail.com/\n  mentioned `update_if_able` and \"gap\" between read and lock, mitigated\n  in patch 1 with the `index->initialized` check.\n\nJun Wu (2):\n  repository: add core.indexLockTimeout\n  commit: lock index before reading\n\n Documentation/config/core.adoc |  6 ++++++\n builtin/commit.c               |  9 ++-------\n repository.c                   | 17 +++++++++++++++-\n t/meson.build                  |  1 +\n t/t0031-lockfile-pid.sh        |  4 ++++\n t/t0032-index-lock-timeout.sh  | 37 ++++++++++++++++++++++++++++++++++\n t/t7400-submodule-basic.sh     |  1 +\n 7 files changed, 67 insertions(+), 8 deletions(-)\n create mode 100755 t/t0032-index-lock-timeout.sh\n\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\n-- \n2.56.0\n\n\n"},{"id":"554882","messageId":"20261013172315.2997446-2-quark.zju@gmail.com","threadId":"66520","inReplyTo":"20261013172315.2997446-1-quark.zju@gmail.com","subject":"[PATCH 1/2] repository: add core.indexLockTimeout","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-13T17:23:14Z","receivedAt":"2026-10-13T17:23:14Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"From: Jun Wu <quark@meta.com>\n\nWhen an IDE runs `git status` periodically in the background, `status`\ncan take the index lock, and this prevents other commands like `add`,\n`commit` from running.\n\nCommit 27344d6a6c (git: add --no-optional-locks option, 2017-09-27)\navoids the conflict by letting background `git --no-optional-locks status`\nskip the index lock, at the cost of losing refreshed stat info, so\nthe next `status` has to re-hash files.\n\nApproach the conflict from the other side: let non-status commands wait\nfor the index lock. Mercurial and Sapling have a similar \"waiting for wlock\"\nbehavior, and Git already has `core.{config,filesRef}LockTimeout`.\n\nIdeally all non-status commands can wait for the index lock. However,\nsome code paths (e.g. `commit`) load the index without locking, then lock and\nwrite changes. It is already racy and the wait behavior would make it much\nworse. As a defense, waiting is disabled if index was already read.\n\nDefault timeout is 1s, effective but not too long to avoid breaking potential\nuser expectation (e.g. \"an index.lock error means I should check whether a\ncommit message editor is open somewhere\"). Tests that are slowed down by the\ndefault timeout now set the timeout to 0.\n\nSigned-off-by: Jun Wu <quark@meta.com>\n---\n Documentation/config/core.adoc |  6 ++++++\n repository.c                   | 17 +++++++++++++++-\n t/meson.build                  |  1 +\n t/t0031-lockfile-pid.sh        |  4 ++++\n t/t0032-index-lock-timeout.sh  | 37 ++++++++++++++++++++++++++++++++++\n t/t7400-submodule-basic.sh     |  1 +\n 6 files changed, 65 insertions(+), 1 deletion(-)\n create mode 100755 t/t0032-index-lock-timeout.sh\n\ndiff --git a/Documentation/config/core.adoc b/Documentation/config/core.adoc\nindex 0b697f53f1..d06990b7ea 100644\n--- a/Documentation/config/core.adoc\n+++ b/Documentation/config/core.adoc\n@@ -604,6 +604,12 @@ core.configLockTimeout::\n \tthat is already on disk before the lock is taken, so it can be\n \tset persistently like any other option.\n \n+core.indexLockTimeout::\n+\tThe length of time, in milliseconds, to retry when trying to\n+\tlock the index file for writing. Value 0 means not to retry at\n+\tall; -1 means to try indefinitely. Default is 1000 (i.e., retry\n+\tfor 1 second). Not supported by all commands.\n+\n core.pager::\n \tText viewer for use by Git commands (e.g., 'less').  The value\n \tis meant to be interpreted by the shell.  The order of preference\ndiff --git a/repository.c b/repository.c\nindex b857e1c580..3eaea3143c 100644\n--- a/repository.c\n+++ b/repository.c\n@@ -473,7 +473,22 @@ int repo_hold_locked_index(struct repository *repo,\n \t\t\t   struct lock_file *lf,\n \t\t\t   int flags)\n {\n+\tint timeout_ms;\n+\n \tif (!repo->index_file)\n \t\tBUG(\"the repo hasn't been setup\");\n-\treturn repo_hold_lock_file_for_update(repo, lf, repo->index_file, flags);\n+\n+\tif (repo->index->initialized) {\n+\t\t/*\n+\t\t * Index was initialized (read) without lock. To minimize the\n+\t\t * chance of lost update, do not wait.\n+\t\t */\n+\t\ttimeout_ms = 0;\n+\t} else {\n+\t\ttimeout_ms = 1000;\n+\t\trepo_config_get_int(repo, \"core.indexlocktimeout\", &timeout_ms);\n+\t}\n+\n+\treturn repo_hold_lock_file_for_update_timeout(repo, lf, repo->index_file,\n+\t\t\t\t\t\t      flags, timeout_ms);\n }\ndiff --git a/t/meson.build b/t/meson.build\nindex f65eb04684..d4debf674e 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -102,6 +102,7 @@ integration_tests = [\n   't0029-core-unsetenvvars.sh',\n   't0030-stripspace.sh',\n   't0031-lockfile-pid.sh',\n+  't0032-index-lock-timeout.sh',\n   't0033-safe-directory.sh',\n   't0034-root-safe-directory.sh',\n   't0035-safe-bare-repository.sh',\ndiff --git a/t/t0031-lockfile-pid.sh b/t/t0031-lockfile-pid.sh\nindex e9e2f04049..3fc8de02b6 100755\n--- a/t/t0031-lockfile-pid.sh\n+++ b/t/t0031-lockfile-pid.sh\n@@ -8,6 +8,10 @@ The feature is opt-in via core.lockfilePid config setting (boolean).\n \n . ./test-lib.sh\n \n+test_expect_success 'setup' '\n+\tgit config --global core.indexLockTimeout 0\n+'\n+\n test_expect_success 'stale lock detected when PID is not running' '\n \tgit init repo &&\n \t(\ndiff --git a/t/t0032-index-lock-timeout.sh b/t/t0032-index-lock-timeout.sh\nnew file mode 100755\nindex 0000000000..ca334c3eef\n--- /dev/null\n+++ b/t/t0032-index-lock-timeout.sh\n@@ -0,0 +1,37 @@\n+#!/bin/sh\n+\n+test_description='Test config core.indexLockTimeout'\n+\n+. ./test-lib.sh\n+\n+prepare_index_lock () {\n+\ttest_when_finished \"rm -f .git/index.lock\" &&\n+\ttouch .git/index.lock &&\n+\t{\n+\t\t( sleep 1 && rm -f .git/index.lock ) &\n+\t} &&\n+\ttest_when_finished \"wait\"\n+}\n+\n+test_must_fail_with_index_lock () {\n+\ttest_must_fail \"$@\" 2>err &&\n+\ttest_grep index.lock err\n+}\n+\n+test_expect_success 'index locked, add fails with indexLockTimeout=0' '\n+\tprepare_index_lock &&\n+\ttouch foo &&\n+\ttest_must_fail_with_index_lock git -c core.indexLockTimeout=0 add foo\n+'\n+\n+test_expect_success 'index locked, add waits with indexLockTimeout>0' '\n+\tprepare_index_lock &&\n+\tgit -c core.indexLockTimeout=5000 add foo\n+'\n+\n+test_expect_success 'index locked, commit fails even with indexLockTimeout>0 (read index before lock)' '\n+\tprepare_index_lock &&\n+\ttest_must_fail_with_index_lock git -c core.indexLockTimeout=5000 commit -m init\n+'\n+\n+test_done\ndiff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh\nindex eefdecb0bd..9a90b6a76d 100755\n--- a/t/t7400-submodule-basic.sh\n+++ b/t/t7400-submodule-basic.sh\n@@ -276,6 +276,7 @@ test_expect_success 'submodule add to reconfigure existing submodule with --forc\n '\n \n test_expect_success 'submodule add relays add --dry-run stderr' '\n+\ttest_config_global core.indexLockTimeout 0 &&\n \ttest_when_finished \"rm -rf addtest/.git/index.lock\" &&\n \t(\n \t\tcd addtest &&\n-- \n2.56.0\n\n\n"},{"id":"554883","messageId":"20261013172315.2997446-3-quark.zju@gmail.com","threadId":"66520","inReplyTo":"20261013172315.2997446-1-quark.zju@gmail.com","subject":"[PATCH 2/2] commit: lock index before reading","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-13T17:23:15Z","receivedAt":"2026-10-13T17:23:15Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"From: Jun Wu <quark@meta.com>\n\nTake the index lock before reading the index, in one place, instead\nof separately in each `if` branch.\n\n`commit` now respects the `core.indexLockTimeout` config.\n\nHistory suggests no particular reason for reading before locking:\n- f5bbc3225c (Port git commit to C., 2007-11-08): lock before reading\n- 2888605c64 (builtin-commit: fix partial-commit support, 2007-11-18):\n  seems to accidentally move reading to before locking\n\nSigned-off-by: Jun Wu <quark@meta.com>\n---\n builtin/commit.c              | 9 ++-------\n t/t0032-index-lock-timeout.sh | 4 ++--\n 2 files changed, 4 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 840b6b4083..08dd53ad4e 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -389,13 +389,13 @@ static const char *prepare_index(const char **argv, const char *prefix,\n \t    (!amend || (fixup_message && strcmp(fixup_prefix, \"amend\"))))))\n \t\tdie(_(\"No paths with --include/--only does not make sense.\"));\n \n+\trepo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);\n+\n \tif (repo_read_index_preload(the_repository, &pathspec, 0) < 0)\n \t\tdie(_(\"index file corrupt\"));\n \n \tif (interactive) {\n \t\tchar *old_index_env = NULL, *old_repo_index_file;\n-\t\trepo_hold_locked_index(the_repository, &index_lock,\n-\t\t\t\t       LOCK_DIE_ON_ERROR);\n \n \t\trefresh_cache_or_die(refresh_flags);\n \n@@ -453,8 +453,6 @@ static const char *prepare_index(const char **argv, const char *prefix,\n \t */\n \tif (all || (also && pathspec.nr)) {\n \t\tchar *ps_matched = xcalloc(pathspec.nr, 1);\n-\t\trepo_hold_locked_index(the_repository, &index_lock,\n-\t\t\t\t       LOCK_DIE_ON_ERROR);\n \t\tadd_files_to_cache(the_repository, also ? prefix : NULL,\n \t\t\t\t   &pathspec, ps_matched, 0, 0, 0 );\n \t\tif (!all && report_path_error(ps_matched, &pathspec))\n@@ -480,8 +478,6 @@ static const char *prepare_index(const char **argv, const char *prefix,\n \t * We still need to refresh the index here.\n \t */\n \tif (!only && !pathspec.nr) {\n-\t\trepo_hold_locked_index(the_repository, &index_lock,\n-\t\t\t\t       LOCK_DIE_ON_ERROR);\n \t\trefresh_cache_or_die(refresh_flags);\n \t\tif (the_repository->index->cache_changed\n \t\t    || !cache_tree_fully_valid(the_repository->index))\n@@ -531,7 +527,6 @@ static const char *prepare_index(const char **argv, const char *prefix,\n \tif (repo_read_index(the_repository) < 0)\n \t\tdie(_(\"cannot read the index\"));\n \n-\trepo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);\n \tadd_remove_files(&partial);\n \trefresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);\n \tcache_tree_update(the_repository->index, WRITE_TREE_SILENT);\ndiff --git a/t/t0032-index-lock-timeout.sh b/t/t0032-index-lock-timeout.sh\nindex ca334c3eef..0c36a7cdfe 100755\n--- a/t/t0032-index-lock-timeout.sh\n+++ b/t/t0032-index-lock-timeout.sh\n@@ -29,9 +29,9 @@ test_expect_success 'index locked, add waits with indexLockTimeout>0' '\n \tgit -c core.indexLockTimeout=5000 add foo\n '\n \n-test_expect_success 'index locked, commit fails even with indexLockTimeout>0 (read index before lock)' '\n+test_expect_success 'index locked, commit waits with indexLockTimeout>0' '\n \tprepare_index_lock &&\n-\ttest_must_fail_with_index_lock git -c core.indexLockTimeout=5000 commit -m init\n+\tgit -c core.indexLockTimeout=5000 commit -m init\n '\n \n test_done\n-- \n2.56.0\n\n\n"},{"id":"554920","messageId":"CALnO6CD8c+nZktineZjo6PkZbXXEQfKto-cvJvLW=_xjTPVCRA@mail.gmail.com","threadId":"66520","inReplyTo":"20261013172315.2997446-2-quark.zju@gmail.com","subject":"Re: [PATCH 1/2] repository: add core.indexLockTimeout","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-13T21:40:01Z","receivedAt":"2026-10-13T21:40:01Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"Hi Jun,\n\nOn Tue, Oct 13, 2026 at 5:07 PM Jun Wu <quark.zju@gmail.com> wrote:\n>\n> From: Jun Wu <quark@meta.com>\n>\n> When an IDE runs `git status` periodically in the background, `status`\n> can take the index lock, and this prevents other commands like `add`,\n> `commit` from running.\n>\n> Commit 27344d6a6c (git: add --no-optional-locks option, 2017-09-27)\n> avoids the conflict by letting background `git --no-optional-locks status`\n> skip the index lock, at the cost of losing refreshed stat info, so\n> the next `status` has to re-hash files.\n\nInteresting. That must be the slow down you mentioned in your cover\nletter? I'm trying to make sense of this. I think you're saying that\nnot having the index lock means status would \"lose\" refreshed info:\nwe're not throwing away the *existing* stat info, just the ones we\nrecomputed for this \"git --no-optional-locks status\" invocation,\nright?\n\nI suppose I don't see how that causes things to slow down continually,\nbut I bet I'm missing something :)\n\n> Approach the conflict from the other side: let non-status commands wait\n> for the index lock. Mercurial and Sapling have a similar \"waiting for wlock\"\n> behavior, and Git already has `core.{config,filesRef}LockTimeout`.\n>\n> Ideally all non-status commands can wait for the index lock. However,\n> some code paths (e.g. `commit`) load the index without locking, then lock and\n> write changes. It is already racy and the wait behavior would make it much\n> worse. As a defense, waiting is disabled if index was already read.\n>\n> Default timeout is 1s, effective but not too long to avoid breaking potential\n> user expectation (e.g. \"an index.lock error means I should check whether a\n> commit message editor is open somewhere\"). Tests that are slowed down by the\n> default timeout now set the timeout to 0.\n\nIt feels weird to me to fix mostly-invisible behavior from external\ntools with a config option, though. I think of config *mostly* as\nuser-facing (whether that user is a client of the Git CLI or\nconfiguring a server repo), and I don't see how we could teach users\nto look for this or know what a reasonable setting should be for\nthemselves :/ Further, we already have a lot of config options! (I'm\nguilty of adding one recently, heh.) It can definitely be overwhelming\nfor users to think about all the options, and I suspect many just\ndon't as a result.\n\nI applaud the effort for sensible defaults here, which I think will help.\n\nAnyway, maybe it's just me: if others are happy with this approach,\ngreat. I think I would prefer to see us try to address the underlying\nperformance problems, though, if we can. (Maybe we can have some way\nfor git-status to not throw away all its work, or some other good way\nto communicate to IDEs that want to poll?)\n\n"},{"id":"554922","messageId":"xmqqh5ipuuhk.fsf@gitster.g","threadId":"66520","inReplyTo":"20261013172315.2997446-1-quark.zju@gmail.com","subject":"Re: [PATCH 0/2] Add a way to wait for index lock","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-13T22:05:27Z","receivedAt":"2026-10-13T22:05:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jun Wu <quark.zju@gmail.com> writes:\n\n> From: Jun Wu <quark@meta.com>\n>\n> An IDE that runs `git status` periodically in the background has 2\n> choices. Both are suboptimal:\n> - with `--no-optional-locks`: `status` becomes slower over time.\n> - without `--no-optional-locks`: conflicts with other commands like\n>   `add`, `commit`, often run by a user from the IDE terminal.\n\nIt is unfair to expect that the readers would guess what you left\nunsaid here, which probably is \"we will introduce the third choice,\nwhich does such and such things, and it improves the situation in\nsuch and such ways because ...\", before listing ...\n\n> Patch 1 adds index lock timeout so `add` can wait instead of fail.\n> Patch 2 adjusts the read/lock order so `commit` can also wait.\n\n... these.\n\n> To minimize write race, if the index is read before locking, the\n> wait will be skipped, as if timeout is 0. Depending on the read/lock\n> order, some commands (e.g. add, rm, mv, checkout, restore, read-tree,\n> apply, update-index) respect the timeout. Some (e.g. reset, stash,\n> merge, rebase, cherry-pick, am) do not. I started with the simple and\n> common commands. If there is interest in other commands I can take a\n> look too.\n\nIs the above supposed to be the description of the proposed solution?\n\nThere are multiple parties (i.e., `git status` that needed to\ndiscard the result with \"--no-optional-locks\" and other end-user\nactions that do need to safely update the index) involved, and it is\nunclear whose actions many of verbs in your writing refer to in the\nabove.  E.g., \"if the index is read before locking, the wait will be\nskipped\" for whom?  \"git status\" that wants to opportunistically\nwrite out the result of refreshing?  End-user actions like \"git add\"\nthat wanted to register new contents to the index?  Both?\n\n> The default timeout is 1s (reasoning in patch 1). I'm happy to change it\n> to 0 if people prefer no behavior change.\n>\n> Related context:\n> - (2017-9-22) https://lore.kernel.org/git/20170923033425.6ilykxl5pi7unlj6@sigill.intra.peff.net/\n>   proposed index lock timeout.\n> - (2017-9-27) Commit 27344d6a6c (git: add --no-optional-locks option, 2017-09-27)\n>   introduces `--no-optional-locks` for the same issue.\n> - (2019-5-3) https://lore.kernel.org/git/CACsJy8BcBk6Fsfy7o1aBTQAPeH-w+rBwLZrzw4jJ-OM_jhkxMQ@mail.gmail.com/\n>   mentioned `update_if_able` and \"gap\" between read and lock, mitigated\n>   in patch 1 with the `index->initialized` check.\n>\n> Jun Wu (2):\n>   repository: add core.indexLockTimeout\n>   commit: lock index before reading\n>\n>  Documentation/config/core.adoc |  6 ++++++\n>  builtin/commit.c               |  9 ++-------\n>  repository.c                   | 17 +++++++++++++++-\n>  t/meson.build                  |  1 +\n>  t/t0031-lockfile-pid.sh        |  4 ++++\n>  t/t0032-index-lock-timeout.sh  | 37 ++++++++++++++++++++++++++++++++++\n>  t/t7400-submodule-basic.sh     |  1 +\n>  7 files changed, 67 insertions(+), 8 deletions(-)\n>  create mode 100755 t/t0032-index-lock-timeout.sh\n>\n>\n> base-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\n\n"},{"id":"554923","messageId":"xmqqbj8xuugv.fsf@gitster.g","threadId":"66520","inReplyTo":"20261013172315.2997446-2-quark.zju@gmail.com","subject":"Re: [PATCH 1/2] repository: add core.indexLockTimeout","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-13T22:05:52Z","receivedAt":"2026-10-13T22:05:52Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jun Wu <quark.zju@gmail.com> writes:\n\n> From: Jun Wu <quark@meta.com>\n>\n> When an IDE runs `git status` periodically in the background, `status`\n> can take the index lock, and this prevents other commands like `add`,\n> `commit` from running.\n>\n> Commit 27344d6a6c (git: add --no-optional-locks option, 2017-09-27)\n> avoids the conflict by letting background `git --no-optional-locks status`\n> skip the index lock, at the cost of losing refreshed stat info, so\n> the next `status` has to re-hash files.\n>\n> Approach the conflict from the other side: let non-status commands wait\n> for the index lock. Mercurial and Sapling have a similar \"waiting for wlock\"\n> behavior, and Git already has `core.{config,filesRef}LockTimeout`.\n>\n> Ideally all non-status commands can wait for the index lock. However,\n> some code paths (e.g. `commit`) load the index without locking, then lock and\n> write changes. It is already racy and the wait behavior would make it much\n> worse. As a defense, waiting is disabled if index was already read.\n>\n> Default timeout is 1s, effective but not too long to avoid breaking potential\n> user expectation (e.g. \"an index.lock error means I should check whether a\n> commit message editor is open somewhere\"). Tests that are slowed down by the\n> default timeout now set the timeout to 0.\n\nNeed to change the existing tests is a sign that end-user workflows\nthat are emulated by them will be impacted, and we'd need to be\ncareful.\n\nI am not sure if the change is making the right trade-off between\nbackground tasks and end-user action that expects immediate\nresponse.\n\n> @@ -473,7 +473,22 @@ int repo_hold_locked_index(struct repository *repo,\n>  \t\t\t   struct lock_file *lf,\n>  \t\t\t   int flags)\n>  {\n> +\tint timeout_ms;\n> +\n>  \tif (!repo->index_file)\n>  \t\tBUG(\"the repo hasn't been setup\");\n> -\treturn repo_hold_lock_file_for_update(repo, lf, repo->index_file, flags);\n> +\n> +\tif (repo->index->initialized) {\n> +\t\t/*\n> +\t\t * Index was initialized (read) without lock. To minimize the\n> +\t\t * chance of lost update, do not wait.\n> +\t\t */\n> +\t\ttimeout_ms = 0;\n> +\t} else {\n> +\t\ttimeout_ms = 1000;\n> +\t\trepo_config_get_int(repo, \"core.indexlocktimeout\", &timeout_ms);\n> +\t}\n> +\n> +\treturn repo_hold_lock_file_for_update_timeout(repo, lf, repo->index_file,\n> +\t\t\t\t\t\t      flags, timeout_ms);\n>  }\n\nA few comments and/or questions..\n\n (1) In the unhappy case where the index was read already and then\n     we try to take the lock, what should happen when we notice that\n     the index is locked and better yet if we notice that the index\n     has been updated before we write the in-core index we wanted to\n     write out?  The question is what \"should\" happen, not \"what\n     happens with the current code\".\n\n (2) Are there cases where we take lock only to make sure nobody\n     will update the index without any intention to write out the\n     contents of our in-core index?  IOW, we are holding the lock\n     only to have a consistent world view while performing a\n     read-only operation?  How would this change affect it?\n\n (3) When 'repo->index->initialized' is true, does it always mean we\n     have read the on-disk index already, or are there cases where\n     we created the in-core index from scratch and called this\n     function with the intention to write over it?  For such a use\n     case, what, if any, are the implications of this change? \n\n"},{"id":"554929","messageId":"20261014023812.2935165-1-quark.zju@gmail.com","threadId":"66520","inReplyTo":"CALnO6CD8c+nZktineZjo6PkZbXXEQfKto-cvJvLW=_xjTPVCRA@mail.gmail.com","subject":"Re: [PATCH 1/2] repository: add core.indexLockTimeout","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-14T02:38:06Z","receivedAt":"2026-10-14T02:38:06Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"On Tue, 13 Oct 2026 17:40:01 -0400, D. Ben Knoble wrote:\n> Hi Jun,\n\nHi Ben,\n\nThanks for taking a look. This area is a bit tricky, so let me try to\nexplain more clearly.\n\n> Interesting. That must be the slow down you mentioned in your cover\n> letter? I'm trying to make sense of this. I think you're saying that\n> not having the index lock means status would \"lose\" refreshed info:\n> we're not throwing away the *existing* stat info, just the ones we\n> recomputed for this \"git --no-optional-locks status\" invocation,\n> right?\n\nYes. A file's stat info might be updated without content change (e.g.\n`touch`-ed) so the existing stat info can become outdated over time, and status\ncannot use the stat fast path to confirm a file is not changed, so status falls\nback to slow content check. Note: if the user runs a command like `add` that\nwrites the index, the performance can recover, but based on our experience,\nusers might not run such recovery commands for a long time, so they suffer from\nslow status.\n\n> I suppose I don't see how that causes things to slow down continually,\n> but I bet I'm missing something :)\n\nTo give some (hopefully useful) examples,\n\n1. https://github.com/facebook/sapling/issues/929#issuecomment-2628723586\n   A user says \"I use CLI exclusively so I would be really interested if\n   --no-optional-locks was applied only to automation\". The user was running\n   Sapling in a `.git` repo. Sapling (back then) passed `--no-optional-locks`\n   to every `git status` call. The user found `status` too slow to use, and\n   hope that non-automation commands would not use `--no-optional-locks`.\n   In other words, always using `--no-optional-locks` caused slowness.\n\n2. https://github.com/facebook/sapling/commit/f11d555a3a237254e1788f9e9da40b37cd6e452f\n   This is Sapling, but the concept (stat fast path) matches Git. Before this\n   change, similar to Git, Sapling's status takes the wlock (akin to\n   index.lock) in a best-effort way, and skip updating if it cannot get the\n   lock.\n   In production, we observed cases where `status` cannot get the lock for a\n   long time, and the user's `status` remains slow (IIRC tens of seconds to\n   minutes) without clear way to recover. This change forces a recovery path:\n   if there are too many (>200) files with outdated `stat` info, then wait for\n   the lock and update stats.\n\n> It feels weird to me to fix mostly-invisible behavior from external\n> tools with a config option, though. I think of config *mostly* as\n> user-facing (whether that user is a client of the Git CLI or\n> configuring a server repo), and I don't see how we could teach users\n> to look for this or know what a reasonable setting should be for\n> themselves :/ Further, we already have a lot of config options! (I'm\n> guilty of adding one recently, heh.) It can definitely be overwhelming\n> for users to think about all the options, and I suspect many just\n> don't as a result.\n\nI agree that users shouldn't need to know about extra options to get reasonable\nbehavior. In this case, I think the default (1s) is good, so users already\ndon't need to learn about it. The before/after matrix looks like:\n\n    | Background status   |     Before         |      After      |\n    |                     | slow   | cannot    | slow   | cannot |\n    |                     | status | commit    | status | commit |\n    --------------------------------------------------------------\n    | --no-optional-locks | yes    | no        | yes    | no     |\n    | (regular) status    | no     | sometimes | no     | rarely |\n\nThe bottom-right \"no, rarely\" + no need for `--no-optional-locks` seems to be\nthe ideal trade-off to me.\n\nI think the same preference (good out-of-box) applies to `--no-optional-locks`,\ntoo. I suspect that not a lot of tool authors know about `--no-optional-locks`.\nhttps://github.com/facebook/sapling/commit/39380e1871d7c5f6f91ebb67c4efb7588eb9f65f\ndocumented our version of \"tool author doesn't know\" - tool didn't set the\n\"is automation\" env var, akin to forgetting `--no-optional-locks`. The repo in\nquestion had a spiked ~40% `git checkout` failure rate.\n\n> I applaud the effort for sensible defaults here, which I think will help.\n>\n> Anyway, maybe it's just me: if others are happy with this approach,\n> great. I think I would prefer to see us try to address the underlying\n> performance problems, though, if we can. (Maybe we can have some way\n> for git-status to not throw away all its work, or some other good way\n> to communicate to IDEs that want to poll?)\n\nTo recap, the user-facing problem is \"commit/checkout\" didn't work when\n`status` was running, or `--no-optional-locks status` can be too slow.\n\nBy \"address the underlying performance problems\", do you mean:\n\n1. Improve `status` performance so it takes index lock for a shorter duration.\n   It helps, but as long as `status` takes the lock, there is a chance it\n   conflicts with other commands. This series allows tools to drop\n   `--no-optional-locks`, which indirectly helps `status` performance.\n2. Improve `--no-optional-locks status` performance so it's fast.\n   This sounds like \"lock-free writes to the index\", which might require\n   a complex file format change.\n3. Switch IDEs from polling `git status` to fsmonitor-style `status` notification.\n   This helps. But similar to 1, reducing `status` frequency alone does not\n   address the root issue.\n\nFor what it's worth, Mercurial and Sapling have waited for the wlock by\ndefault for a long time, with default timeout set to 10 minutes. In our\nexperience that has worked well. The most common \"waiting for lock\" report we\nsee is an editor left open somewhere, which got rarer after we started printing\nthe process tree of the lock holder.\n\nThanks, Jun\n\n"},{"id":"554991","messageId":"20261014100432.GB1344504@coredump.intra.peff.net","threadId":"66520","inReplyTo":"20261013172315.2997446-3-quark.zju@gmail.com","subject":"Re: [PATCH 2/2] commit: lock index before reading","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2026-10-14T10:04:32Z","receivedAt":"2026-10-14T10:04:32Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Oct 13, 2026 at 10:23:15AM -0700, Jun Wu wrote:\n\n> Take the index lock before reading the index, in one place, instead\n> of separately in each `if` branch.\n> \n> `commit` now respects the `core.indexLockTimeout` config.\n> \n> History suggests no particular reason for reading before locking:\n> - f5bbc3225c (Port git commit to C., 2007-11-08): lock before reading\n> - 2888605c64 (builtin-commit: fix partial-commit support, 2007-11-18):\n>   seems to accidentally move reading to before locking\n\nHmm. We may do a lot more work under lock than we used to, running\nstat() on the whole repo.\n\nBut I think there's a deeper issue here. We're reading the index, _then_\ntaking the lock, but not necessarily discarding what we read earlier. So\nit feels like two processes could race here, with git-commit discarding\nsome update by another process (like git-add).\n\nIf we instrument git-commit like this:\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 840b6b4083..2a78cb473a 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -392,6 +392,8 @@ static const char *prepare_index(const char **argv, const char *prefix,\n \tif (repo_read_index_preload(the_repository, &pathspec, 0) < 0)\n \t\tdie(_(\"index file corrupt\"));\n \n+\tsleep(5);\n+\n \tif (interactive) {\n \t\tchar *old_index_env = NULL, *old_repo_index_file;\n \t\trepo_hold_locked_index(the_repository, &index_lock,\n\nthat widens the race window. And then something like this:\n\n  echo new content >old-file\n  git commit -am foo &\n  sleep 1 ;# commit is sleeping in race window\n\n  git add new-file\n\nwill clobber the index change from git-add.\n\nAnd that is true with today's code, not using any timeouts. Adding a\ntimeout doesn't really change anything, though if you _do_ see lock\ncontention it is a good sign that you are about to hit the race and\nclobber something. ;) So it is widening the window. But the correctness\nproblem is already there.\n\nI do think your patch fixes the problem. But we probably want to discuss\nthat in the commit message as a motivation (and likewise, discuss the\nfact that we may be holding the lock for longer).\n\nThere's probably some kind of hybrid world where we optimistically read\nwithout the lock, then take the lock, and then check (cheaply-ish) that\nthe index didn't change since what we read. But I don't think we do that\nin this code path.\n\n-Peff\n\n"},{"id":"554992","messageId":"20261014101402.GC1344504@coredump.intra.peff.net","threadId":"66520","inReplyTo":"20261013172315.2997446-1-quark.zju@gmail.com","subject":"Re: [PATCH 0/2] Add a way to wait for index lock","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2026-10-14T10:14:02Z","receivedAt":"2026-10-14T10:14:02Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Oct 13, 2026 at 10:23:13AM -0700, Jun Wu wrote:\n\n> From: Jun Wu <quark@meta.com>\n> \n> An IDE that runs `git status` periodically in the background has 2\n> choices. Both are suboptimal:\n> - with `--no-optional-locks`: `status` becomes slower over time.\n\nThe idea of --no-optional-locks is that the user eventually does actually\nrun some command in the CLI that updates the index (or even via the IDE\nif it's not git-status). But yeah, that isn't necessarily guaranteed.\n\nIt's possible that an IDE caller could be smarter about _sometimes_\nrunning a normal git-status, if it's been a while. But then of course it\nhas to worry about what \"a while\" means. ;)\n\nSo I think looking at lock timeouts and retries on the other side is a\nreasonable approach.\n\n> To minimize write race, if the index is read before locking, the\n> wait will be skipped, as if timeout is 0. Depending on the read/lock\n> order, some commands (e.g. add, rm, mv, checkout, restore, read-tree,\n> apply, update-index) respect the timeout. Some (e.g. reset, stash,\n> merge, rebase, cherry-pick, am) do not. I started with the simple and\n> common commands. If there is interest in other commands I can take a\n> look too.\n\nI think any problem exacerbated by the timeout was already a possible\nrace even without it. But it would be increasing the race window (which\nis fine for racing benign commands like \"git status\", but probably bad\nfor racing \"git add\"; see my response to patch 2).\n\nI think the git-commit race, at least, has probably been there for 20+\nyears. It's just that nobody has really seen it in practice because\nmaking commits tends to be a serial-ish process (as opposed to things\nlike receiving pushes, where big sites hit every kind of race). But it\ndoes seem reasonable to close those races, at which point the timeout\nbehavior seems like a no-brainer.\n\n-Peff\n\n"},{"id":"555047","messageId":"CALnO6CA=rnFfgvokMUQ5M-2xO+aqowF5KB1SSTYXT8hNYKUq+w@mail.gmail.com","threadId":"66520","inReplyTo":"20261014023812.2935165-1-quark.zju@gmail.com","subject":"Re: [PATCH 1/2] repository: add core.indexLockTimeout","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-14T16:29:25Z","receivedAt":"2026-10-14T16:29:25Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Tue, Oct 13, 2026 at 10:38 PM Jun Wu <quark.zju@gmail.com> wrote:\n>\n> On Tue, 13 Oct 2026 17:40:01 -0400, D. Ben Knoble wrote:\n> > Hi Jun,\n>\n> Hi Ben,\n>\n> Thanks for taking a look. This area is a bit tricky, so let me try to\n> explain more clearly.\n>\n> > Interesting. That must be the slow down you mentioned in your cover\n> > letter? I'm trying to make sense of this. I think you're saying that\n> > not having the index lock means status would \"lose\" refreshed info:\n> > we're not throwing away the *existing* stat info, just the ones we\n> > recomputed for this \"git --no-optional-locks status\" invocation,\n> > right?\n>\n> Yes. A file's stat info might be updated without content change (e.g.\n> `touch`-ed) so the existing stat info can become outdated over time, and status\n> cannot use the stat fast path to confirm a file is not changed, so status falls\n> back to slow content check. Note: if the user runs a command like `add` that\n> writes the index, the performance can recover, but based on our experience,\n> users might not run such recovery commands for a long time, so they suffer from\n> slow status.\n\nMakes sense.\n\n> > I suppose I don't see how that causes things to slow down continually,\n> > but I bet I'm missing something :)\n>\n> To give some (hopefully useful) examples,\n\nSorry, I wonder if I wasn't clear: I read the cover letter as saying\nthat performance worsens over time, and I'm not sure how that's\nimplied. Maybe the idea is that more and more files need to be\ncontent-hashed, though?\n\n> > I applaud the effort for sensible defaults here, which I think will help.\n> >\n> > Anyway, maybe it's just me: if others are happy with this approach,\n> > great. I think I would prefer to see us try to address the underlying\n> > performance problems, though, if we can. (Maybe we can have some way\n> > for git-status to not throw away all its work, or some other good way\n> > to communicate to IDEs that want to poll?)\n\nIt does sound like others (Peff) think this is a fine approach, so\nI'll bow out now :)\n\n> By \"address the underlying performance problems\", do you mean:\n>\n> 1. Improve `status` performance so it takes index lock for a shorter duration.\n>    It helps, but as long as `status` takes the lock, there is a chance it\n>    conflicts with other commands. This series allows tools to drop\n>    `--no-optional-locks`, which indirectly helps `status` performance.\n> 2. Improve `--no-optional-locks status` performance so it's fast.\n>    This sounds like \"lock-free writes to the index\", which might require\n>    a complex file format change.\n> 3. Switch IDEs from polling `git status` to fsmonitor-style `status` notification.\n>    This helps. But similar to 1, reducing `status` frequency alone does not\n>    address the root issue.\n\nThese all sound like great ideas! I actually didn't have a concrete\nsuggestion, other than trying to address the root cause rather than\n(IMO) band-aiding the surface.\n\nLooking a bit more at how my editor integration works (tpope's\nfugitive for Vim), it seems to *not* use --no-optional-locks and\n(presumably) avoids the issues you're seeing by limiting background\nrefreshes to specific events [1] and doing some kind of caching, but I\nhaven't really worked through it all and I'm not sure how easy it\nwould be to extract generic ideas for different editors :) It's also\npossible I just haven't ever run into the situation where\n--no-optional-locks would have helped, so might be a moot point.\n\n[1]: https://github.com/tpope/vim-fugitive/tree/3b753cf8c6a4dcde6edee8827d464ba9b8c4a6f0\n\n-- \nD. Ben Knoble\n\n"},{"id":"555082","messageId":"20261014221226.1693842-1-quark.zju@gmail.com","threadId":"66520","inReplyTo":"xmqqh5ipuuhk.fsf@gitster.g","subject":"Re: [PATCH 0/2] Add a way to wait for index lock","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-14T22:12:24Z","receivedAt":"2026-10-14T22:12:24Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"On Tue, 13 Oct 2026 15:05:27 -0700, Junio C Hamano wrote:\n> It is unfair to expect that the readers would guess what you left\n> unsaid here, which probably is \"we will introduce the third choice,\n> which does such and such things, and it improves the situation in\n> such and such ways because ...\", before listing ...\n\nGot it. I was trying to reduce duplication with commit message.\nWill adjust going forward.\n\n> > To minimize write race, if the index is read before locking, the\n> > wait will be skipped, as if timeout is 0. Depending on the read/lock\n> > order, some commands (e.g. add, rm, mv, checkout, restore, read-tree,\n> > apply, update-index) respect the timeout. Some (e.g. reset, stash,\n> > merge, rebase, cherry-pick, am) do not. I started with the simple and\n> > common commands. If there is interest in other commands I can take a\n> > look too.\n>\n> Is the above supposed to be the description of the proposed solution?\n\nI thought it was the exciting key detail that is worth highlighting.\nBut it seems too tricky to grasp. Will adjust going forward.\n\n> There are multiple parties (i.e., `git status` that needed to\n> discard the result with \"--no-optional-locks\" and other end-user\n> actions that do need to safely update the index) involved, and it is\n> unclear whose actions many of verbs in your writing refer to in the\n> above.  E.g., \"if the index is read before locking, the wait will be\n> skipped\" for whom?  \"git status\" that wants to opportunistically\n\nScope \"if the index ...\" to a single process only:\n- \"who\": the current process that calls repo_hold_locked_index (could be any\n  command that calls this function)\n- \"if the index is read before locking, ...\": within the current process,\n  if the index is read (repo->index->initialized) before locking\n  (repo_hold_locked_index), then use the old behavior (timeout=0).\n- whether there is a separate `git status` process or not, whether that process\n  has read/locked the index or not, are irrelevant to the above rule scoped to\n  the current process.\n\n> write out the result of refreshing?  End-user actions like \"git add\"\n> that wanted to register new contents to the index?  Both?\n\nBoth are subject to the above rule. Today, `git status` always reads the\nindex before locking, so timeout=0, and its behavior is not changed.\n\n"},{"id":"555083","messageId":"20261014221233.1694344-1-quark.zju@gmail.com","threadId":"66520","inReplyTo":"xmqqbj8xuugv.fsf@gitster.g","subject":"Re: [PATCH 1/2] repository: add core.indexLockTimeout","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-14T22:12:32Z","receivedAt":"2026-10-14T22:12:32Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"On Tue, 13 Oct 2026 15:05:52 -0700, Junio C Hamano wrote:\n> > Default timeout is 1s, effective but not too long to avoid breaking potential\n> > user expectation (e.g. \"an index.lock error means I should check whether a\n> > commit message editor is open somewhere\"). Tests that are slowed down by the\n> > default timeout now set the timeout to 0.\n>\n> Need to change the existing tests is a sign that end-user workflows\n> that are emulated by them will be impacted, and we'd need to be\n> careful.\n\nYes. I'm aware of end-user behavior change. The test changes aren't strictly\nnecessary - without the changes tests still pass, although run a bit slower.\n\n> I am not sure if the change is making the right trade-off between\n> background tasks and end-user action that expects immediate\n> response.\n\nIt seems the trade-off is between command success rate and command duration.\n\nI suspect that end-users want commands to work (not fail) first, performance\ncomes next. In my experience, high command failure rate escalates to oncall\nincidents, occasional slightly slower commands often get unnoticed unless we\nlook at the metrics explicitly.\n\nMercurial has been using 10-minute timeout by default. I picked 1 second as a\nmiddle ground, limit the \"slowness\". I'm open to setting it to 10 minutes\n(matches hg), or 0 (preserves the old behavior), or another value.\n\n> A few comments and/or questions..\n>\n>  (1) In the unhappy case where the index was read already and then\n>      we try to take the lock, what should happen when we notice that\n>      the index is locked and better yet if we notice that the index\n>      has been updated before we write the in-core index we wanted to\n>      write out?  The question is what \"should\" happen, not \"what\n>      happens with the current code\".\n\nI think the lock infrastructure might want to force dropping outdated in-memory\nindex once lock gets acquired. The users of the lock APIs need to handle this\nnew cache invalidation situation. Locks in hg work this way.\n\nHow each command treats the \"cache gets dropped by locking\" can vary by case.\nFor example,\n- status: read -> refresh -> lock(timeout=0) -> invalidated ? skip : write\n- add -i: read -> lock(timeout>0) -> invalidated ? re-apply \"add\" : write\n\n>  (2) Are there cases where we take lock only to make sure nobody\n>      will update the index without any intention to write out the\n>      contents of our in-core index?  IOW, we are holding the lock\n>      only to have a consistent world view while performing a\n>      read-only operation?  How would this change affect it?\n\nThere are commands (e.g. rm --dry-run) that lock without writing, but these\nmight be for convenience, not consistency. This change will make them wait.\n\nSince a single read of the index is already consistent (it is replaced\natomically), I assume by \"view\" you also mean the working tree files.\nThen, there is a question about the intended scope of the index lock:\n  a. only covers the index\n  b. covers both the index and working copy files\n\nThe answer probably does not matter that much, since commands that write\nworking copy files likely also want to update stat, therefore take the index\nlock. The only exception seems to be \"clean\", since index has no stat for\nuntracked files. Today, \"clean\" does not lock. So this change doesn't affect\nit.\n\n>  (3) When 'repo->index->initialized' is true, does it always mean we\n>      have read the on-disk index already, or are there cases where\n>      we created the in-core index from scratch and called this\n>      function with the intention to write over it?  For such a use\n>      case, what, if any, are the implications of this change?\n\n## Does initialized=1 mean reading from disk already?\n\nNot always.\n\ninitialized=1 is set by either 'do_read_index' or 'unpack_trees'.\n'do_read_index' is the regular path. 'unpack_trees' sets result->initialized=1\nso it depends on the input of 'unpack_trees'.  Some callers (e.g. read-tree)\ncall unpack_trees when repo->index is empty, not initialized. So in this case,\ninitialized=1 does not mean \"read from disk\".\n\n## Cases we created index from scratch, then call repo_hold_locked_index\n\nI didn't find any.\n\nThere are commands that create index from scratch, intend to write: clone and\nread-tree. But they all take the index lock (when initialized=0) before calling\n'unpack_trees' (sets initialized=1).\n\n"},{"id":"555087","messageId":"20261015001938.2092523-1-quark.zju@gmail.com","threadId":"66520","inReplyTo":"20261014100432.GB1344504@coredump.intra.peff.net","subject":"Re: [PATCH 2/2] commit: lock index before reading","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-15T00:19:37Z","receivedAt":"2026-10-15T00:19:37Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"Hi Peff,\n\nOn Wed, 14 Oct 2026 06:04:32 -0400, Jeff King wrote:\n> I do think your patch fixes the problem. But we probably want to discuss\n> that in the commit message as a motivation (and likewise, discuss the\n> fact that we may be holding the lock for longer).\n\nWill do in v2: describe the race as motivation, and mention the longer hold\ntrade-off.\n\nI'd like to note 'commit' is just one command with the read/lock order issue.\nThere are many others, like: reset, rebase, stash, am, merge, etc.\nSome are easy to fix, some seem tricky (e.g. merge).\n\nDo you think that I should fix all order issues before adding the timeout?\n\nNote Patch 1 alone does not hurt - if a command has the read/lock order issue\nand is therefore racy, then the timeout feature is automatically disabled\n(by the 'initialized' check) so the racy command behaves exactly the same as\nbefore, not worse.\n\nMy plan is to add the timeout support first, then incrementally fix order\nissues, starting from the easy/more common ones (e.g. commit).\n\n> There's probably some kind of hybrid world where we optimistically read\n> without the lock, then take the lock, and then check (cheaply-ish) that\n> the index didn't change since what we read. But I don't think we do that\n> in this code path.\n\nFor reference, 'status' is such an example, although its write is optional.\n'add -i' could be another example - it avoids locking when waiting for user\ninput, although it lacks the \"didn't change\" check right now.\n\n-Jun\n\n"},{"id":"555299","messageId":"20261017073231.3357477-1-quark.zju@gmail.com","threadId":"66520","inReplyTo":"20261013172315.2997446-1-quark.zju@gmail.com","subject":"[PATCH v2 0/4] Wait for the index lock instead of failing","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-17T07:32:27Z","receivedAt":"2026-10-17T07:32:27Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"From: Jun Wu <quark@meta.com>\n\nWhen another process holds the index lock, commands like \"git add\",\n\"git checkout\" and \"git commit\" fail immediately, causing user\nfrustration. This series lets commands that must write the index to wait\nbriefly for the index lock (core.indexLockTimeout, default 1s), to be\nmore user friendly.\n\n\"another process that holds the index lock\" is often \"git status\" run\nperiodically by an IDE, or a tool. Today, such tools have 2 choices:\n- \"git status\": risks making \"checkout\", \"commit\" fail more often.\n- \"git --no-optional-locks status\": because \"status\" no longer writes\n  the refreshed stat info, \"git --no-optional-locks status\" will become\n  slower and slower over time, assuming no other commands refresh the\n  index.\n\nThis series aims for the end state that:\n- Tool authors can use background \"git status\" just fine, and \"status\"\n  won't become slower. They can still use \"--no-optional-locks\", with\n  no behavior changes.\n- End users can run \"git commit/checkout\" just fine, without seeing\n  command failures due to the index lock.\n\nConservatively, the new wait behavior disables itself for commands with\nwrite race (typically \"read (index) -> lock -> (no reload) -> write\"),\nso those racy commands stay unchanged, instead of getting a larger race\nwindow (between \"read\" and \"lock\"). This series fixes the write races\nfor \"commit\" and \"reset\" so the two commands, together with other\ncommands that already properly lock before reading, all get the new\nwait behavior. The trade-off is that \"commit\" now lstat() the working\ntree under the index lock.\n\nChanges since v1:\n- Explain details more clearly.\n- New patch 1: make \"git status\"'s non-waiting behavior explicit.\n- New patch 4: fix \"reset\" write race, similar to \"commit\".\n- Patch 3 and 4: use \"write race\" as motivation.\n\nJun Wu (4):\n  repository: add repo_try_hold_locked_index()\n  repository: add core.indexLockTimeout\n  commit: fix write race\n  reset: fix write race\n\n Documentation/config/core.adoc |  6 +++++\n builtin/commit.c               | 11 +++------\n builtin/describe.c             |  4 ++--\n builtin/diff.c                 |  2 +-\n builtin/merge.c                |  2 +-\n builtin/rebase.c               |  2 +-\n builtin/reset.c                |  7 +++---\n repository.c                   | 33 ++++++++++++++++++++++++++\n repository.h                   | 11 +++++++++\n sequencer.c                    |  2 +-\n t/meson.build                  |  1 +\n t/t0031-lockfile-pid.sh        |  4 ++++\n t/t0032-index-lock-timeout.sh  | 42 ++++++++++++++++++++++++++++++++++\n t/t7400-submodule-basic.sh     |  1 +\n wt-status.c                    |  2 +-\n 15 files changed, 112 insertions(+), 18 deletions(-)\n create mode 100755 t/t0032-index-lock-timeout.sh\n\n\nbase-commit: c51408889c3b372814bc20b9431c3b74d0d268eb\n-- \n2.56.0\n\n\n"},{"id":"555300","messageId":"20261017073231.3357477-2-quark.zju@gmail.com","threadId":"66520","inReplyTo":"20261017073231.3357477-1-quark.zju@gmail.com","subject":"[PATCH v2 1/4] repository: add repo_try_hold_locked_index()","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-17T07:32:28Z","receivedAt":"2026-10-17T07:32:28Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"From: Jun Wu <quark@meta.com>\n\nRefactoring to make the next commit easier to reason about.\n\nrepo_hold_locked_index() has 2 categories of users:\n1. Lock is required, or fail - they might want to wait for the lock,\n   with the intention to not fail.\n2. Lock is optional, won't fail if lock cannot be obtained - they\n   don't need to wait for the lock.\n\nMigrate the 2nd category, identified by repo_update_index_if_able(),\nto a separate repo_try_hold_locked_index() to clarify intention.\nFor now, the two functions are the same. The next commit changes\nrepo_hold_locked_index().\n\nAlternative approach considered: a LOCK_NO_RETRY flag could express\nthis, but hold_lock_file_for_update_timeout() already takes\ntimeout_ms, so the flag would duplicate or conflict with it.\n\nHelped-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Jun Wu <quark@meta.com>\n---\n builtin/commit.c   | 2 +-\n builtin/describe.c | 4 ++--\n builtin/diff.c     | 2 +-\n builtin/merge.c    | 2 +-\n builtin/rebase.c   | 2 +-\n repository.c       | 9 +++++++++\n repository.h       | 7 +++++++\n sequencer.c        | 2 +-\n wt-status.c        | 2 +-\n 9 files changed, 24 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 840b6b4083..a808c0ba39 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1632,7 +1632,7 @@ struct repository *repo UNUSED)\n \t\t      &s.pathspec, NULL, NULL);\n \n \tif (use_optional_locks())\n-\t\tfd = repo_hold_locked_index(the_repository, &index_lock, 0);\n+\t\tfd = repo_try_hold_locked_index(the_repository, &index_lock, 0);\n \telse\n \t\tfd = -1;\n \ndiff --git a/builtin/describe.c b/builtin/describe.c\nindex c0abc931a5..f28030e970 100644\n--- a/builtin/describe.c\n+++ b/builtin/describe.c\n@@ -764,8 +764,8 @@ int cmd_describe(int argc,\n \t\t\trepo_read_index(the_repository);\n \t\t\trefresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED,\n \t\t\t\t      NULL, NULL, NULL);\n-\t\t\tfd = repo_hold_locked_index(the_repository,\n-\t\t\t\t\t\t    &index_lock, 0);\n+\t\t\tfd = repo_try_hold_locked_index(the_repository,\n+\t\t\t\t\t\t\t&index_lock, 0);\n \t\t\tif (0 <= fd)\n \t\t\t\trepo_update_index_if_able(the_repository, &index_lock);\n \ndiff --git a/builtin/diff.c b/builtin/diff.c\nindex 18b1083e98..c6c6de629a 100644\n--- a/builtin/diff.c\n+++ b/builtin/diff.c\n@@ -239,7 +239,7 @@ static void refresh_index_quietly(void)\n \tstruct lock_file lock_file = LOCK_INIT;\n \tint fd;\n \n-\tfd = repo_hold_locked_index(the_repository, &lock_file, 0);\n+\tfd = repo_try_hold_locked_index(the_repository, &lock_file, 0);\n \tif (fd < 0)\n \t\treturn;\n \tdiscard_index(the_repository->index);\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex 5b4eb23a83..1e9ede5bb5 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -347,7 +347,7 @@ static int save_state(struct object_id *stash)\n \tint fd;\n \tint rc = -1;\n \n-\tfd = repo_hold_locked_index(the_repository, &lock_file, 0);\n+\tfd = repo_try_hold_locked_index(the_repository, &lock_file, 0);\n \trefresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);\n \tif (0 <= fd)\n \t\trepo_update_index_if_able(the_repository, &lock_file);\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 535db60181..6df9e5b058 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -1371,7 +1371,7 @@ int cmd_rebase(int argc,\n \t\tif (repo_get_oid(the_repository, \"HEAD\", &head))\n \t\t\tdie(_(\"Cannot read HEAD\"));\n \n-\t\tfd = repo_hold_locked_index(the_repository, &lock_file, 0);\n+\t\tfd = repo_try_hold_locked_index(the_repository, &lock_file, 0);\n \t\tif (repo_read_index(the_repository) < 0)\n \t\t\tdie(_(\"could not read index\"));\n \t\trefresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,\ndiff --git a/repository.c b/repository.c\nindex b857e1c580..a74b1118cb 100644\n--- a/repository.c\n+++ b/repository.c\n@@ -477,3 +477,12 @@ int repo_hold_locked_index(struct repository *repo,\n \t\tBUG(\"the repo hasn't been setup\");\n \treturn repo_hold_lock_file_for_update(repo, lf, repo->index_file, flags);\n }\n+\n+int repo_try_hold_locked_index(struct repository *repo,\n+\t\t\t       struct lock_file *lf,\n+\t\t\t       int flags)\n+{\n+\tif (!repo->index_file)\n+\t\tBUG(\"the repo hasn't been setup\");\n+\treturn repo_hold_lock_file_for_update(repo, lf, repo->index_file, flags);\n+}\ndiff --git a/repository.h b/repository.h\nindex 11f5c2ed10..253a5f1d3f 100644\n--- a/repository.h\n+++ b/repository.h\n@@ -288,6 +288,13 @@ int repo_hold_locked_index(struct repository *repo,\n \t\t\t   struct lock_file *lf,\n \t\t\t   int flags);\n \n+/*\n+ * Try to obtain the index lock in a non-blocking way.\n+ */\n+int repo_try_hold_locked_index(struct repository *repo,\n+\t\t\t       struct lock_file *lf,\n+\t\t\t       int flags);\n+\n int repo_read_index_unmerged(struct repository *);\n /*\n  * Opportunistically update the index but do not complain if we can't.\ndiff --git a/sequencer.c b/sequencer.c\nindex efbfd1315b..889807ba45 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4772,7 +4772,7 @@ static void create_autostash_internal(struct repository *r,\n \tif (path && refname)\n \t\tBUG(\"can only pass path or refname\");\n \n-\tfd = repo_hold_locked_index(r, &lock_file, 0);\n+\tfd = repo_try_hold_locked_index(r, &lock_file, 0);\n \trefresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);\n \tif (0 <= fd)\n \t\trepo_update_index_if_able(r, &lock_file);\ndiff --git a/wt-status.c b/wt-status.c\nindex 57772c7501..0b19161de8 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -2791,7 +2791,7 @@ int require_clean_work_tree(struct repository *r,\n \tstruct lock_file lock_file = LOCK_INIT;\n \tint err = 0, fd;\n \n-\tfd = repo_hold_locked_index(r, &lock_file, 0);\n+\tfd = repo_try_hold_locked_index(r, &lock_file, 0);\n \trefresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);\n \tif (0 <= fd)\n \t\trepo_update_index_if_able(r, &lock_file);\n-- \n2.56.0\n\n\n"},{"id":"555301","messageId":"20261017073231.3357477-3-quark.zju@gmail.com","threadId":"66520","inReplyTo":"20261017073231.3357477-1-quark.zju@gmail.com","subject":"[PATCH v2 2/4] repository: add core.indexLockTimeout","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-17T07:32:29Z","receivedAt":"2026-10-17T07:32:29Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"From: Jun Wu <quark@meta.com>\n\nMake \"git add/rm/checkout\" wait for the index lock, so they won't fail\nimmediately.\n\nWhen an IDE runs \"git status\" every 5 seconds, and each \"git status\" holds the\nindex lock for 0.5s, a user has 10% chance to be unable to commit or checkout.\nBy waiting, the add and checkout commands can succeed.\n\nExample commands that will wait:\n- add, rm, mv, checkout / switch / restore, read-tree, apply --index /\n  --cached, update-index, cherry-pick, revert, clone.\n\nExample commands that are unchanged:\n- repo_try_hold_locked_index() users (see previous commit):\n  status, describe --dirty, part of diff / merge / rebase.\n- repo_hold_locked_index() users, but racy (waiting makes race worse,\n  see code comment for details): stash, part of merge, am,\n  add -i / -p, checkout-index, sparse-checkout, rerere,\n  commit (until the next commit), reset (until a later commit).\n\nDefault timeout is 1s. Mercurial uses 600s. In case of an unexpected\nuse-case with unexpected expectation, I picked a conservative middle\nground.\n\nSome tests emulate the index lock being taken, the new behavior makes\nthose tests slower (but still passing). Those tests now set timeout to 0\nto run as fast as before.\n\nAlternatively, IDEs could use \"git --no-optional-locks status\" to avoid taking\nthe index lock. However, not all tool authors know about \"--no-optional-locks\",\nand \"--no-optional-locks\" will lose the refreshed stat info, so later\n(periodical) \"git --no-optional-locks status\" runs become slower over time\n(assuming nothing refreshes the index).\n\nSigned-off-by: Jun Wu <quark@meta.com>\n---\n Documentation/config/core.adoc |  6 +++++\n repository.c                   | 26 +++++++++++++++++++-\n repository.h                   |  4 ++++\n t/meson.build                  |  1 +\n t/t0031-lockfile-pid.sh        |  4 ++++\n t/t0032-index-lock-timeout.sh  | 43 ++++++++++++++++++++++++++++++++++\n t/t7400-submodule-basic.sh     |  1 +\n 7 files changed, 84 insertions(+), 1 deletion(-)\n create mode 100755 t/t0032-index-lock-timeout.sh\n\ndiff --git a/Documentation/config/core.adoc b/Documentation/config/core.adoc\nindex bdc74d291c..88f3529cc0 100644\n--- a/Documentation/config/core.adoc\n+++ b/Documentation/config/core.adoc\n@@ -604,6 +604,12 @@ core.configLockTimeout::\n \tthat is already on disk before the lock is taken, so it can be\n \tset persistently like any other option.\n \n+core.indexLockTimeout::\n+\tThe length of time, in milliseconds, to retry when trying to\n+\tlock the index file for writing. Value 0 means not to retry at\n+\tall; -1 means to try indefinitely. Default is 1000 (i.e., retry\n+\tfor 1 second). Not supported by all commands.\n+\n core.pager::\n \tText viewer for use by Git commands (e.g., 'less').  The value\n \tis meant to be interpreted by the shell.  The order of preference\ndiff --git a/repository.c b/repository.c\nindex a74b1118cb..c7d098d166 100644\n--- a/repository.c\n+++ b/repository.c\n@@ -473,9 +473,33 @@ int repo_hold_locked_index(struct repository *repo,\n \t\t\t   struct lock_file *lf,\n \t\t\t   int flags)\n {\n+\tint timeout_ms;\n+\n \tif (!repo->index_file)\n \t\tBUG(\"the repo hasn't been setup\");\n-\treturn repo_hold_lock_file_for_update(repo, lf, repo->index_file, flags);\n+\n+\tif (repo->index->initialized) {\n+\t\t/*\n+\t\t * Index was read before lock. Depending on the callsite\n+\t\t * logic, the write might be racy:\n+\t\t *\n+\t\t * - read: repo_read_index()\n+\t\t * - lock: repo_hold_locked_index() -> maybe sleep()\n+\t\t * - write: repo_update_index_if_able() # okay\n+\t\t *          write_locked_index() # racy, worse with sleep()\n+\t\t *\n+\t\t * Be conservative, avoid sleep() to not make the write race\n+\t\t * worse. If the callsite wants the \"waiting for index lock\"\n+\t\t * behavior, fix the read/lock order.\n+\t\t */\n+\t\ttimeout_ms = 0;\n+\t} else {\n+\t\ttimeout_ms = 1000;\n+\t\trepo_config_get_int(repo, \"core.indexlocktimeout\", &timeout_ms);\n+\t}\n+\n+\treturn repo_hold_lock_file_for_update_timeout(repo, lf, repo->index_file,\n+\t\t\t\t\t\t      flags, timeout_ms);\n }\n \n int repo_try_hold_locked_index(struct repository *repo,\ndiff --git a/repository.h b/repository.h\nindex 253a5f1d3f..fe4eedd7d3 100644\n--- a/repository.h\n+++ b/repository.h\n@@ -284,6 +284,10 @@ void repo_clear(struct repository *repo);\n  * populated then the number of entries will simply be returned.\n  */\n int repo_read_index(struct repository *repo);\n+\n+/*\n+ * Obtain the index lock. This can block (retry with backoff).\n+ */\n int repo_hold_locked_index(struct repository *repo,\n \t\t\t   struct lock_file *lf,\n \t\t\t   int flags);\ndiff --git a/t/meson.build b/t/meson.build\nindex 11b3752f00..3aff2a9f50 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -108,6 +108,7 @@ integration_tests = [\n   't0029-core-unsetenvvars.sh',\n   't0030-stripspace.sh',\n   't0031-lockfile-pid.sh',\n+  't0032-index-lock-timeout.sh',\n   't0033-safe-directory.sh',\n   't0034-root-safe-directory.sh',\n   't0035-safe-bare-repository.sh',\ndiff --git a/t/t0031-lockfile-pid.sh b/t/t0031-lockfile-pid.sh\nindex e9e2f04049..3fc8de02b6 100755\n--- a/t/t0031-lockfile-pid.sh\n+++ b/t/t0031-lockfile-pid.sh\n@@ -8,6 +8,10 @@ The feature is opt-in via core.lockfilePid config setting (boolean).\n \n . ./test-lib.sh\n \n+test_expect_success 'setup' '\n+\tgit config --global core.indexLockTimeout 0\n+'\n+\n test_expect_success 'stale lock detected when PID is not running' '\n \tgit init repo &&\n \t(\ndiff --git a/t/t0032-index-lock-timeout.sh b/t/t0032-index-lock-timeout.sh\nnew file mode 100755\nindex 0000000000..cde6f1892c\n--- /dev/null\n+++ b/t/t0032-index-lock-timeout.sh\n@@ -0,0 +1,43 @@\n+#!/bin/sh\n+\n+test_description='Test config core.indexLockTimeout'\n+\n+. ./test-lib.sh\n+\n+prepare_index_lock () {\n+\ttest_when_finished \"rm -f .git/index.lock\" &&\n+\ttouch .git/index.lock &&\n+\t{\n+\t\t( sleep 1 && rm -f .git/index.lock ) &\n+\t} &&\n+\ttest_when_finished \"wait\"\n+}\n+\n+test_must_fail_with_index_lock () {\n+\ttest_must_fail \"$@\" 2>err &&\n+\ttest_grep index.lock err\n+}\n+\n+test_expect_success 'index locked, add fails with indexLockTimeout=0' '\n+\tprepare_index_lock &&\n+\ttouch foo &&\n+\ttest_must_fail_with_index_lock git -c core.indexLockTimeout=0 add foo\n+'\n+\n+test_expect_success 'index locked, add waits with indexLockTimeout>0' '\n+\tprepare_index_lock &&\n+\tgit -c core.indexLockTimeout=5000 add foo\n+'\n+\n+test_expect_success 'index locked, commit fails even with indexLockTimeout>0 (read index before lock)' '\n+\tprepare_index_lock &&\n+\ttest_must_fail_with_index_lock git -c core.indexLockTimeout=5000 commit -m init\n+'\n+\n+test_expect_success 'index locked, checkout waits with indexLockTimeout>0' '\n+\tgit commit -m init &&\n+\tprepare_index_lock &&\n+\tgit -c core.indexLockTimeout=5000 checkout HEAD\n+'\n+\n+test_done\ndiff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh\nindex eefdecb0bd..9a90b6a76d 100755\n--- a/t/t7400-submodule-basic.sh\n+++ b/t/t7400-submodule-basic.sh\n@@ -276,6 +276,7 @@ test_expect_success 'submodule add to reconfigure existing submodule with --forc\n '\n \n test_expect_success 'submodule add relays add --dry-run stderr' '\n+\ttest_config_global core.indexLockTimeout 0 &&\n \ttest_when_finished \"rm -rf addtest/.git/index.lock\" &&\n \t(\n \t\tcd addtest &&\n-- \n2.56.0\n\n\n"},{"id":"555302","messageId":"20261017073231.3357477-4-quark.zju@gmail.com","threadId":"66520","inReplyTo":"20261017073231.3357477-1-quark.zju@gmail.com","subject":"[PATCH v2 3/4] commit: fix write race","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-17T07:32:30Z","receivedAt":"2026-10-17T07:32:30Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"From: Jun Wu <quark@meta.com>\n\n\"commit\" today does \"read (index) -> lock -> write\" and it does not verify\nindex is unchanged after locking, therefore is racy (an update made by another\nprocess between \"read\" and \"lock\" gets overwritten). Fix the race by reordering\nto \"lock -> read -> write\". This is more correct, but at the cost of more work\n(e.g. lstat() the whole working tree) while holding the index lock.\n\nThe other purpose of fixing the write race is to make repo_hold_locked_index()\n(see previous commit) believe that \"commit\" is now race-free, so \"commit\"\nqualifies the new \"waiting for index lock\" behavior, which could be more\nfriendly to end-users (that expect \"commit\" to not fail).\n\nContext checked: history shows no particular reason for reading before locking:\n- f5bbc3225c (Port git commit to C., 2007-11-08): lock before reading\n- 2888605c64 (builtin-commit: fix partial-commit support, 2007-11-18):\n  seems to accidentally move reading to before locking\n\nHelped-by: Jeff King <peff@peff.net>\nSigned-off-by: Jun Wu <quark@meta.com>\n---\n builtin/commit.c              | 9 ++-------\n t/t0032-index-lock-timeout.sh | 5 ++---\n 2 files changed, 4 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex a808c0ba39..bf41f04da7 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -389,13 +389,13 @@ static const char *prepare_index(const char **argv, const char *prefix,\n \t    (!amend || (fixup_message && strcmp(fixup_prefix, \"amend\"))))))\n \t\tdie(_(\"No paths with --include/--only does not make sense.\"));\n \n+\trepo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);\n+\n \tif (repo_read_index_preload(the_repository, &pathspec, 0) < 0)\n \t\tdie(_(\"index file corrupt\"));\n \n \tif (interactive) {\n \t\tchar *old_index_env = NULL, *old_repo_index_file;\n-\t\trepo_hold_locked_index(the_repository, &index_lock,\n-\t\t\t\t       LOCK_DIE_ON_ERROR);\n \n \t\trefresh_cache_or_die(refresh_flags);\n \n@@ -453,8 +453,6 @@ static const char *prepare_index(const char **argv, const char *prefix,\n \t */\n \tif (all || (also && pathspec.nr)) {\n \t\tchar *ps_matched = xcalloc(pathspec.nr, 1);\n-\t\trepo_hold_locked_index(the_repository, &index_lock,\n-\t\t\t\t       LOCK_DIE_ON_ERROR);\n \t\tadd_files_to_cache(the_repository, also ? prefix : NULL,\n \t\t\t\t   &pathspec, ps_matched, 0, 0, 0 );\n \t\tif (!all && report_path_error(ps_matched, &pathspec))\n@@ -480,8 +478,6 @@ static const char *prepare_index(const char **argv, const char *prefix,\n \t * We still need to refresh the index here.\n \t */\n \tif (!only && !pathspec.nr) {\n-\t\trepo_hold_locked_index(the_repository, &index_lock,\n-\t\t\t\t       LOCK_DIE_ON_ERROR);\n \t\trefresh_cache_or_die(refresh_flags);\n \t\tif (the_repository->index->cache_changed\n \t\t    || !cache_tree_fully_valid(the_repository->index))\n@@ -531,7 +527,6 @@ static const char *prepare_index(const char **argv, const char *prefix,\n \tif (repo_read_index(the_repository) < 0)\n \t\tdie(_(\"cannot read the index\"));\n \n-\trepo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);\n \tadd_remove_files(&partial);\n \trefresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);\n \tcache_tree_update(the_repository->index, WRITE_TREE_SILENT);\ndiff --git a/t/t0032-index-lock-timeout.sh b/t/t0032-index-lock-timeout.sh\nindex cde6f1892c..18caa182d7 100755\n--- a/t/t0032-index-lock-timeout.sh\n+++ b/t/t0032-index-lock-timeout.sh\n@@ -29,13 +29,12 @@ test_expect_success 'index locked, add waits with indexLockTimeout>0' '\n \tgit -c core.indexLockTimeout=5000 add foo\n '\n \n-test_expect_success 'index locked, commit fails even with indexLockTimeout>0 (read index before lock)' '\n+test_expect_success 'index locked, commit waits with indexLockTimeout>0' '\n \tprepare_index_lock &&\n-\ttest_must_fail_with_index_lock git -c core.indexLockTimeout=5000 commit -m init\n+\tgit -c core.indexLockTimeout=5000 commit -m init\n '\n \n test_expect_success 'index locked, checkout waits with indexLockTimeout>0' '\n-\tgit commit -m init &&\n \tprepare_index_lock &&\n \tgit -c core.indexLockTimeout=5000 checkout HEAD\n '\n-- \n2.56.0\n\n\n"},{"id":"555303","messageId":"20261017073231.3357477-5-quark.zju@gmail.com","threadId":"66520","inReplyTo":"20261017073231.3357477-1-quark.zju@gmail.com","subject":"[PATCH v2 4/4] reset: fix write race","fromName":"Jun Wu","fromEmail":"quark.zju@gmail.com","sentAt":"2026-10-17T07:32:31Z","receivedAt":"2026-10-17T07:32:31Z","isPatch":true,"sender":{"key":"quark.zju@gmail.com","avatar":null},"body":"From: Jun Wu <quark@meta.com>\n\nSimilar to the previous commit \"commit: fix write race\", fix a similar race in\n\"reset\" (except for --soft, which does not lock/write the index) by changing\n\"read -> lock -> write\" to \"lock -> read -> write\", so \"reset\" now qualifies\nthe new user-friendly \"waiting for index lock\" behavior.\n\nUnlike \"commit\", \"reset\" uses repo_read_index(), not repo_read_index_preload(),\nso it won't add significant work (e.g. lstat() working tree) while holding the\nindex lock.\n\nI chose to not add a test to t0032-index-lock-timeout.sh, since every waiting\ntest case there (undesirably) takes 1 second to run, there are already\nrepresentative test cases, and the change looks straightforward.\n\nSigned-off-by: Jun Wu <quark@meta.com>\n---\n builtin/reset.c | 7 ++++---\n 1 file changed, 4 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/reset.c b/builtin/reset.c\nindex 78e69bd84b..0e1aa76aaa 100644\n--- a/builtin/reset.c\n+++ b/builtin/reset.c\n@@ -346,6 +346,7 @@ int cmd_reset(int argc,\n \tstruct object_id oid;\n \tstruct pathspec pathspec;\n \tint intent_to_add = 0;\n+\tstruct lock_file lock = LOCK_INIT;\n \tstruct interactive_options interactive_opts = INTERACTIVE_OPTIONS_INIT;\n \tconst struct option options[] = {\n \t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n@@ -477,6 +478,9 @@ int cmd_reset(int argc,\n \tif (intent_to_add && reset_type != MIXED)\n \t\tdie(_(\"the option '%s' requires '%s'\"), \"-N\", \"--mixed\");\n \n+\tif (reset_type != SOFT)\n+\t\trepo_hold_locked_index(the_repository, &lock,\n+\t\t\t\t       LOCK_DIE_ON_ERROR);\n \tif (repo_read_index(the_repository) < 0)\n \t\tdie(_(\"index file corrupt\"));\n \n@@ -487,9 +491,6 @@ int cmd_reset(int argc,\n \t\tdie_if_unmerged_cache(reset_type);\n \n \tif (reset_type != SOFT) {\n-\t\tstruct lock_file lock = LOCK_INIT;\n-\t\trepo_hold_locked_index(the_repository, &lock,\n-\t\t\t\t       LOCK_DIE_ON_ERROR);\n \t\tif (reset_type == MIXED) {\n \t\t\tint flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;\n \t\t\tif (read_from_tree(&pathspec, &oid, intent_to_add)) {\n-- \n2.56.0\n\n\n"},{"id":"555435","messageId":"F02EB9EC-E157-49D2-BF27-81DFF625D884@gmail.com","threadId":"66520","inReplyTo":"20261014221233.1694344-1-quark.zju@gmail.com","subject":"Re: [PATCH 1/2] repository: add core.indexLockTimeout","fromName":"Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-19T16:23:40Z","receivedAt":"2026-10-19T16:23:40Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"\n> Le 14 oct. 2026 à 18:13, Jun Wu <quark.zju@gmail.com> a écrit :\n> \n> ﻿On Tue, 13 Oct 2026 15:05:52 -0700, Junio C Hamano wrote:\n>>> Default timeout is 1s, effective but not too long to avoid breaking potential\n>>> user expectation (e.g. \"an index.lock error means I should check whether a\n>>> commit message editor is open somewhere\"). Tests that are slowed down by the\n>>> default timeout now set the timeout to 0.\n> \n>> I am not sure if the change is making the right trade-off between\n>> background tasks and end-user action that expects immediate\n>> response.\n> \n> It seems the trade-off is between command success rate and command duration.\n> \n> I suspect that end-users want commands to work (not fail) first, performance\n> comes next. In my experience, high command failure rate escalates to oncall\n> incidents, occasional slightly slower commands often get unnoticed unless we\n> look at the metrics explicitly.\n\nI’ve given it some more thought, and acknowledging that \nI’m unaffected by the bug being fixed, I would add:\n\nIt seems like working flows have to become more complex \nto handle these broken flows. And that leaves a sour taste for me.\nFolks do notice hangups at a shell that is typically quick to respond.\n\nBut, as I said, I’ll bow out since it seems like there’s some support\nfrom more experienced folks for this direction.\n"},{"id":"555898","messageId":"20261022184906.3679783-1-mcosma@gmail.com","threadId":"66520","inReplyTo":"20261017073231.3357477-1-quark.zju@gmail.com","subject":"Re: [PATCH v2 0/4] Wait for the index lock instead of failing","fromName":"Mihai Cosma","fromEmail":"mcosma@gmail.com","sentAt":"2026-10-22T18:49:06Z","receivedAt":"2026-10-22T18:49:06Z","isPatch":true,"sender":{"key":"mcosma@gmail.com","avatar":null},"body":"Hi Jun,\n\nI've been working on the same problem from the status side and just\nsent a two-patch series:\n\n  https://lore.kernel.org/git/20261022184420.3648771-1-mcosma@gmail.com/\n\nStatus takes index.lock before collecting and holds it to the end, even\nwhen it has nothing to write, so it's often the process \"git add\" and\n\"git commit\" end up waiting on. My series makes it lock only to write.\nOn top of your v2, in a gcc checkout with status running in a loop, the\nslowest of 200 \"git add\" runs went from 1020 ms (one hit your 1s\ntimeout) to 160 ms.\n\nThe two only conflict on the lock call in cmd_status() that your 1/4\nrenames, so whichever lands second can pick up the other's change. My\nfirst patch also makes repo_update_index_if_able()'s check work under\nindex.skipHash, which the read-then-lock callers your series leaves\nalone rely on.\n\nThanks,\nMihai\n\n"}]}