Skip to content

Commit 5581a01

Browse files
szedergitster
authored andcommitted
split-index: smudge and add racily clean cache entries to split index
Ever since the split index feature was introduced [1], refreshing a split index is prone to a variant of the classic racy git problem. Consider the following sequence of commands updating the split index when the shared index contains a racily clean cache entry, i.e. an entry whose cached stat data matches with the corresponding file in the worktree and the cached mtime matches that of the index: echo "cached content" >file git update-index --split-index --add file echo "dirty worktree" >file # size stays the same! # ... wait ... git update-index --add other-file Normally, when a non-split index is updated, then do_write_index() (the function responsible for writing all kinds of indexes, "regular", split, and shared) recognizes racily clean cache entries, and writes them with smudged stat data, i.e. with file size set to 0. When subsequent git commands read the index, they will notice that the smudged stat data doesn't match with the file in the worktree, and then go on to check the file's content and notice its dirtiness. In the above example, however, in the second 'git update-index' prepare_to_write_split_index() decides which cache entries stored only in the shared index should be replaced in the new split index. Alas, this function never looks out for racily clean cache entries, and since the file's stat data in the worktree hasn't changed since the shared index was written, it won't be replaced in the new split index. Consequently, do_write_index() doesn't even get this racily clean cache entry, and can't smudge its stat data. Subsequent git commands will then see that the index has more recent mtime than the file and that the (not smudged) cached stat data still matches with the file in the worktree, and, ultimately, will erroneously consider the file clean. Modify prepare_to_write_split_index() to recognize racily clean cache entries, and mark them to be added to the split index. Note that there are two places where it should check raciness: first those cache entries that are only stored in the shared index, and then those that have been copied by unpack_trees() from the shared index while it constructed a new index. This way do_write_index() will get these racily clean cache entries as well, and will then write them with smudged stat data to the new split index. This change makes all tests in 't1701-racy-split-index.sh' pass, so flip the two 'test_expect_failure' tests to success. Also add the '#' (as in nr. of trial) to those tests' description that were omitted when the tests expected failure. Note that after this change if the index is split when it contains a racily clean cache entry, then a smudged cache entry will be written both to the new shared and to the new split indexes. This doesn't affect regular git commands: as far as they are concerned this is just an entry in the split index replacing an outdated entry in the shared index. It did affect a few tests in 't1700-split-index.sh', though, because they actually check which entries are stored in the split index; a previous patch in this series has already made the necessary adjustments in 't1700'. And racily clean cache entries and index splitting are rare enough to not worry about the resulting duplicated smudged cache entries, and the additional complexity required to prevent them is not worth it. Several tests failed occasionally when the test suite was run with 'GIT_TEST_SPLIT_INDEX=yes'. Here are those that I managed to trace back to this racy split index problem, starting with those failing more frequently, with a link to a failing Travis CI build job for each. The highlighted line [2] shows when the racy file was written, which is not always in the failing test but in a preceeding setup test. t3903-stash.sh: https://travis-ci.org/git/git/jobs/385542084#L5858 t4024-diff-optimize-common.sh: https://travis-ci.org/git/git/jobs/386531969#L3174 t4015-diff-whitespace.sh: https://travis-ci.org/git/git/jobs/360797600#L8215 t2200-add-update.sh: https://travis-ci.org/git/git/jobs/382543426#L3051 t0090-cache-tree.sh: https://travis-ci.org/git/git/jobs/416583010#L3679 There might be others, e.g. perhaps 't1000-read-tree-m-3way.sh' and others using 'lib-read-tree-m-3way.sh', but I couldn't confirm yet. [1] In the branch leading to the merge commit v2.1.0-rc0~45 (Merge branch 'nd/split-index', 2014-07-16). [2] Note that those highlighted lines are in the 'after failure' fold, and your browser might unhelpfully fold it up before you could take a good look. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e3d8379 commit 5581a01

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ extern void *read_blob_data_from_index(const struct index_state *, const char *,
781781
#define CE_MATCH_REFRESH 0x10
782782
/* don't refresh_fsmonitor state or do stat comparison even if CE_FSMONITOR_VALID is true */
783783
#define CE_MATCH_IGNORE_FSMONITOR 0X20
784+
extern int is_racy_timestamp(const struct index_state *istate,
785+
const struct cache_entry *ce);
784786
extern int ie_match_stat(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
785787
extern int ie_modified(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
786788

read-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ static int is_racy_stat(const struct index_state *istate,
337337
);
338338
}
339339

340-
static int is_racy_timestamp(const struct index_state *istate,
340+
int is_racy_timestamp(const struct index_state *istate,
341341
const struct cache_entry *ce)
342342
{
343343
return (!S_ISGITLINK(ce->ce_mode) &&

split-index.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,39 @@ void prepare_to_write_split_index(struct index_state *istate)
259259
}
260260
ce->ce_flags |= CE_MATCHED; /* or "shared" */
261261
base = si->base->cache[ce->index - 1];
262-
if (ce == base)
262+
if (ce == base) {
263+
/* The entry is present in the shared index. */
264+
if (ce->ce_flags & CE_UPDATE_IN_BASE) {
265+
/*
266+
* Already marked for inclusion in
267+
* the split index, either because
268+
* the corresponding file was
269+
* modified and the cached stat data
270+
* was refreshed, or because there
271+
* is already a replacement entry in
272+
* the split index.
273+
* Nothing more to do here.
274+
*/
275+
} else if (!ce_uptodate(ce) &&
276+
is_racy_timestamp(istate, ce)) {
277+
/*
278+
* A racily clean cache entry stored
279+
* only in the shared index: it must
280+
* be added to the split index, so
281+
* the subsequent do_write_index()
282+
* can smudge its stat data.
283+
*/
284+
ce->ce_flags |= CE_UPDATE_IN_BASE;
285+
} else {
286+
/*
287+
* The entry is only present in the
288+
* shared index and it was not
289+
* refreshed.
290+
* Just leave it there.
291+
*/
292+
}
263293
continue;
294+
}
264295
if (ce->ce_namelen != base->ce_namelen ||
265296
strcmp(ce->name, base->name)) {
266297
ce->index = 0;
@@ -281,6 +312,15 @@ void prepare_to_write_split_index(struct index_state *istate)
281312
* the split index.
282313
* Nothing to do.
283314
*/
315+
} else if (!ce_uptodate(ce) &&
316+
is_racy_timestamp(istate, ce)) {
317+
/*
318+
* A copy of a racily clean cache entry from
319+
* the shared index. It must be added to
320+
* the split index, so the subsequent
321+
* do_write_index() can smudge its stat data.
322+
*/
323+
ce->ce_flags |= CE_UPDATE_IN_BASE;
284324
} else {
285325
/*
286326
* Thoroughly compare the cached data to see

t/t1701-racy-split-index.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ done
148148

149149
for trial in $trials
150150
do
151-
test_expect_failure "update the split index when a racily clean cache entry is stored only in the shared index $trial" '
151+
test_expect_success "update the split index when a racily clean cache entry is stored only in the shared index #$trial" '
152152
rm -f .git/index .git/sharedindex.* &&
153153
154154
# The next three commands must be run within the same
@@ -170,8 +170,6 @@ do
170170
# entry of racy-file is only stored in the shared index.
171171
# A corresponding replacement cache entry with smudged
172172
# stat data should be added to the new split index.
173-
#
174-
# Alas, such a smudged replacement entry is not added!
175173
git update-index --add other-file &&
176174
177175
# Subsequent git commands should notice the smudged
@@ -182,7 +180,7 @@ done
182180

183181
for trial in $trials
184182
do
185-
test_expect_failure "update the split index after unpack trees() copied a racily clean cache entry from the shared index $trial" '
183+
test_expect_success "update the split index after unpack trees() copied a racily clean cache entry from the shared index #$trial" '
186184
rm -f .git/index .git/sharedindex.* &&
187185
188186
# The next three commands must be run within the same
@@ -205,8 +203,6 @@ do
205203
# index. A corresponding replacement cache entry
206204
# with smudged stat data should be added to the new
207205
# split index.
208-
#
209-
# Alas, such a smudged replacement entry is not added!
210206
git read-tree -m HEAD &&
211207
212208
# Subsequent git commands should notice the smudged

0 commit comments

Comments
 (0)