git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>,
	"Han-Wen Nienhuys" <hanwen@google.com>,
	"Michael Haggerty" <mhagger@alum.mit.edu>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH v5 09/13] reflog expire: don't lock reflogs using previously seen OID
Date: Mon, 23 Aug 2021 13:36:10 +0200	[thread overview]
Message-ID: <patch-v5-09.13-aba12606cea-20210823T113115Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-v5-00.13-00000000000-20210823T113115Z-avarab@gmail.com>

During reflog expiry, the cmd_reflog_expire() function first iterates
over all reflogs in logs/*, and then one-by-one acquires the lock for
each one and expires it. This behavior has been with us since this
command was implemented in 4264dc15e1 ("git reflog expire",
2006-12-19).

Change this to stop calling lock_ref_oid_basic() with the OID we saw
when we looped over the logs, instead have it pass the OID it managed
to lock.

This mostly mitigates a race condition where e.g. "git gc" will fail
in a concurrently updated repository because the branch moved since
"git reflog expire --all" was started. I.e. with:

    error: cannot lock ref '<refname>': ref '<refname>' is at <OID-A> but expected <OID-B>

This behavior of passing in an "oid" was needed for an edge-case that
I've untangled in this and preceding commits though, namely that we
needed this OID because we'd:

 1. Lookup the reflog name/OID via dwim_log()
 2. With that OID, lock the reflog
 3. Later in builtin/reflog.c we use the OID we looked as input to
    lookup_commit_reference_gently(), assured that it's equal to the
    OID we got from dwim_log().

We can be sure that this change is safe to make because between
dwim_log (step #1) and lock_ref_oid_basic (step #2) there was no other
logic relevant to the OID or expiry run in the cmd_reflog_expire()
caller.

We can thus treat that code as a black box, before and after this
change it would get an OID that's been locked, the only difference is
that now we mostly won't be failing to get the lock due to the TOCTOU
race[0]. That failure was purely an implementation detail in how the
"current OID" was looked up, it was divorced from the locking
mechanism.

What do we mean with "mostly"? It mostly mitigates it because we'll
still run into cases where the ref is locked and being updated as we
want to expire it, and other git processes wanting to update the refs
will in turn race with us as we expire the reflog.

That remaining race can in turn be mitigated with the
core.filesRefLockTimeout setting, see 4ff0f01cb7 ("refs: retry
acquiring reference locks for 100ms", 2017-08-21). In practice if that
value is high enough we'll probably never have ref updates or reflog
expiry failing, since the clients involved will retry for far longer
than the time any of those operations could take.

See [1] for an initial report of how this impacted "git gc" and a
large discussion about this change in early 2019. In particular patch
looked good to Michael Haggerty, see his[2]. That message seems to not
have made it to the ML archive, its content is quoted in full in my
[3].

I'm leaving behind now-unused code the refs API etc. that takes the
now-NULL "unused_oid" argument, and other code that can be simplified now
that we never have on OID in that context, that'll be cleaned up in
subsequent commits, but for now let's narrowly focus on fixing the
"git gc" issue. As the modified assert() shows we always pass a NULL
oid to reflog_expire() now.

Unfortunately this sort of probabilistic contention is hard to turn
into a test. I've tested this by running the following three subshells
in concurrent terminals:

    (
        rm -rf /tmp/git &&
        git init /tmp/git &&
        while true
        do
            head -c 10 /dev/urandom | hexdump >/tmp/git/out &&
            git -C /tmp/git add out &&
            git -C /tmp/git commit -m"out"
        done
    )

    (
	rm -rf /tmp/git-clone &&
        git clone file:///tmp/git /tmp/git-clone &&
        while git -C /tmp/git-clone pull
        do
            date
        done
    )

    (
        while git -C /tmp/git-clone reflog expire --all
        do
            date
        done
    )

Before this change the "reflog expire" would fail really quickly with
the "but expected" error noted above.

After this change both the "pull" and "reflog expire" will run for a
while, but eventually fail because I get unlucky with
core.filesRefLockTimeout (the "reflog expire" is in a really tight
loop). As noted above that can in turn be mitigated with higher values
of core.filesRefLockTimeout than the 100ms default.

As noted in the commentary added in the preceding commit there's also
the case of branches being racily deleted, that can be tested by
adding this to the above:

    (
        while git -C /tmp/git-clone branch topic master &&
	      git -C /tmp/git-clone branch -D topic
        do
            date
        done
    )

With core.filesRefLockTimeout set to 10 seconds (it can probably be a
lot lower) I managed to run all four of these concurrently for about
an hour, and accumulated ~125k commits, auto-gc's and all, and didn't
have a single failure. The loops visibly stall while waiting for the
lock, but that's expected and desired behavior.

0. https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use
1. https://lore.kernel.org/git/87tvg7brlm.fsf@evledraar.gmail.com/
2. http://lore.kernel.org/git/b870a17d-2103-41b8-3cbc-7389d5fff33a@alum.mit.edu
3. https://lore.kernel.org/git/87pnqkco8v.fsf@evledraar.gmail.com/

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/reflog.c     | 13 ++++++-------
 refs.h               |  2 +-
 refs/files-backend.c |  7 +++++--
 3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 09541d1c804..61795f22d53 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -629,8 +629,9 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 		free_worktrees(worktrees);
 		for (i = 0; i < collected.nr; i++) {
 			struct collected_reflog *e = collected.e[i];
+
 			set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
-			status |= reflog_expire(e->reflog, &e->oid, flags,
+			status |= reflog_expire(e->reflog, NULL, flags,
 						reflog_expiry_prepare,
 						should_expire_reflog_ent,
 						reflog_expiry_cleanup,
@@ -642,13 +643,12 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 
 	for (; i < argc; i++) {
 		char *ref;
-		struct object_id oid;
-		if (!dwim_log(argv[i], strlen(argv[i]), &oid, &ref)) {
+		if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
 			status |= error(_("%s points nowhere!"), argv[i]);
 			continue;
 		}
 		set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
-		status |= reflog_expire(ref, &oid, flags,
+		status |= reflog_expire(ref, NULL, flags,
 					reflog_expiry_prepare,
 					should_expire_reflog_ent,
 					reflog_expiry_cleanup,
@@ -700,7 +700,6 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 
 	for ( ; i < argc; i++) {
 		const char *spec = strstr(argv[i], "@{");
-		struct object_id oid;
 		char *ep, *ref;
 		int recno;
 
@@ -709,7 +708,7 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 			continue;
 		}
 
-		if (!dwim_log(argv[i], spec - argv[i], &oid, &ref)) {
+		if (!dwim_log(argv[i], spec - argv[i], NULL, &ref)) {
 			status |= error(_("no reflog for '%s'"), argv[i]);
 			continue;
 		}
@@ -724,7 +723,7 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 			cb.cmd.expire_total = 0;
 		}
 
-		status |= reflog_expire(ref, &oid, flags,
+		status |= reflog_expire(ref, NULL, flags,
 					reflog_expiry_prepare,
 					should_expire_reflog_ent,
 					reflog_expiry_cleanup,
diff --git a/refs.h b/refs.h
index 48970dfc7e0..ddbf15f1c21 100644
--- a/refs.h
+++ b/refs.h
@@ -796,7 +796,7 @@ enum expire_reflog_flags {
  * expiration policy that is desired.
  *
  * reflog_expiry_prepare_fn -- Called once after the reference is
- *     locked.
+ *     locked. Called with the OID of the locked reference.
  *
  * reflog_expiry_should_prune_fn -- Called once for each entry in the
  *     existing reflog. It should return true iff that entry should be
diff --git a/refs/files-backend.c b/refs/files-backend.c
index fbcd0c790bc..d81bda8bc25 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3027,7 +3027,7 @@ static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
 }
 
 static int files_reflog_expire(struct ref_store *ref_store,
-			       const char *refname, const struct object_id *oid,
+			       const char *refname, const struct object_id *unused_oid,
 			       unsigned int flags,
 			       reflog_expiry_prepare_fn prepare_fn,
 			       reflog_expiry_should_prune_fn should_prune_fn,
@@ -3044,6 +3044,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
 	int status = 0;
 	int type;
 	struct strbuf err = STRBUF_INIT;
+	const struct object_id *oid;
 
 	memset(&cb, 0, sizeof(cb));
 	cb.flags = flags;
@@ -3055,12 +3056,13 @@ static int files_reflog_expire(struct ref_store *ref_store,
 	 * reference itself, plus we might need to update the
 	 * reference if --updateref was specified:
 	 */
