Skip to content

Commit c715f78

Browse files
spearceJunio C Hamano
authored andcommitted
Don't find objects in packs which aren't available anymore.
Matthias Lederhofer identified a race condition where a Git reader process was able to locate an object in a packed_git index, but was then preempted while a `git repack -a -d` ran and completed. By the time the reader was able to seek in the packfile to get the object data, the packfile no longer existed on disk. In this particular case the reader process did not attempt to open the packfile before it was deleted, so it did not already have the pack_fd field popuplated. With the packfile itself gone, there was no way for the reader to open it and fetch the data. I'm fixing the race condition by teaching find_pack_entry to ignore a packed_git whose packfile is not currently open and which cannot be opened. If none of the currently known packs can supply the object, we will return 0 and the caller will decide the object is not available. If this is the first attempt at finding an object, the caller will reprepare_packed_git and try again. If it was the second attempt, the caller will typically return NULL back, and an error message about a missing object will be reported. This patch does not address the situation of a reader which is being starved out by a tight sequence of `git repack -a -d` runs. In this particular case the reader will try twice, probably fail both times, and declare the object in question cannot be found. As it is highly unlikely that a real world `git repack -a -d` can complete faster than a reader can open a packfile, so I don't think this is a huge concern. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 072db27 commit c715f78

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

sha1_file.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,18 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, cons
14081408
}
14091409
offset = find_pack_entry_one(sha1, p);
14101410
if (offset) {
1411+
/*
1412+
* We are about to tell the caller where they can
1413+
* locate the requested object. We better make
1414+
* sure the packfile is still here and can be
1415+
* accessed before supplying that answer, as
1416+
* it may have been deleted since the index
1417+
* was loaded!
1418+
*/
1419+
if (p->pack_fd == -1 && open_packed_git(p)) {
1420+
error("packfile %s cannot be accessed", p->pack_name);
1421+
continue;
1422+
}
14111423
e->offset = offset;
14121424
e->p = p;
14131425
hashcpy(e->sha1, sha1);

0 commit comments

Comments
 (0)