Skip to content

Commit 7af3e9b

Browse files
committed
fsck: snapshot default refs before object walk
Fsck has a race when operating on live repositories; consider the following simple script that writes new commits as fsck runs: #!/bin/bash git fsck & PID=$! while ps -p $PID >/dev/null; do sleep 3 git commit -q --allow-empty -m "Another commit" done Since fsck walks objects for connectivity and then reads the refs at the end to check, this can cause fsck to get confused and think that the new refs refer to missing commits and that new reflog entries are invalid. Running the above script in a clone of git.git results in the following (output ellipsized to remove additional errors of the same type): $ ./fsck-while-writing.sh Checking ref database: 100% (1/1), done. Checking object directories: 100% (256/256), done. warning in tag d6602ec: missingTaggerEntry: invalid format - expected 'tagger' line Checking objects: 100% (835091/835091), done. error: HEAD: invalid reflog entry 2aac9f9286e2164fbf8e4f1d1df53044ace2b310 error: HEAD: invalid reflog entry 2aac9f9286e2164fbf8e4f1d1df53044ace2b310 error: HEAD: invalid reflog entry da0f5b80d61844a6f0ad2ddfd57e4fdfa246ea68 error: HEAD: invalid reflog entry da0f5b80d61844a6f0ad2ddfd57e4fdfa246ea68 [...] error: HEAD: invalid reflog entry 87c8a5c2f6b79d9afa9e941590b9a097b6f7ac09 error: HEAD: invalid reflog entry d80887a48865e6ad165274b152cbbbed29f8a55a error: HEAD: invalid reflog entry d80887a48865e6ad165274b152cbbbed29f8a55a error: HEAD: invalid reflog entry 6724f2dfede88bfa9445a333e06e78536c0c6c0d error: refs/heads/mybranch invalid reflog entry 2aac9f9286e2164fbf8e4f1d1df53044ace2b310 error: refs/heads/mybranch: invalid reflog entry 2aac9f9286e2164fbf8e4f1d1df53044ace2b310 error: refs/heads/mybranch: invalid reflog entry da0f5b80d61844a6f0ad2ddfd57e4fdfa246ea68 error: refs/heads/mybranch: invalid reflog entry da0f5b80d61844a6f0ad2ddfd57e4fdfa246ea68 [...] error: refs/heads/mybranch: invalid reflog entry 87c8a5c2f6b79d9afa9e941590b9a097b6f7ac09 error: refs/heads/mybranch: invalid reflog entry d80887a48865e6ad165274b152cbbbed29f8a55a error: refs/heads/mybranch: invalid reflog entry d80887a48865e6ad165274b152cbbbed29f8a55a error: refs/heads/mybranch: invalid reflog entry 6724f2dfede88bfa9445a333e06e78536c0c6c0d Checking connectivity: 833846, done. missing commit 6724f2dfede88bfa9445a333e06e78536c0c6c0d Verifying commits in commit graph: 100% (242243/242243), done. We can minimize the race opportunities by taking a snapshot of refs at program invocation, doing the connectivity check, and then checking the snapshotted refs afterward. This avoids races with regular refs between fsck and adding objects to the database, though it still leaves a race between a gc and fsck. We are less concerned about folks simultaneously running gc with fsck; though, if it becomes an issue, we could lock fsck during gc. We definitely do not want to lock fsck during operations that may add objects to the object store; that would be problematic for forges. Note that refs aren't the only problem, though; reflog entries and index entries could be problematic as well. For now we punt on index entries just leaving a TODO comment, and for reflogs we use a coarse solution of taking the time at the beginning of the program and ignoring reflog entries newer than that time. That may be imperfect if dealing with a network filesystem, so we leave TODO comment for those that want to improve that handling as well. As a high level overview: * In addition to fsck_handle_ref(), which now is only a few lines long to process a ref, there's also a snapshot_ref() which is called early in the program for each ref and takes all the error checking logic. * The iterating over refs that used to be in get_default_heads() plus a loop over the arguments now appears in shapshot_refs(). * There's a new process_refs() as well that kind of looks like the old get_default_heads() though it is streamlined due to the work done by snapshot_refs(). This combination of changes modifies the output of running the script (from the beginning of this commit message) to: $ ./fsck-while-writing.sh Checking ref database: 100% (1/1), done. Checking object directories: 100% (256/256), done. warning in tag d6602ec: missingTaggerEntry: invalid format - expected 'tagger' line Checking objects: 100% (835091/835091), done. Checking connectivity: 833846, done. Verifying commits in commit graph: 100% (242243/242243), done. While worries about live updates while running fsck is likely of most interest for forge operators, it may also benefit those with automated jobs (such as git maintenance) or even casual users who want to do other work in their clone while fsck is running. Originally-based-on-a-patch-by: Matthew John Cheetham <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Elijah Newren <[email protected]>
1 parent b31ab93 commit 7af3e9b