-	lock = lock_ref_oid_basic(refs, refname, oid, &type, &err);
+	lock = lock_ref_oid_basic(refs, refname, NULL, &type, &err);
 	if (!lock) {
 		error("cannot lock ref '%s': %s", refname, err.buf);
 		strbuf_release(&err);
 		return -1;
 	}
+	oid = &lock->old_oid;
 
 	/*
 	 * When refs are deleted, their reflog is deleted before the
@@ -3104,6 +3106,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
 		}
 	}
 
+	assert(!unused_oid);
 	(*prepare_fn)(refname, oid, cb.policy_cb);
 	refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
 	(*cleanup_fn)(cb.policy_cb);
-- 
2.33.0.662.g438caf9576d


  parent reply	other threads:[~2021-08-23 11:36 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-14 11:17 [PATCH] refs file backend: remove dead "errno == EISDIR" code Ævar Arnfjörð Bjarmason
2021-07-14 16:21 ` Jeff King
2021-07-14 19:07   ` Ævar Arnfjörð Bjarmason
2021-07-14 23:15     ` Jeff King
2021-07-15  0:02       ` Ævar Arnfjörð Bjarmason
2021-07-15  5:16         ` Jeff King
2021-07-17  1:28           ` Junio C Hamano
2021-07-17  2:33             ` Jeff King
2021-07-19 15:42               ` Junio C Hamano
2021-07-19 16:59                 ` Junio C Hamano
2021-07-17 21:36             ` Ævar Arnfjörð Bjarmason
2021-07-16 14:12 ` [PATCH v2 00/11] fix "git reflog expire" race & get rid of EISDIR in refs API Ævar Arnfjörð Bjarmason
2021-07-16 14:12   ` [PATCH v2 01/11] refs/packet: add missing BUG() invocations to reflog callbacks Ævar Arnfjörð Bjarmason
2021-07-16 14:12   ` [PATCH v2 02/11] refs/files: remove unused REF_DELETING in lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-07-17  2:03     ` Jeff King
2021-07-19 16:16     ` Junio C Hamano
2021-07-20  7:19       ` Jeff King
2021-07-16 14:12   ` [PATCH v2 03/11] refs/files: remove unused "extras/skip" " Ævar Arnfjörð Bjarmason
2021-07-16 14:13   ` [PATCH v2 04/11] refs/files: remove unused "skip" in lock_raw_ref() too Ævar Arnfjörð Bjarmason
2021-07-16 14:13   ` [PATCH v2 05/11] refs/debug: re-indent argument list for "prepare" Ævar Arnfjörð Bjarmason
2021-07-16 14:13   ` [PATCH v2 06/11] refs API: pass the "lock OID" to reflog "prepare" Ævar Arnfjörð Bjarmason
2021-07-17  2:04     ` Jeff King
2021-07-19 16:30     ` Junio C Hamano
2021-07-19 19:21       ` Ævar Arnfjörð Bjarmason
2021-07-16 14:13   ` [PATCH v2 07/11] refs: make repo_dwim_log() accept a NULL oid Ævar Arnfjörð Bjarmason
2021-07-16 14:13   ` [PATCH v2 08/11] refs/files: add a comment about refs_reflog_exists() call Ævar Arnfjörð Bjarmason
2021-07-17  2:08     ` Jeff King
2021-07-19 16:43       ` Junio C Hamano
2021-07-20  7:22         ` Jeff King
2021-07-16 14:13   ` [PATCH v2 09/11] reflog expire: don't lock reflogs using previously seen OID Ævar Arnfjörð Bjarmason
2021-07-17  2:23     ` Jeff King
2021-08-17 13:35     ` Han-Wen Nienhuys
2021-08-18 21:05       ` Junio C Hamano
2021-08-19 10:06         ` Carlo Marcelo Arenas Belón
2021-08-20  2:30           ` Junio C Hamano
2021-07-16 14:13   ` [PATCH v2 10/11] refs/files: remove unused "oid" in lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-07-17  2:26     ` Jeff King
2021-07-16 14:13   ` [PATCH v2 11/11] refs/files: remove unused "errno == EISDIR" code Ævar Arnfjörð Bjarmason
2021-07-17  2:30     ` Jeff King
2021-07-17  2:34   ` [PATCH v2 00/11] fix "git reflog expire" race & get rid of EISDIR in refs API Jeff King
2021-07-20 10:24   ` [PATCH v3 00/12] " Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 01/12] refs/packet: add missing BUG() invocations to reflog callbacks Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 02/12] refs/files: remove unused REF_DELETING in lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-08-02 17:17       ` Junio C Hamano
2021-07-20 10:24     ` [PATCH v3 03/12] refs/files: remove unused "extras/skip" " Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 04/12] refs/files: remove unused "skip" in lock_raw_ref() too Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 05/12] refs/debug: re-indent argument list for "prepare" Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 06/12] refs API: pass the "lock OID" to reflog "prepare" Ævar Arnfjörð Bjarmason
2021-07-21 17:40       ` Junio C Hamano
2021-07-21 17:47         ` Junio C Hamano
     [not found]           ` <CAFQ2z_PuNJ_KtS_O9R2s0jdGbNNKnKdS3=_-nEu6367pteCxwA@mail.gmail.com>
2021-07-23 19:41             ` Ævar Arnfjörð Bjarmason
2021-07-23 20:49               ` Junio C Hamano
2021-07-26  5:39                 ` Ævar Arnfjörð Bjarmason
2021-07-26 17:47                   ` Junio C Hamano
2021-07-20 10:24     ` [PATCH v3 07/12] refs: make repo_dwim_log() accept a NULL oid Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 08/12] refs/files: add a comment about refs_reflog_exists() call Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 09/12] reflog expire: don't lock reflogs using previously seen OID Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 10/12] refs/files: remove unused "oid" in lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 11/12] refs/files: remove unused "errno == EISDIR" code Ævar Arnfjörð Bjarmason
2021-07-20 10:24     ` [PATCH v3 12/12] refs/files: remove unused "errno != ENOTDIR" condition Ævar Arnfjörð Bjarmason
2021-07-26 23:44     ` [PATCH v4 00/11] fix "git reflog expire" race & get rid of EISDIR in refs API Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 01/11] refs/packet: add missing BUG() invocations to reflog callbacks Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 02/11] refs/files: remove unused REF_DELETING in lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 03/11] refs/files: remove unused "extras/skip" " Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 04/11] refs/files: remove unused "skip" in lock_raw_ref() too Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 05/11] refs/debug: re-indent argument list for "prepare" Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 06/11] refs: make repo_dwim_log() accept a NULL oid Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 07/11] refs/files: add a comment about refs_reflog_exists() call Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 08/11] reflog expire: don't lock reflogs using previously seen OID Ævar Arnfjörð Bjarmason
2021-08-02 17:26         ` Junio C Hamano
2021-08-04  9:56           ` Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 09/11] refs/files: remove unused "oid" in lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 10/11] refs/files: remove unused "errno == EISDIR" code Ævar Arnfjörð Bjarmason
2021-07-26 23:44       ` [PATCH v4 11/11] refs/files: remove unused "errno != ENOTDIR" condition Ævar Arnfjörð Bjarmason
2021-08-23 11:36       ` [PATCH v5 00/13] fix "git reflog expire" race & get rid of EISDIR in refs API Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 01/13] refs/packet: add missing BUG() invocations to reflog callbacks Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 02/13] refs/files: remove unused REF_DELETING in lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 03/13] refs: drop unused "flags" parameter to lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 04/13] refs/files: remove unused "extras/skip" in lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 05/13] refs/files: remove unused "skip" in lock_raw_ref() too Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 06/13] refs/debug: re-indent argument list for "prepare" Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 07/13] refs: make repo_dwim_log() accept a NULL oid Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 08/13] refs/files: add a comment about refs_reflog_exists() call Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` Ævar Arnfjörð Bjarmason [this message]
2021-08-23 11:36         ` [PATCH v5 10/13] refs API: remove OID argument to reflog_expire() Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 11/13] refs/files: remove unused "oid" in lock_ref_oid_basic() Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 12/13] refs/files: remove unused "errno == EISDIR" code Ævar Arnfjörð Bjarmason
2021-08-23 11:36         ` [PATCH v5 13/13] refs/files: remove unused "errno != ENOTDIR" condition Ævar Arnfjörð Bjarmason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=patch-v5-09.13-aba12606cea-20210823T113115Z-avarab@gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=hanwen@google.com \
    --cc=mhagger@alum.mit.edu \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).