Repository navigation
git: avoid racy temp-index stats for untracked files - #3014
Conversation
working_tree_diff_stats_with_untracked() used a copied index to make untracked files visible to git diff: first git add --intent-to-add ., then git diff --shortstat HEAD against that temporary index. That means the copied index also has to rediscover tracked working-tree modifications. In a riscv64 qemu-user chroot, the test passes by itself and the full lib test suite passes with --test-threads=1, but the default concurrent lib test run reliably exposes a failure. Diagnostic output from the failing test showed tracked.txt already contained the new contents, while git status and git diff through the temporary index reported only untracked.txt. The tracked rewrite was same-size, so the copied index could treat it as racily clean under this slower, heavily concurrent qemu-user workload. Do not use the temporary index to rediscover tracked changes. Keep the existing git diff --shortstat HEAD path for tracked, staged, and unstaged changes, then list untracked files with git ls-files --others --exclude-standard -z, stage only those paths into the temporary index, and sum their cached numstat output. The real index remains untouched, while the qemu-user-only racy-index window is avoided.
do we know where this race comes from? @worktrunk-bot please can you explain it to me? |
|
This comes from Git's racy-index heuristic interacting with the copied temp index. The short version: the old code asked a freshly-written temp index to rediscover tracked modifications, and that rediscovery relies on a stat comparison that Git deliberately treats as untrustworthy in a narrow timing window — a window the copy re-opens. Git's stat cache and the racy ruleTo make That optimization has a hole: a file can be modified, the index written, and the file modified again, all within one filesystem timestamp tick. The second modification would leave the mtime unchanged, so the stale stat would still "match." Git guards against this with the racy rule:
The reference point for all of this is the index file's own timestamp. Why the temp-index path was vulnerableThe old The
So The real index never has this problem here because the change goes through Why only slow riscv64-under-qemuOn a normal machine two things hide the bug:
qemu-user emulating riscv64 reports coarse/second-granularity timestamps (and can zero the nsec field that the racy comparison leans on), and the heavily-concurrent default test run slows wall-clock progress enough that the rewrite and the temp-index write frequently share a tick. That's why it's reliable there, passes in isolation, and passes with Why the fix avoids itAfter the change the temp index is only ever used for untracked paths that we explicitly — 🤖 worktrunk-bot |
|
thanks! if we copied the index and maintained its timestamp, would that work? |
|
Short answer: no, not as described — because the vulnerable write happens inside Why preserving the copy's timestamp doesn't surviveThe racy reference point is the index file's own mtime ( The The direction that would work — and why we didn't do itYou can force the catch by going the opposite way: after the add, backdate the temp index file's mtime to before the working-tree edits. Then every entry has But that's:
So the merged fix instead just never asks the temp index to judge tracked files: tracked/staged/unstaged come from — 🤖 worktrunk-bot |
|
Thanks for the great review and explanation, max-sixty and bot!
Nit: The bug occurs not only under qemu, but also on real riscv64 hardware which are mostly slow. |
|
You're right, and thanks for the correction — I'll own that "qemu-user-only" framing was wrong. The racy-index trap isn't an emulation artifact at all; it's purely about timestamp granularity. Git's racy rule leans on So the accurate framing is "coarse-timestamp + slow + same-size rewrite," not "qemu." The fix is unaffected either way, since it stops asking the temp index to judge tracked files at all. One heads-up: the same "qemu-user-only" wording is in the commit message body ( — 🤖 worktrunk-bot |
|
@Xeonacid thank you very much for the thoughtful and technically deep change! |
working_tree_diff_stats_with_untracked()used a copied index to make untracked files visible to git diff: firstgit add --intent-to-add ., thengit diff --shortstat HEADagainst that temporary index. That means the copied index also has to rediscover tracked working-tree modifications.On a slow riscv64 machine, the test passes by itself and the full lib test suite passes with
--test-threads=1, but the default concurrent lib test run reliably exposes a failure. Diagnostic output from the failing test showed tracked.txt already contained the new contents, while git status and git diff through the temporary index reported only untracked.txt. The tracked rewrite was same-size, so the copied index could treat it as racily clean under this slow riscv64 machine.Do not use the temporary index to rediscover tracked changes. Keep the existing
git diff --shortstat HEADpath for tracked, staged, and unstaged changes, then list untracked files withgit ls-files --others --exclude-standard -z, stage only those paths into the temporary index, and sum their cached numstat output. The real index remains untouched, while the racy-index window is avoided.