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

[PATCH v2 4/4] reset: fix write race

From
JWJun Wu <quark.zju@gmail.com>
Date
Oct 17, 2026, 07:32 UTC
Message-ID
<20261017073231.3357477-5-quark.zju@gmail.com>
In-Reply-To
<20261017073231.3357477-1-quark.zju@gmail.com>
From: Jun Wu <quark@meta.com>

Similar to the previous commit "commit: fix write race", fix a similar race in "reset" (except for --soft, which does not lock/write the index) by changing "read -> lock -> write" to "lock -> read -> write", so "reset" now qualifies the new user-friendly "waiting for index lock" behavior.

Unlike "commit", "reset" uses repo_read_index(), not repo_read_index_preload(), so it won't add significant work (e.g. lstat() working tree) while holding the index lock.

I chose to not add a test to t0032-index-lock-timeout.sh, since every waiting test case there (undesirably) takes 1 second to run, there are already representative test cases, and the change looks straightforward.

Signed-off-by: Jun Wu <quark@meta.com>
---
 builtin/reset.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/builtin/reset.c b/builtin/reset.c
index 78e69bd84b..0e1aa76aaa 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -346,6 +346,7 @@ int cmd_reset(int argc,
 	struct object_id oid;
 	struct pathspec pathspec;
 	int intent_to_add = 0;
+	struct lock_file lock = LOCK_INIT;
 	struct interactive_options interactive_opts = INTERACTIVE_OPTIONS_INIT;
 	const struct option options[] = {
 		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
@@ -477,6 +478,9 @@ int cmd_reset(int argc,
 	if (intent_to_add && reset_type != MIXED)
 		die(_("the option '%s' requires '%s'"), "-N", "--mixed");
 
+	if (reset_type != SOFT)
+		repo_hold_locked_index(the_repository, &lock,
+				       LOCK_DIE_ON_ERROR);
 	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
@@ -487,9 +491,6 @@ int cmd_reset(int argc,
 		die_if_unmerged_cache(reset_type);
 
 	if (reset_type != SOFT) {
-		struct lock_file lock = LOCK_INIT;
-		repo_hold_locked_index(the_repository, &lock,
-				       LOCK_DIE_ON_ERROR);
 		if (reset_type == MIXED) {
 			int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
 			if (read_from_tree(&pathspec, &oid, intent_to_add)) {
-- 
2.56.0
Previous: Jun WuNext: Mihai Cosma
Message 19 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.