File tree

1 file changed

+122
-40
lines changed

1 file changed

+122
-40
lines changed

builtin/fsck.c

Lines changed: 122 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static int show_progress = -1;
5151
static int show_dangling = 1;
5252
static int name_objects;
5353
static int check_references = 1;
54+
static timestamp_t now;
5455
#define ERROR_OBJECT 01
5556
#define ERROR_REACHABLE 02
5657
#define ERROR_PACK 04
@@ -509,6 +510,9 @@ static int fsck_handle_reflog_ent(const char *refname,
509510
timestamp_t timestamp, int tz UNUSED,
510511
const char *message UNUSED, void *cb_data UNUSED)
511512
{
513+
if (now && timestamp > now)
514+
return 0;
515+
512516
if (verbose)
513517
fprintf_ln(stderr, _("Checking reflog %s->%s"),
514518
oid_to_hex(ooid), oid_to_hex(noid));
@@ -530,8 +534,22 @@ static int fsck_handle_reflog(const char *logname, void *cb_data)
530534
return 0;
531535
}
532536

533-
static int fsck_handle_ref(const struct reference *ref, void *cb_data UNUSED)
537+
struct ref_snapshot {
538+
char *refname;
539+
struct object_id oid;
540+
/* TODO: Maybe supplement with latest reflog entry info too? */
541+
};
542+
543+
struct snapshot {
544+
size_t nr;
545+
size_t alloc;
546+
struct ref_snapshot *ref;
547+
/* TODO: Consider also snapshotting the index of each worktree. */
548+
};
549+
550+
static int snapshot_ref(const struct reference *ref, void *cb_data)
534551
{
552+
struct snapshot *snap = cb_data;
535553
struct object *obj;
536554

537555
obj = parse_object(the_repository, ref->oid);
@@ -555,6 +573,20 @@ static int fsck_handle_ref(const struct reference *ref, void *cb_data UNUSED)
555573
errors_found |= ERROR_REFS;
556574
}
557575
default_refs++;
576+
577+
ALLOC_GROW(snap->ref, snap->nr + 1, snap->alloc);
578+
snap->ref[snap->nr].refname = xstrdup(ref->name);
579+
oidcpy(&snap->ref[snap->nr].oid, ref->oid);
580+
snap->nr++;
581+
582+
return 0;
583+
}
584+
585+
static int fsck_handle_ref(const struct reference *ref, void *cb_data UNUSED)
586+
{
587+
struct object *obj;
588+
589+
obj = parse_object(the_repository, ref->oid);
558590
obj->flags |= USED;
559591
fsck_put_object_name(&fsck_walk_options,
560592
ref->oid, "%s", ref->name);
@@ -567,14 +599,35 @@ static int fsck_head_link(const char *head_ref_name,
567599
const char **head_points_at,
568600
struct object_id *head_oid);
569601

570-
static void get_default_heads(void)
602+
static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
571603
{
572604
struct worktree **worktrees, **p;
573605
const char *head_points_at;
574606
struct object_id head_oid;
575607

608+
for (int i = 0; i < argc; i++) {
609+
const char *arg = argv[i];
610+
struct object_id oid;
611+
if (!repo_get_oid(the_repository, arg, &oid)) {
612+
struct reference ref = {
613+
.name = arg,
614+
.oid = &oid,
615+
};
616+
617+
snapshot_ref(&ref, snap);
618+
continue;
619+
}
620+
error(_("invalid parameter: expected sha1, got '%s'"), arg);
621+
errors_found |= ERROR_OBJECT;
622+
}
623+
624+
if (argc) {
625+
include_reflogs = 0;
626+
return;
627+
}
628+
576629
refs_for_each_rawref(get_main_ref_store(the_repository),
577-
fsck_handle_ref, NULL);
630+
snapshot_ref, snap);
578631

579632
worktrees = get_worktrees();
580633
for (p = worktrees; *p; p++) {
@@ -589,15 +642,52 @@ static void get_default_heads(void)
589642
.oid = &head_oid,
590643
};
591644

592-
fsck_handle_ref(&ref, NULL);
645+
snapshot_ref(&ref, snap);
593646
}
594647
strbuf_release(&refname);
595648

