Skip to content

Commit fed6ebe

Browse files
mhaggergitster
authored andcommitted
lock_packed_refs(): fix cache validity check
Commit 28ed983 (get_packed_ref_cache(): assume "packed-refs" won't change while locked, 2017-05-22) assumes that the "packed-refs" file cannot change while we hold the lock. That assumption is justified *if* the lock has been held the whole time since the "packed-refs" file was last read. But in `lock_packed_refs()`, we ourselves lock the "packed-refs" file and then call `get_packed_ref_cache()` to ensure that the cache agrees with the file. The intent is to guard against the possibility that another process changed the "packed-refs" file the moment before we locked it. This check was defeated because `get_packed_ref_cache()` saw that the file was locked, and therefore didn't do the `stat_validity_check()` that we want. The mistake was compounded with a misleading comment in `lock_packed_refs()` claiming that it was doing the right thing. That comment came from an earlier draft of the mh/packed-ref-store-prep patch series when the commits were in a different order. So instead: * Extract a function `validate_packed_ref_cache()` that does the validity check independent of whether the lock is held. * Change `get_packed_ref_cache()` to call the new function, but only if the lock *isn't* held. * Change `lock_packed_refs()` to call the new function in any case before calling `get_packed_ref_cache()`. * Fix the comment in `lock_packed_refs()`. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f23092f commit fed6ebe

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

refs/files-backend.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ static void files_ref_path(struct files_ref_store *refs,
369369
}
370370
}
371371

372+
/*
373+
* Check that the packed refs cache (if any) still reflects the
374+
* contents of the file. If not, clear the cache.
375+
*/
376+
static void validate_packed_ref_cache(struct files_ref_store *refs)
377+
{
378+
if (refs->packed &&
379+
!stat_validity_check(&refs->packed->validity,
380+
files_packed_refs_path(refs)))
381+
clear_packed_ref_cache(refs);
382+
}
383+
372384
/*
373385
* Get the packed_ref_cache for the specified files_ref_store,
374386
* creating and populating it if it hasn't been read before or if the
@@ -381,10 +393,8 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
381393
{
382394
const char *packed_refs_file = files_packed_refs_path(refs);
383395

384-
if (refs->packed &&
385-
!is_lock_file_locked(&refs->packed_refs_lock) &&
386-
!stat_validity_check(&refs->packed->validity, packed_refs_file))
387-
clear_packed_ref_cache(refs);
396+
if (!is_lock_file_locked(&refs->packed_refs_lock))
397+
validate_packed_ref_cache(refs);
388398

389399
if (!refs->packed)
390400
refs->packed = read_packed_refs(packed_refs_file);
@@ -1311,13 +1321,17 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
13111321
&refs->packed_refs_lock, files_packed_refs_path(refs),
13121322
flags, timeout_value) < 0)
13131323
return -1;
1324+
13141325
/*
1315-
* Get the current packed-refs while holding the lock. It is
1316-
* important that we call `get_packed_ref_cache()` before
1317-
* setting `packed_ref_cache->lock`, because otherwise the
1318-
* former will see that the file is locked and assume that the
1319-
* cache can't be stale.
1326+
* Now that we hold the `packed-refs` lock, make sure that our
1327+
* cache matches the current version of the file. Normally
1328+
* `get_packed_ref_cache()` does that for us, but that
1329+
* function assumes that when the file is locked, any existing
1330+
* cache is still valid. We've just locked the file, but it
1331+
* might have changed the moment *before* we locked it.
13201332
*/
1333+
validate_packed_ref_cache(refs);
1334+
13211335
packed_ref_cache = get_packed_ref_cache(refs);
13221336
/* Increment the reference count to prevent it from being freed: */
13231337
acquire_packed_ref_cache(packed_ref_cache);

0 commit comments

Comments
 (0)