git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 7/9] refs/files: detect race when generating reflog entry for HEAD

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 4, 2025, 07:38 UTC
Message-ID
<aJBjdWv9qUeBL25-@pks.im>
In-Reply-To
<20250802111128.GC1180347@coredump.intra.peff.net>
On Sat, Aug 02, 2025 at 07:11:28AM -0400, Jeff King wrote:
Show 16 quoted lines
> On Tue, Jul 29, 2025 at 10:55:25AM +0200, Patrick Steinhardt wrote:
> 
> > Unfortunately, this change only helps with the second race. We cannot
> > reliably plug the first race without locking the HEAD reference at the
> > start of the transaction. Locking HEAD unconditionally would effectively
> > serialize all writes though, and that doesn't seem like an option. Also,
> > double checking its value at the end of the transaction is not an option
> > either, as its target may have flip-flopped during the transaction.
> 
> I agree we should not always take a lock on HEAD, since most refs would
> not need it. But I wonder if we could do better by examining HEAD, then
> taking a lock when we think we'll need it, and then re-checking the
> value of HEAD. That is still racy, though (somebody could have pointed
> HEAD at us between the two checks). Fundamentally the files backend is
> not atomic across the whole namespace, and we are trying to update two
> refs. So I think there will always be some race.

We do that though. When queueing the log-only update for HEAD we don't lock immediately, but we lock once we process that log-only update. And that's where we now do the check whether HEAD has changed meanwhile, which should be race-free given that it did change indeed.

Show 7 quoted lines
> It does make me wonder if this race-fix is even worth it, then. We are
> catching the case where somebody moves HEAD away from the ref we are
> updating while we are updating it. But without atomicity, do we even
> know which happened first? That is, would it be incorrect to update
> HEAD anyway? I guess the outcome is observable because their movement of
> HEAD generated a reflog entry, and thus the entries would be out of
> order. So maybe that is worth it.

So with the above I think that we are race-free for the case where HEAD has been modified after we have decided to write a reflog entry for it. We aren't though in the case where we _haven't_ decided to write a reflog entry, as HEAD might have been adjusted to point to one of the updated refs meanwhile as you rightfully point out.

But I think that's an acceptable tradeoff. I'd rather write no reflog entry than an incorrect one.

