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

[PATCH] t4129: fix racy index when calling chmod after git-add

From
Jeff King <peff@peff.net>
Date
Aug 15, 2024, 15:30 UTC
Message-ID
<20240815153007.GA1477220@coredump.intra.peff.net>
In-Reply-To
<xmqqcym9vnwg.fsf@gitster.g>
On Thu, Aug 15, 2024 at 07:52:47AM -0700, Junio C Hamano wrote:
Show 10 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > +test_expect_success POSIXPERM 'patch mode for deleted file is canonicalized' '
> 
> This test seems to fail under "--stress" and I need to borrow a
> brain better clued than mine.  It appears to be fooled by mtime that
> is not updated immediately and failing match_stat check, but since
> the index file is written on the other side of the second resolution
> boundary, racy-git double-checking code does not trigger, or
> something like that.

Ah, thanks. I saw this fail once in CI, and then later succeed. But for some reason I wrote it off as CI flakiness rather than trying --stress locally. It's easy to reproduce the issue.

I didn't puzzle out the exact condition that causes the race, but I think the fix is pretty clearly this:

-- >8 --
Subject: [PATCH] t4129: fix racy index when calling chmod after git-add
This patch fixes a racy test failure in t4129.

The deletion test added by e95d515141 (apply: canonicalize modes read from patches, 2024-08-05) wants to make sure that git-apply does not complain about a non-canonical mode in the patch, even if that mode does not match the working tree file. So it does this:

	echo content >non-canon &&
	git add non-canon &&
	chmod 666 non-canon &&

This is wrong, because running chmod will update the ctime on the file, making it stat-dirty and causing git-apply to refuse to apply the patch. But this only happens sometimes, since it depends on the timestamps crossing a second boundary (but it triggers pretty quickly when run with --stress).

We can fix this by doing the chmod before updating the index. The order isn't important here, as the mode will be canonicalized to 100644 in the index anyway (in fact, the chmod is not even that important in the first place, since git-apply will only look at the index; I only added it as an extra confirmation that git-apply would not be confused by it).

Signed-off-by: Jeff King <peff@peff.net>
---
 t/t4129-apply-samemode.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/t/t4129-apply-samemode.sh b/t/t4129-apply-samemode.sh
index d9a1084b5e..87ffd2b8e1 100755
--- a/t/t4129-apply-samemode.sh
+++ b/t/t4129-apply-samemode.sh
@@ -153,8 +153,8 @@ test_expect_success POSIXPERM 'patch mode for new file is canonicalized' '
 test_expect_success POSIXPERM 'patch mode for deleted file is canonicalized' '
 	test_when_finished "git reset --hard" &&
 	echo content >non-canon &&
-	git add non-canon &&
 	chmod 666 non-canon &&
+	git add non-canon &&
 
 	cat >patch <<-\EOF &&
 	diff --git a/non-canon b/non-canon
-- 
2.46.0.476.g32f0e7348d
Previous: Junio C HamanoNext: Junio C Hamano
Message 9 of 10 in “quiltimport mode detection oddity”
  1. Andrew MortonAug 1, 2024
  2. Junio C HamanoAug 2, 2024
  3. Andrew MortonAug 2, 2024
  4. Jeff KingAug 2, 2024
  5. Andrew MortonAug 2, 2024
  6. Junio C HamanoAug 2, 2024
  7. apply: canonicalize modes read from patchesJeff King, Aug 5, 2024
  8. Junio C HamanoAug 15, 2024
  9. t4129: fix racy index when calling chmod after git-addJeff King, Aug 15, 2024
  10. Junio C HamanoAug 15, 2024

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.