596-
if (include_reflogs)
649+
/*
650+
* TODO: Could use refs_for_each_reflog(...) to find
651+
* latest entry instead of using a global 'now' for that
652+
* purpose.
653+
*/
654+
}
655+
free_worktrees(worktrees);
656+
657+
/* Ignore reflogs newer than now */
658+
now = time(NULL);
659+
}
660+
661+
662+
static void free_snapshot_refs(struct snapshot *snap)
663+
{
664+
for (size_t i = 0; i < snap->nr; i++)
665+
free(snap->ref[i].refname);
666+
free(snap->ref);
667+
}
668+
669+
static void process_refs(struct snapshot *snap)
670+
{
671+
struct worktree **worktrees, **p;
672+
673+
for (size_t i = 0; i < snap->nr; i++) {
674+
struct reference ref = {
675+
.name = snap->ref[i].refname,
676+
.oid = &snap->ref[i].oid,
677+
};
678+
fsck_handle_ref(&ref, NULL);
679+
}
680+
681+
if (include_reflogs) {
682+
worktrees = get_worktrees();
683+
for (p = worktrees; *p; p++) {
684+
struct worktree *wt = *p;
685+
597686
refs_for_each_reflog(get_worktree_ref_store(wt),
598687
fsck_handle_reflog, wt);
688+
}
689+
free_worktrees(worktrees);
599690
}
600-
free_worktrees(worktrees);
601691

602692
/*
603693
* Not having any default heads isn't really fatal, but
@@ -962,8 +1052,12 @@ int cmd_fsck(int argc,
9621052
const char *prefix,
9631053
struct repository *repo UNUSED)
9641054
{
965-
int i;
9661055
struct odb_source *source;
1056+
struct snapshot snap = {
1057+
.nr = 0,
1058+
.alloc = 0,
1059+
.ref = NULL
1060+
};
9671061

9681062
/* fsck knows how to handle missing promisor objects */
9691063
fetch_if_missing = 0;
@@ -999,6 +1093,17 @@ int cmd_fsck(int argc,
9991093
if (check_references)
10001094
fsck_refs(the_repository);
10011095

1096+
/*
1097+
* Take a snapshot of the refs before walking objects to avoid looking
1098+
* at a set of refs that may be changed by the user while we are walking
1099+
* objects. We can still walk over new objects that are added during the
1100+
* execution of fsck but won't miss any objects that were reachable.
1101+
*/
1102+
snapshot_refs(&snap, argc, argv);
1103+
1104+
/* Ensure we get a "fresh" view of the odb */
1105+
odb_reprepare(the_repository->objects);
1106+
10021107
if (connectivity_only) {
10031108
for_each_loose_object(the_repository->objects,
10041109
mark_loose_for_connectivity, NULL, 0);
@@ -1040,42 +1145,18 @@ int cmd_fsck(int argc,
10401145
errors_found |= ERROR_OBJECT;
10411146
}
10421147

1043-
for (i = 0; i < argc; i++) {
1044-
const char *arg = argv[i];
1045-
struct object_id oid;
1046-
if (!repo_get_oid(the_repository, arg, &oid)) {
1047-
struct object *obj = lookup_object(the_repository,
1048-
&oid);
1049-
1050-
if (!obj || !(obj->flags & HAS_OBJ)) {
1051-
if (is_promisor_object(the_repository, &oid))
1052-
continue;
1053-
error(_("%s: object missing"), oid_to_hex(&oid));
1054-
errors_found |= ERROR_OBJECT;
1055-
continue;
1056-
}
1057-
1058-
obj->flags |= USED;
1059-
fsck_put_object_name(&fsck_walk_options, &oid,
1060-
"%s", arg);
1061-
mark_object_reachable(obj);
1062-
continue;
1063-
}
1064-
error(_("invalid parameter: expected sha1, got '%s'"), arg);
1065-
errors_found |= ERROR_OBJECT;
1066-
}
1148+
/* Process the snapshotted refs and the reflogs. */
1149+
process_refs(&snap);
10671150

1068-
/*
1069-
* If we've not been given any explicit head information, do the
1070-
* default ones from .git/refs. We also consider the index file
1071-
* in this case (ie this implies --cache).
1072-
*/
1073-
if (!argc) {
1074-
get_default_heads();
1151+
/* If not given any explicit objects, process index files too. */
1152+
if (!argc)
10751153
keep_cache_objects = 1;
1076-
}
1077-
10781154
if (keep_cache_objects) {
1155+
/*
1156+
* TODO: Consider first walking these indexes in snapshot_refs,
1157+
* to snapshot where the index entries used to point, and then
1158+
* check those snapshotted locations here.
1159+
*/
10791160
struct worktree **worktrees, **p;
10801161

10811162
verify_index_checksum = 1;
@@ -1148,5 +1229,6 @@ int cmd_fsck(int argc,
11481229
}
11491230
}
11501231

1232+
free_snapshot_refs(&snap);
11511233
return errors_found;
11521234
}

0 commit comments

Comments
 (0)