Show 55 quoted lines
> Anyway, I had two questions about the code:
> 
> > @@ -2600,7 +2607,36 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re
> >  
> >  	update->backend_data = lock;
> >  
> > -	if (update->type & REF_ISSYMREF) {
> > +	if (update->flags & REF_LOG_VIA_SPLIT) {
> > +		struct ref_lock *parent_lock;
> > +
> > +		if (!update->parent_update)
> > +			BUG("split update without a parent");
> > +
> > +		parent_lock = update->parent_update->backend_data;
> > +
> > +		/*
> > +		 * Check that "HEAD" didn't racily change since we have looked
> > +		 * it up. If it did we must refuse to write the reflog entry.
> > +		 *
> > +		 * Note that this does not catch all races: if "HEAD" was
> > +		 * racily changed to point to one of the refs part of the
> > +		 * transaction then we would miss writing the split reflog
> > +		 * entry for "HEAD".
> > +		 */
> > +		if (!(update->type & REF_ISSYMREF) ||
> > +		    strcmp(update->parent_update->refname, referent.buf)) {
> > +			strbuf_addstr(err, "HEAD has been racily updated");
> > +			ret = REF_TRANSACTION_ERROR_GENERIC;
> > +			goto out;
> > +		}
> 
> One, what happens with a multi-level ref (e.g., HEAD points to
> refs/heads/foo which points to refs/heads/bar)?
> 
> We've resolved HEAD to get referent.buf. Do we get "foo" or "bar" here?
> If "bar", then a write through "foo" will complain. But if we get "foo",
> then theoretically a write through "bar" will complain.
> 
> I _think_ we are OK, though. Constructing it like this:
> 
>   git init
>   git commit --allow-empty -m whatever
> 
>   git symbolic-ref refs/heads/foo refs/heads/bar
>   git symbolic-ref HEAD refs/heads/foo
>   git update-ref refs/heads/foo main
> 
> triggers the check and shows that our referent from lock_raw_ref() is
> the first level (i.e., "foo"). Which is good.
> 
> If we swap out "foo" for "bar" in the update-ref call, then we'd get a
> mismatch. But in that case we do not figure out that HEAD needs be
> written at all! That is, we only do a single level of look-back to
> decide whether to write HEAD at all. So as long as we keep doing so, we
> are OK.

Yeah, the whole code is best-effort only and doesn't work for all kinds of edge cases. The mere fact that we only handle this case for HEAD is already a limitation -- in theory we should do it for all symbolic refs.

Show 16 quoted lines
> > +		if (!(update->type & REF_ISSYMREF) ||
> > +		    strcmp(update->parent_update->refname, referent.buf)) {
> > +			strbuf_addstr(err, "HEAD has been racily updated");
> > +			ret = REF_TRANSACTION_ERROR_GENERIC;
> > +			goto out;
> > +		}
> 
> And two, is an error the right thing here? The user asked us to update
> "foo", and we saw that HEAD pointed to it. So we decided to update
> HEAD's reflog, too. And when it came time to do so under lock, we found
> that HEAD did not point to "foo" any more.
> 
> Shouldn't we quietly drop the HEAD reflog update, rather than forcing
> the whole transaction to fail? The user never asked us to update HEAD at
> all. It was something we opportunistically decided to do, and now we
> find out that it is not appropriate to do so.

That's something I wasn't quite sure about, either. Honestly, the reason I shied away from it is that it needs a bit more munging for an edge case that is hard to test reliably. But I guess we can do something like the below patch to skip writing the reflog message instea.

Patrick
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 905555365b..851b1b33f4 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -643,14 +643,16 @@ int parse_loose_ref_contents(const struct git_hash_algo *algop,
 	return 0;
 }
 
-static void unlock_ref(struct ref_lock *lock)
+static int unlock_ref(struct ref_lock *lock)
 {
 	lock->count--;
 	if (!lock->count) {
 		rollback_lock_file(&lock->lk);
 		free(lock->ref_name);
 		free(lock);
+		return 1;
 	}
+	return 0;
 }
 
 /*
@@ -2557,6 +2559,9 @@ struct files_transaction_backend_data {
  *   the referent to transaction.
  * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
  *   update of HEAD.
+ *
+ * Returns 0 on success, 1 in case the update needs to be dropped or a negative
+ * error code otherwise.
  */
 static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *refs,
 						      struct ref_update *update,
@@ -2617,7 +2622,8 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re
 
 		/*
 		 * Check that "HEAD" didn't racily change since we have looked
-		 * it up. If it did we must refuse to write the reflog entry.
+		 * it up. If it did we remove the reflog-only updateg from the
+		 * transaction again.
 		 *
 		 * Note that this does not catch all races: if "HEAD" was
 		 * racily changed to point to one of the refs part of the
@@ -2626,8 +2632,16 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re
 		 */
 		if (!(update->type & REF_ISSYMREF) ||
 		    strcmp(update->parent_update->refname, referent.buf)) {
-			strbuf_addstr(err, "HEAD has been racily updated");
-			ret = REF_TRANSACTION_ERROR_GENERIC;
+			if (unlock_ref(lock))
+				strmap_remove(&backend_data->ref_locks,
+					      update->refname, 0);
+
+			memmove(transaction->updates + update_idx,
+				transaction->updates + update_idx + 1,
+				(transaction->nr - update_idx - 1) * sizeof(*transaction->updates));
+			transaction->nr--;
+
+			ret = 1;
 			goto out;
 		}
 
@@ -2896,6 +2910,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,
 					  head_ref, &refnames_to_check,
 					  err);
 		if (ret) {
+			if (ret > 0)
+				continue;
 			if (ref_transaction_maybe_set_rejected(transaction, i, ret)) {
 				strbuf_reset(err);
 				ret = 0;
Previous: Jeff KingNext: Jeff King
Message 75 of 114 in “refs: fix migration of reflog entries”
  1. 0/8 refs: fix migration of reflog entriesPatrick Steinhardt, Jul 22, 2025
  2. 1/8 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Jul 22, 2025
  3. Junio C HamanoJul 22, 2025
  4. 2/8 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Jul 22, 2025
  5. Justin ToblerJul 23, 2025
  6. Patrick SteinhardtJul 24, 2025
  7. Junio C HamanoJul 24, 2025
  8. 3/8 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Jul 22, 2025
  9. Justin ToblerJul 23, 2025
  10. Karthik NayakJul 24, 2025
  11. Toon ClaesJul 24, 2025
  12. 4/8 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Jul 22, 2025
  13. Justin ToblerJul 23, 2025
  14. Patrick SteinhardtJul 24, 2025
  15. Toon ClaesJul 24, 2025
  16. Patrick SteinhardtJul 25, 2025
  17. SZEDER GáborJul 24, 2025
  18. Junio C HamanoJul 24, 2025
  19. Patrick SteinhardtJul 25, 2025
  20. Junio C HamanoJul 25, 2025
  21. 5/8 ident: fix type of string length parameterPatrick Steinhardt, Jul 22, 2025
  22. 6/8 refs: fix identity for migrated reflogsPatrick Steinhardt, Jul 22, 2025
  23. Justin ToblerJul 23, 2025
  24. Patrick SteinhardtJul 24, 2025
  25. Karthik NayakJul 24, 2025
  26. Toon ClaesJul 24, 2025
  27. 7/8 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Jul 22, 2025
  28. Justin ToblerJul 23, 2025
  29. Patrick SteinhardtJul 24, 2025
  30. Karthik NayakJul 24, 2025
  31. Patrick SteinhardtJul 24, 2025
  32. 8/8 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Jul 22, 2025
  33. Junio C HamanoJul 22, 2025
  34. Patrick SteinhardtJul 23, 2025
  35. 0/8 refs: fix migration of reflog entriesPatrick Steinhardt, Jul 25, 2025
  36. 1/8 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Jul 25, 2025
  37. 2/8 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Jul 25, 2025
  38. 3/8 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Jul 25, 2025
  39. 4/8 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Jul 25, 2025
  40. Kristoffer HaugsbakkJul 28, 2025
  41. Junio C HamanoJul 28, 2025
  42. Karthik NayakJul 28, 2025
  43. Junio C HamanoJul 28, 2025
  44. Karthik NayakJul 30, 2025
  45. Ben KnobleJul 29, 2025
  46. Kristoffer HaugsbakkJul 29, 2025
  47. Patrick SteinhardtJul 29, 2025
  48. Junio C HamanoJul 29, 2025
  49. Patrick SteinhardtJul 30, 2025
  50. Patrick SteinhardtJul 30, 2025
  51. 5/8 ident: fix type of string length parameterPatrick Steinhardt, Jul 25, 2025
  52. 6/8 refs: fix identity for migrated reflogsPatrick Steinhardt, Jul 25, 2025
  53. 7/8 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Jul 25, 2025
  54. Jeff KingJul 25, 2025
  55. Patrick SteinhardtJul 28, 2025
  56. Jeff KingJul 29, 2025
  57. Patrick SteinhardtJul 29, 2025
  58. 8/8 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Jul 25, 2025
  59. 0/9 refs: fix migration of reflog entriesPatrick Steinhardt, Jul 29, 2025
  60. 1/9 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Jul 29, 2025
  61. 2/9 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Jul 29, 2025
  62. 3/9 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Jul 29, 2025
  63. Toon ClaesAug 1, 2025
  64. Patrick SteinhardtAug 4, 2025
  65. 4/9 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Jul 29, 2025
  66. Junio C HamanoJul 29, 2025
  67. Toon ClaesAug 1, 2025
  68. Patrick SteinhardtAug 4, 2025
  69. 5/9 ident: fix type of string length parameterPatrick Steinhardt, Jul 29, 2025
  70. 6/9 refs: fix identity for migrated reflogsPatrick Steinhardt, Jul 29, 2025
  71. 7/9 refs/files: detect race when generating reflog entry for HEADPatrick Steinhardt, Jul 29, 2025
  72. Junio C HamanoJul 29, 2025
  73. Toon ClaesAug 1, 2025
  74. Jeff KingAug 2, 2025
  75. Patrick SteinhardtAug 4, 2025
  76. Jeff KingAug 4, 2025
  77. 8/9 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Jul 29, 2025
  78. 9/9 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Jul 29, 2025
  79. 0/9 refs: fix migration of reflog entriesPatrick Steinhardt, Aug 4, 2025
  80. 1/9 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Aug 4, 2025
  81. 2/9 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Aug 4, 2025
  82. 3/9 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Aug 4, 2025
  83. 4/9 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Aug 4, 2025
  84. 5/9 ident: fix type of string length parameterPatrick Steinhardt, Aug 4, 2025
  85. 6/9 refs: fix identity for migrated reflogsPatrick Steinhardt, Aug 4, 2025
  86. 7/9 refs/files: detect race when generating reflog entry for HEADPatrick Steinhardt, Aug 4, 2025
  87. Jeff KingAug 4, 2025
  88. 8/9 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Aug 4, 2025
  89. 9/9 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Aug 4, 2025
  90. 0/9 refs: fix migration of reflog entriesPatrick Steinhardt, Aug 5, 2025
  91. 1/9 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Aug 5, 2025
  92. Jean-Noël AVILAAug 5, 2025
  93. Junio C HamanoAug 5, 2025
  94. Patrick SteinhardtAug 6, 2025
  95. 2/9 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Aug 5, 2025
  96. 3/9 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Aug 5, 2025
  97. 4/9 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Aug 5, 2025
  98. 5/9 ident: fix type of string length parameterPatrick Steinhardt, Aug 5, 2025
  99. 6/9 refs: fix identity for migrated reflogsPatrick Steinhardt, Aug 5, 2025
  100. 7/9 refs/files: detect race when generating reflog entry for HEADPatrick Steinhardt, Aug 5, 2025
  101. 8/9 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Aug 5, 2025
  102. 9/9 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Aug 5, 2025
  103. Jeff KingAug 5, 2025
  104. Patrick SteinhardtAug 6, 2025
  105. 0/9 refs: fix migration of reflog entriesPatrick Steinhardt, Aug 6, 2025
  106. 1/9 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Aug 6, 2025
  107. 2/9 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Aug 6, 2025
  108. 3/9 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Aug 6, 2025
  109. 4/9 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Aug 6, 2025
  110. 5/9 ident: fix type of string length parameterPatrick Steinhardt, Aug 6, 2025
  111. 6/9 refs: fix identity for migrated reflogsPatrick Steinhardt, Aug 6, 2025
  112. 7/9 refs/files: detect race when generating reflog entry for HEADPatrick Steinhardt, Aug 6, 2025
  113. 8/9 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Aug 6, 2025
  114. 9/9 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Aug 6, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.