git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/2] commit: lock index before reading

From
Jeff King <peff@peff.net>
Date
Oct 14, 2026, 10:04 UTC
Message-ID
<20261014100432.GB1344504@coredump.intra.peff.net>
In-Reply-To
<20261013172315.2997446-3-quark.zju@gmail.com>
On Tue, Oct 13, 2026 at 10:23:15AM -0700, Jun Wu wrote:
Show 9 quoted lines
> Take the index lock before reading the index, in one place, instead
> of separately in each `if` branch.
> 
> `commit` now respects the `core.indexLockTimeout` config.
> 
> History suggests no particular reason for reading before locking:
> - f5bbc3225c (Port git commit to C., 2007-11-08): lock before reading
> - 2888605c64 (builtin-commit: fix partial-commit support, 2007-11-18):
>   seems to accidentally move reading to before locking

Hmm. We may do a lot more work under lock than we used to, running stat() on the whole repo.

But I think there's a deeper issue here. We're reading the index, _then_ taking the lock, but not necessarily discarding what we read earlier. So it feels like two processes could race here, with git-commit discarding some update by another process (like git-add).

If we instrument git-commit like this:
diff --git a/builtin/commit.c b/builtin/commit.c
index 840b6b4083..2a78cb473a 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -392,6 +392,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
 	if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
 		die(_("index file corrupt"));
 
+	sleep(5);
+
 	if (interactive) {
 		char *old_index_env = NULL, *old_repo_index_file;
 		repo_hold_locked_index(the_repository, &index_lock,

that widens the race window. And then something like this:

  echo new content >old-file
  git commit -am foo &
  sleep 1 ;# commit is sleeping in race window

  git add new-file

will clobber the index change from git-add.

And that is true with today's code, not using any timeouts. Adding a
timeout doesn't really change anything, though if you _do_ see lock
contention it is a good sign that you are about to hit the race and
clobber something. ;) So it is widening the window. But the correctness
problem is already there.

I do think your patch fixes the problem. But we probably want to discuss
that in the commit message as a motivation (and likewise, discuss the
fact that we may be holding the lock for longer).

There's probably some kind of hybrid world where we optimistically read
without the lock, then take the lock, and then check (cheaply-ish) that
the index didn't change since what we read. But I don't think we do that
in this code path.

-Peff
Previous: Jun WuNext: Jun Wu
Message 10 of 20 in “Add a way to wait for index lock”
  1. 0/2 Add a way to wait for index lockJun Wu, Oct 13, 2026
  2. 1/2 repository: add core.indexLockTimeoutJun Wu, Oct 13, 2026
  3. D. Ben KnobleOct 13, 2026
  4. Jun WuOct 14, 2026
  5. D. Ben KnobleOct 14, 2026
  6. Junio C HamanoOct 13, 2026
  7. Jun WuOct 14, 2026
  8. Ben KnobleOct 19, 2026
  9. 2/2 commit: lock index before readingJun Wu, Oct 13, 2026
  10. Jeff KingOct 14, 2026
  11. Jun WuOct 15, 2026
  12. Junio C HamanoOct 13, 2026
  13. Jun WuOct 14, 2026
  14. Jeff KingOct 14, 2026
  15. 0/4 Wait for the index lock instead of failingJun Wu, Oct 17, 2026
  16. 1/4 repository: add repo_try_hold_locked_index()Jun Wu, Oct 17, 2026
  17. 2/4 repository: add core.indexLockTimeoutJun Wu, Oct 17, 2026
  18. 3/4 commit: fix write raceJun Wu, Oct 17, 2026
  19. 4/4 reset: fix write raceJun Wu, Oct 17, 2026
  20. Mihai CosmaOct 22, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.