Commit
8f909ff4e9e883bf4938c0f0f67b9e1d48cc0167
packfile: recover when a multi-pack-index names a removed pack
A geometric repack writes a new pack and multi-pack-index and then deletes the packs the new one subsumes. A process still using the previous MIDX keeps seeing a removed pack listed as the owner of some objects. Since a MIDX attributes each object to exactly one pack, such an object is served only through its recorded owner; if that owner was just removed, find_pack_entry() cannot serve it -- the MIDX lookup routes to the missing pack, and the regular pack fallback deliberately skips every MIDX-covered pack, so a surviving copy in another covered pack (e.g. a kept base pack) is never consulted. Unlike the ordinary "a pack's .idx is mapped but its .pack is gone" race, the second read does not rescue us. Reloading the on-disk pack set does not reload the borrowed, cached MIDX (freeing it under the code that caches the "struct multi_pack_index *" would be a use-after-free), so the stale MIDX keeps routing to the removed pack and the surviving copy stays hidden behind the covered-pack skip. cat-file, rev-list and pack-objects can thus all spuriously fail with "unable to read object". Teach find_pack_entry() to recover. The MIDX lookup now returns a tri-state, distinguishing an object absent from the MIDX from one it owns via a pack that can no longer be opened; in the latter case, once the regular fallback has also missed, scan the MIDX's packs directly for a surviving copy. Because the return value is no longer a boolean, rename fill_midx_entry() to midx_fill_entry() so callers must reckon with the new enum rather than silently treat MIDX_FILL_OWNER_UNAVAILABLE as a hit. Do the scan only on the second read (OBJECT_INFO_SECOND_READ): by then the cheaper on-disk reload has run, so an object merely relocated into a new (uncovered) pack has already been found by the regular fallback, and only a genuine hidden duplicate reaches the rescan. A QUICK caller that skips the second read simply accepts the false negative, as QUICK is designed to. Reloading the stale MIDX would be a more complete fix but is much more involved (the borrowers above need proper invalidation), so leave that for later. Assisted-by: Claude Opus 4.8 & GPT-5.6 Sol Helped-by: Jeff King <peff@peff.net> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Elijah Newren <newren@gmail.com> · 2026-08-29 07:00 UTC · committed by Junio C Hamano
6 files changed,
+108
−19
- builtin/pack-objects.c
- midx.c
- midx.h
- odb/source-packed.c
- t/helper/test-read-midx.c
- t/t5319-multi-pack-index.sh
modified builtin/pack-objects.c
| @@ -1786,7 +1786,7 @@ static int want_object_in_pack_mtime(const struct object_id *oid, | |||
| 1786 | 1786 | struct multi_pack_index *m = get_multi_pack_index(files->packed); | |
| 1787 | 1787 | struct pack_entry e; | |
| 1788 | 1788 | ||
| 1789 | - | if (m && fill_midx_entry(m, oid, &e, NULL)) { | |
| 1789 | + | if (m && midx_fill_entry(m, oid, &e, NULL) == MIDX_FILL_HIT) { | |
| 1790 | 1790 | want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset, found_mtime); | |
| 1791 | 1791 | if (want != -1) | |
| 1792 | 1792 | return want; | |
modified midx.c
| @@ -589,23 +589,23 @@ uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos) | |||
| 589 | 589 | (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH); | |
| 590 | 590 | } | |
| 591 | 591 | ||
| 592 | - | int fill_midx_entry(struct multi_pack_index *m, | |
| 593 | - | const struct object_id *oid, | |
| 594 | - | struct pack_entry *e, | |
| 595 | - | struct packed_git **bad_pack) | |
| 592 | + | enum midx_fill_result midx_fill_entry(struct multi_pack_index *m, | |
| 593 | + | const struct object_id *oid, | |
| 594 | + | struct pack_entry *e, | |
| 595 | + | struct packed_git **bad_pack) | |
| 596 | 596 | { | |
| 597 | 597 | uint32_t pos; | |
| 598 | 598 | uint32_t pack_int_id; | |
| 599 | 599 | struct packed_git *p; | |
| 600 | 600 | ||
| 601 | 601 | if (!bsearch_midx(oid, m, &pos)) | |
| 602 | - | return 0; | |
| 602 | + | return MIDX_FILL_MISS; | |
| 603 | 603 | ||
| 604 | 604 | midx_for_object(&m, pos); | |
| 605 | 605 | pack_int_id = nth_midxed_pack_int_id(m, pos); | |
| 606 | 606 | ||
| 607 | 607 | if (prepare_midx_pack(m, pack_int_id)) | |
| 608 | - | return 0; | |
| 608 | + | return MIDX_FILL_OWNER_UNAVAILABLE; | |
| 609 | 609 | p = m->packs[pack_int_id - m->num_packs_in_base]; | |
| 610 | 610 | ||
| 611 | 611 | /* | |
| @@ -616,19 +616,19 @@ int fill_midx_entry(struct multi_pack_index *m, | |||
| 616 | 616 | * loaded! | |
| 617 | 617 | */ | |
| 618 | 618 | if (!is_pack_valid(p)) | |
| 619 | - | return 0; | |
| 619 | + | return MIDX_FILL_OWNER_UNAVAILABLE; | |
| 620 | 620 | ||
| 621 | 621 | if (oidset_size(&p->bad_objects) && | |
| 622 | 622 | oidset_contains(&p->bad_objects, oid)) { | |
| 623 | 623 | if (bad_pack && !*bad_pack) | |
| 624 | 624 | *bad_pack = p; | |
| 625 | - | return 0; | |
| 625 | + | return MIDX_FILL_MISS; | |
| 626 | 626 | } | |
| 627 | 627 | ||
| 628 | 628 | e->offset = nth_midxed_offset(m, pos); | |
| 629 | 629 | e->p = p; | |
| 630 | 630 | ||
| 631 | - | return 1; | |
| 631 | + | return MIDX_FILL_HIT; | |
| 632 | 632 | } | |
| 633 | 633 | ||
| 634 | 634 | /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */ | |
| @@ -1032,7 +1032,7 @@ int verify_midx_file(struct odb_source_packed *source, unsigned flags) | |||
| 1032 | 1032 | ||
| 1033 | 1033 | nth_midxed_object_oid(&oid, m, pairs[i].pos); | |
| 1034 | 1034 | ||
| 1035 | - | if (!fill_midx_entry(m, &oid, &e, NULL)) { | |
| 1035 | + | if (midx_fill_entry(m, &oid, &e, NULL) != MIDX_FILL_HIT) { | |
| 1036 | 1036 | midx_report(_("failed to load pack entry for oid[%d] = %s"), | |
| 1037 | 1037 | pairs[i].pos, oid_to_hex(&oid)); | |
| 1038 | 1038 | continue; | |
modified midx.h
| @@ -117,8 +117,25 @@ uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos); | |||
| 117 | 117 | struct object_id *nth_midxed_object_oid(struct object_id *oid, | |
| 118 | 118 | struct multi_pack_index *m, | |
| 119 | 119 | uint32_t n); | |
| 120 | - | int fill_midx_entry(struct multi_pack_index *m, const struct object_id *oid, | |
| 121 | - | struct pack_entry *e, struct packed_git **bad_pack); | |
| 120 | + | /* | |
| 121 | + | * Result of looking an object up in a multi-pack-index. MIDX_FILL_HIT means | |
| 122 | + | * "e was filled in"; the two miss variants distinguish an object the midx does | |
| 123 | + | * not know about (MIDX_FILL_MISS) from one it does know about but whose owning | |
| 124 | + | * pack we can no longer open (MIDX_FILL_OWNER_UNAVAILABLE -- the signature of a | |
| 125 | + | * concurrent repack having removed that pack). A known-bad (corrupt) object | |
| 126 | + | * reports MIDX_FILL_MISS but also sets *bad_pack, if provided, to the owning | |
| 127 | + | * pack so the caller can tell "corrupt" apart from "absent". | |
| 128 | + | */ | |
| 129 | + | enum midx_fill_result { | |
| 130 | + | MIDX_FILL_MISS = 0, | |
| 131 | + | MIDX_FILL_HIT, | |
| 132 | + | MIDX_FILL_OWNER_UNAVAILABLE, | |
| 133 | + | }; | |
| 134 | + | ||
| 135 | + | enum midx_fill_result midx_fill_entry(struct multi_pack_index *m, | |
| 136 | + | const struct object_id *oid, | |
| 137 | + | struct pack_entry *e, | |
| 138 | + | struct packed_git **bad_pack); | |
| 122 | 139 | int midx_contains_pack(struct multi_pack_index *m, | |
| 123 | 140 | const char *idx_or_pack_name); | |
| 124 | 141 | int midx_layer_contains_pack(struct multi_pack_index *m, | |
modified odb/source-packed.c
| @@ -17,13 +17,18 @@ | |||
| 17 | 17 | static int find_pack_entry(struct odb_source_packed *store, | |
| 18 | 18 | const struct object_id *oid, | |
| 19 | 19 | struct pack_entry *e, | |
| 20 | + | enum object_info_flags flags, | |
| 20 | 21 | struct packed_git **bad_pack) | |
| 21 | 22 | { | |
| 22 | 23 | struct packfile_list_entry *l; | |
| 24 | + | enum midx_fill_result midx_result = MIDX_FILL_MISS; | |
| 23 | 25 | ||
| 24 | 26 | odb_source_prepare(&store->base, 0); | |
| 25 | - | if (store->midx && fill_midx_entry(store->midx, oid, e, bad_pack)) | |
| 26 | - | return 1; | |
| 27 | + | if (store->midx) { | |
| 28 | + | midx_result = midx_fill_entry(store->midx, oid, e, bad_pack); | |
| 29 | + | if (midx_result == MIDX_FILL_HIT) | |
| 30 | + | return 1; | |
| 31 | + | } | |
| 27 | 32 | ||
| 28 | 33 | for (l = store->packs.head; l; l = l->next) { | |
| 29 | 34 | struct packed_git *p = l->pack; | |
| @@ -35,6 +40,33 @@ static int find_pack_entry(struct odb_source_packed *store, | |||
| 35 | 40 | } | |
| 36 | 41 | } | |
| 37 | 42 | ||
| 43 | + | /* | |
| 44 | + | * Recovery for a concurrent-repack race: a stale MIDX may still name a | |
| 45 | + | * vanished owning pack even though the object survives in another pack | |
| 46 | + | * the same MIDX covers. The regular fallback above skips MIDX-covered | |
| 47 | + | * packs, and repreparing the on-disk pack set does not reload the | |
| 48 | + | * borrowed, cached MIDX, so scan its packs directly for the survivor. | |
| 49 | + | * | |
| 50 | + | * Do this only on the second read, by which point repreparing packs has | |
| 51 | + | * already had a chance to find an object merely relocated into a new, | |
| 52 | + | * uncovered pack; only a genuine hidden duplicate reaches here. | |
| 53 | + | */ | |
| 54 | + | if (midx_result == MIDX_FILL_OWNER_UNAVAILABLE && | |
| 55 | + | (flags & OBJECT_INFO_SECOND_READ)) { | |
| 56 | + | struct multi_pack_index *m = store->midx; | |
| 57 | + | uint32_t i; | |
| 58 | + | ||
| 59 | + | for (i = 0; i < m->num_packs + m->num_packs_in_base; i++) { | |
| 60 | + | struct packed_git *p; | |
| 61 | + | ||
| 62 | + | if (prepare_midx_pack(m, i)) | |
| 63 | + | continue; | |
| 64 | + | p = nth_midxed_pack(m, i); | |
| 65 | + | if (p && packfile_fill_entry(p, oid, e, bad_pack)) | |
| 66 | + | return 1; | |
| 67 | + | } | |
| 68 | + | } | |
| 69 | + | ||
| 38 | 70 | return 0; | |
| 39 | 71 | } | |
| 40 | 72 | ||
| @@ -57,7 +89,7 @@ static enum odb_read_status odb_source_packed_read_object_info(struct odb_source | |||
| 57 | 89 | if (flags & OBJECT_INFO_SECOND_READ) | |
| 58 | 90 | odb_source_prepare(source, ODB_PREPARE_FLUSH_CACHES); | |
| 59 | 91 | ||
| 60 | - | if (!find_pack_entry(packed, oid, &e, &bad_pack)) { | |
| 92 | + | if (!find_pack_entry(packed, oid, &e, flags, &bad_pack)) { | |
| 61 | 93 | /* | |
| 62 | 94 | * The lookup may have failed because the object is known to be | |
| 63 | 95 | * corrupt in one of the packfiles. Report the object as | |
| @@ -105,7 +137,7 @@ static int odb_source_packed_read_object_stream(struct odb_read_stream **out, | |||
| 105 | 137 | struct odb_source_packed *packed = odb_source_packed_downcast(source); | |
| 106 | 138 | struct pack_entry e; | |
| 107 | 139 | ||
| 108 | - | if (!find_pack_entry(packed, oid, &e, NULL)) | |
| 140 | + | if (!find_pack_entry(packed, oid, &e, 0, NULL)) | |
| 109 | 141 | return -1; | |
| 110 | 142 | ||
| 111 | 143 | return packfile_read_object_stream(out, oid, e.p, e.offset); | |
| @@ -611,7 +643,7 @@ static int odb_source_packed_freshen_object(struct odb_source *source, | |||
| 611 | 643 | timesp = × | |
| 612 | 644 | } | |
| 613 | 645 | ||
| 614 | - | if (!find_pack_entry(packed, oid, &e, NULL)) | |
| 646 | + | if (!find_pack_entry(packed, oid, &e, 0, NULL)) | |
| 615 | 647 | return 0; | |
| 616 | 648 | if (e.p->is_cruft) | |
| 617 | 649 | return 0; | |
modified t/helper/test-read-midx.c
| @@ -82,7 +82,7 @@ static int read_midx_file(const char *object_dir, const char *checksum, | |||
| 82 | 82 | for (i = 0; i < m->num_objects; i++) { | |
| 83 | 83 | nth_midxed_object_oid(&oid, m, | |
| 84 | 84 | i + m->num_objects_in_base); | |
| 85 | - | fill_midx_entry(m, &oid, &e, NULL); | |
| 85 | + | midx_fill_entry(m, &oid, &e, NULL); | |
| 86 | 86 | ||
| 87 | 87 | printf("%s %"PRIu64"\t%s\n", | |
| 88 | 88 | oid_to_hex(&oid), e.offset, e.p->pack_name); | |
modified t/t5319-multi-pack-index.sh
| @@ -1393,4 +1393,44 @@ test_expect_success 'pack.preferBitmapTips interprets patterns as hierarchy' ' | |||
| 1393 | 1393 | ) | |
| 1394 | 1394 | ' | |
| 1395 | 1395 | ||
| 1396 | + | test_expect_success 'lookup recovers object whose midx-owning pack was removed' ' | |
| 1397 | + | test_when_finished "rm -fr repo" && | |
| 1398 | + | git init repo && | |
| 1399 | + | ( | |
| 1400 | + | cd repo && | |
| 1401 | + | ||
| 1402 | + | # "keep" ends up only in the big pack; "dup" is deliberately | |
| 1403 | + | # placed in two packs so the midx has to choose an owner. | |
| 1404 | + | test_commit keep && | |
| 1405 | + | echo duplicated-content >dup && | |
| 1406 | + | git add dup && | |
| 1407 | + | git commit -m dup && | |
| 1408 | + | dup_oid=$(git rev-parse HEAD:dup) && | |
| 1409 | + | ||
| 1410 | + | # Roll every object, including dup, into a single big pack. | |
| 1411 | + | git repack -adq && | |
| 1412 | + | ||
| 1413 | + | # Build a second, "moderate" pack that also contains dup, so dup | |
| 1414 | + | # now lives in two packs that the midx will cover. | |
| 1415 | + | moderate=$(echo "$dup_oid" | | |
| 1416 | + | git pack-objects --quiet $objdir/pack/pack) && | |
| 1417 | + | ||
| 1418 | + | # Attribute dup to the moderate pack in the midx. | |
| 1419 | + | git multi-pack-index write \ | |
| 1420 | + | --preferred-pack="pack-$moderate.idx" && | |
| 1421 | + | ||
| 1422 | + | # Simulate a concurrent "git repack" retiring the moderate pack: | |
| 1423 | + | # its files disappear, but the now-stale midx still names it as | |
| 1424 | + | # the owner of dup. A valid copy of dup survives in the big pack. | |
| 1425 | + | rm -f $objdir/pack/pack-$moderate.* && | |
| 1426 | + | ||
| 1427 | + | # The midx routes the lookup to the deleted pack, and the regular | |
| 1428 | + | # pack fallback skips midx-covered packs, so without recovery dup | |
| 1429 | + | # would appear missing even though it is physically present. | |
| 1430 | + | echo blob >expect && | |
| 1431 | + | git cat-file -t "$dup_oid" >actual && | |
| 1432 | + | test_cmp expect actual | |
| 1433 | + | ) | |
| 1434 | + | ' | |
| 1435 | + | ||
| 1396 | 1436 | test_done | |