Skip to contentSkip to conversation

refs/files: avoid packed-refs lock for root ref deletion

PATCH Superseded Ariel Keselman 2 participants 5 messages Sep 10 – Sep 12

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 refs/files: avoid packed-refs lock for root ref deletion

Hi,

Deleting root refs in the files backend unnecessarily locks packed-refs, even though root refs cannot be packed. This can cause post-commit cleanup to report an error after a successful commit in a linked worktree with read-only shared metadata.

The attached patch skips that lock for root-ref deletion and adds regression tests. All seven new tests fail without the fix and pass with it; broader ref, worktree, and sequencer tests also pass.

AI assistance was used to generate the patch, tests, and commit message.

Thanks,
Ariel

From c5d12e97a78123965590553fddc0dd78af3e006e Mon Sep 17 00:00:00 2001
From: Ariel Keselman <[email protected]>
Date: Wed, 9 Sep 2026 22:16:50 -0700
To: [email protected]
Subject: [PATCH] refs/files: avoid packed-refs lock for root ref deletion

Deleting a root ref queues a packed-ref transaction in the files backend, even though root refs cannot be packed. For example, holding .git/packed-refs.lock makes "git update-ref --no-deref -d AUTO_MERGE" fail, whether or not AUTO_MERGE exists.

This also affects post-commit cleanup, which deletes AUTO_MERGE after updating HEAD. In a linked worktree with read-only shared metadata, commit succeeds but cleanup reports a packed-refs.lock error. Deleting CHERRY_PICK_HEAD and REVERT_HEAD is affected as well.

Skip the packed transaction for root-ref deletions. Keep loose-ref locking and packed-ref deletion for other refs unchanged.

Test existing and absent root refs with packed-refs.lock held. Also check that root-ref deletion leaves packed refs intact, and that a packed branch still requires the lock and can be deleted once it is released.

---
AI assistance was used to generate the patch, tests, and commit message.

Based on maint at e9019fcafe (Git 2.55).

Validation:
- All seven new tests fail with packed-refs.lock errors without the fix.
- With the fix, t0600 and t0601 pass (one platform skip in t0600).
- Broader ref, worktree, rebase, cherry-pick, commit and merge tests pass:
123 scripts, 4083 tests on the maint-based tree.
- After merging with master at b8242b093d, 128 scripts / 4178 tests and 260 unit tests pass.

 refs/files-backend.c        |  9 +++++---
 t/t0600-reffiles-backend.sh | 46 +++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 3 deletions(-)
refs/files-backend.c +6 -3
index a4c7858787..41887f180f 100644
@@ -2981,10 +2981,13 @@ static int files_transaction_prepare(struct ref_store *ref_store,
 
 		if (update->flags & REF_DELETING &&
 		    !(update->flags & REF_LOG_ONLY) &&
-		    !(update->flags & REF_IS_PRUNING)) {
+		    !(update->flags & REF_IS_PRUNING) &&
+		    !is_root_ref(update->refname)) {
 			/*
-			 * This reference has to be deleted from
-			 * packed-refs if it exists there.
+			 * Root refs cannot be packed. Do not acquire the shared
+			 * packed-refs lock when deleting a per-worktree root ref.
+			 * Other references have to be deleted from
+			 * packed-refs if they exist there.
 			 */
 			if (!packed_transaction) {
 				packed_transaction = ref_store_transaction_begin(
t/t0600-reffiles-backend.sh +46 -0

50 lines not on this page · raw message

Signature · 1 line
2.55.0
#1 lore raw

Hi,

Quoted 7 lines · Ariel Keselman

Hi,

Deleting root refs in the files backend unnecessarily locks packed-refs, even though root refs cannot be packed. This can cause post-commit cleanup to report an error after a successful commit in a linked worktree with read-only shared metadata.

Quoted 5 lines

The attached patch skips that lock for root-ref deletion and adds regression tests. All seven new tests fail without the fix and pass with it; broader ref, worktree, and sequencer tests also pass.

AI assistance was used to generate the patch, tests, and commit message.

Please consult Documentation/SubmittingPatches. The expectation is that patches will not be sent as attachments. I'd recommend using a tool like b4 to send your patches, which handles a lot of the nuisances for you.

Quoted 11 lines

From c5d12e97a78123965590553fddc0dd78af3e006e Mon Sep 17 00:00:00 2001
From: Ariel Keselman <[email protected]>
Date: Wed, 9 Sep 2026 22:16:50 -0700
To: [email protected]
Subject: [PATCH] refs/files: avoid packed-refs lock for root ref deletion

Deleting a root ref queues a packed-ref transaction in the files backend, even though root refs cannot be packed. For example, holding .git/packed-refs.lock makes "git update-ref --no-deref -d AUTO_MERGE" fail, whether or not AUTO_MERGE exists.

Quoted 7 lines

This also affects post-commit cleanup, which deletes AUTO_MERGE after updating HEAD. In a linked worktree with read-only shared metadata, commit succeeds but cleanup reports a packed-refs.lock error. Deleting CHERRY_PICK_HEAD and REVERT_HEAD is affected as well.

Skip the packed transaction for root-ref deletions. Keep loose-ref locking and packed-ref deletion for other refs unchanged.

Makes sense indeed. Root refs are never packed, and consequently it does not make any sense for us to try to evict them from packed-refs, either.

Quoted 9 lines
refs/files-backend.c +0 -1
index a4c7858787..41887f180f 100644
@@ -2981,10 +2981,13 @@ static int files_transaction_prepare(struct ref_store *ref_store,
 
 		if (update->flags & REF_DELETING &&
 		    !(update->flags & REF_LOG_ONLY) &&
-		    !(update->flags & REF_IS_PRUNING)) {

refs/files-backend.c static int files_transaction_prepare(struct ref_store *ref_store, · line 2991

Quoted 10 lines
refs/files-backend.c +6 -2
+		    !(update->flags & REF_IS_PRUNING) &&
+		    !is_root_ref(update->refname)) {
 			/*
-			 * This reference has to be deleted from
-			 * packed-refs if it exists there.
+			 * Root refs cannot be packed. Do not acquire the shared
+			 * packed-refs lock when deleting a per-worktree root ref.
+			 * Other references have to be deleted from
+			 * packed-refs if they exist there.
 			 */

Nit: I feel like this comment is a bit too focussed on the root refs now. A small, incremental change could've been:

/*
* This reference has to be deleted from packed-refs if it exists
* there. Note that root refs are never packed, so we don't have to
* deltee those from packed-refs.
*/

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 522

Quoted 11 lines
refs/files-backend.c +0 -0
 			if (!packed_transaction) {
 				packed_transaction = ref_store_transaction_begin(
t/t0600-reffiles-backend.sh +1 -0
index 74bfa2e9ba..b7f3287841 100755
@@ -519,4 +519,50 @@ test_expect_success 'symref transaction supports false symlink config' '
 	test_cmp expect actual
 '
 
+for ref in AUTO_MERGE CHERRY_PICK_HEAD REVERT_HEAD

Isn't it a bit excessive to test for all these different root refs? I don't see much value in doing that.

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 524

Quoted 2 lines
t/t0600-reffiles-backend.sh +2 -0
+do
+	for state in existing missing

Likewise, I'm not quite sure what we prove here. Should be fine to just test with an existing root ref.

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 537

Quoted 13 lines
t/t0600-reffiles-backend.sh +13 -0
+	do
+		test_expect_success "deleting $state $ref does not lock packed-refs" '
+			test_when_finished "rm -rf root-ref" &&
+			git init root-ref &&
+			(
+				cd root-ref &&
+				test_commit initial &&
+				if test "$state" = existing
+				then
+					git update-ref "$ref" HEAD
+				fi &&
+				: >.git/packed-refs.lock &&
+				git -c core.packedRefsTimeout=0 update-ref --no-deref -d "$ref" &&

Setting the timeout shouldn't really have any impact on the test result, should it?

Quoted 19 lines
t/t0600-reffiles-backend.sh +19 -0
+				test_path_is_missing ".git/$ref" &&
+				test_path_is_file .git/packed-refs.lock
+			)
+		'
+	done
+done
+
+test_expect_success 'root ref deletion preserves packed refs and their locking' '
+	test_when_finished "rm -rf root-ref" &&
+	git init root-ref &&
+	(
+		cd root-ref &&
+		test_commit initial &&
+		git update-ref refs/heads/packed-branch HEAD &&
+		git pack-refs --all &&
+		test_path_is_missing .git/refs/heads/packed-branch &&
+		cp .git/packed-refs expect &&
+		git update-ref AUTO_MERGE HEAD &&
+		: >.git/packed-refs.lock &&

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 566

Quoted 10 lines
t/t0600-reffiles-backend.sh +10 -0
+		git -c core.packedRefsTimeout=0 update-ref --no-deref -d AUTO_MERGE &&
+		test_cmp expect .git/packed-refs &&
+		test_must_fail git -c core.packedRefsTimeout=0 update-ref -d refs/heads/packed-branch 2>err &&
+		test_grep "Unable to create .*packed-refs.lock" err &&
+		test_cmp expect .git/packed-refs &&
+		rm .git/packed-refs.lock &&
+		git update-ref -d refs/heads/packed-branch &&
+		test_must_fail git rev-parse --verify refs/heads/packed-branch
+	)
+'

And this test feels like it's testing almost exactly what the other test does. The only difference is that we have an actual packed-refs file, but that can easily be squashed into the other test, too.

That being said, what we're missing is a test that creates a single transaction that updates both a root ref and a non-root-ref with a preexisting lockfile. Such a transaction should fail even though we skip the packed transaction for the roof ref itself.

Thanks!

Patrick

#2 lore raw

PATCH v2 1/1 refs/files: avoid packed-refs lock for root ref deletion

Deleting a root ref queues a packed-ref transaction in the files backend, even though root refs cannot be packed. For example, holding .git/packed-refs.lock makes "git update-ref --no-deref -d AUTO_MERGE" fail, whether or not AUTO_MERGE exists.

This also affects post-commit cleanup, which deletes AUTO_MERGE after updating HEAD. In a linked worktree with read-only shared metadata, commit succeeds but cleanup reports a packed-refs.lock error. Deleting CHERRY_PICK_HEAD and REVERT_HEAD is affected as well.

Skip the packed transaction for root-ref deletions. Keep loose-ref locking and packed-ref deletion for other refs unchanged.

Test deleting a root ref with packed-refs.lock held, and check that a transaction deleting both a root ref and a packed branch still fails without changing either ref.

---
Thanks for the review, Patrick.

Changes since v1:
- Keep the packed-ref deletion comment focused on its original purpose.
- Use one existing root ref and include a packed-refs file in the test.
- Drop the timeout overrides and redundant individual-ref cases.
- Add a transaction deleting a root ref and a packed branch together; check that a held packed-ref lock causes failure and preserves both refs.

AI assistance was used to generate the patch, tests, and commit message, including this revision.

The root-ref deletion regression fails without the fix. With the fix, 123 test scripts / 4078 tests pass, along with 249 unit tests and t0600 with SHA-256 (one platform skip in t0600).

 refs/files-backend.c        |  8 ++++---
 t/t0600-reffiles-backend.sh | 43 +++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 3 deletions(-)
refs/files-backend.c +5 -3
index a4c7858787..0333d5d355 100644
@@ -2981,10 +2981,12 @@ static int files_transaction_prepare(struct ref_store *ref_store,
 
 		if (update->flags & REF_DELETING &&
 		    !(update->flags & REF_LOG_ONLY) &&
-		    !(update->flags & REF_IS_PRUNING)) {
+		    !(update->flags & REF_IS_PRUNING) &&
+		    !is_root_ref(update->refname)) {
 			/*
-			 * This reference has to be deleted from
-			 * packed-refs if it exists there.
+			 * This reference has to be deleted from packed-refs if it
+			 * exists there. Root refs are never packed, so we do not
+			 * have to delete them from packed-refs.
 			 */
 			if (!packed_transaction) {
 				packed_transaction = ref_store_transaction_begin(
t/t0600-reffiles-backend.sh +43 -0

47 lines not on this page · raw message

Signature · 1 line
2.55.0
#3 lore raw
Quoted 10 lines · Ariel Keselman

Deleting a root ref queues a packed-ref transaction in the files backend, even though root refs cannot be packed. For example, holding .git/packed-refs.lock makes "git update-ref --no-deref -d AUTO_MERGE" fail, whether or not AUTO_MERGE exists.

This also affects post-commit cleanup, which deletes AUTO_MERGE after updating HEAD. In a linked worktree with read-only shared metadata, commit succeeds but cleanup reports a packed-refs.lock error. Deleting CHERRY_PICK_HEAD and REVERT_HEAD is affected as well.

Quoted 6 lines

Skip the packed transaction for root-ref deletions. Keep loose-ref locking and packed-ref deletion for other refs unchanged.

Test deleting a root ref with packed-refs.lock held, and check that a transaction deleting both a root ref and a packed branch still fails without changing either ref.

Nit: this last paragraph doesn't really add any value, as it's trivially visible from the patch that we add tests.

Quoted 11 lines

---
Thanks for the review, Patrick.

Changes since v1:
- Keep the packed-ref deletion comment focused on its original purpose.
- Use one existing root ref and include a packed-refs file in the test.
- Drop the timeout overrides and redundant individual-ref cases.
- Add a transaction deleting a root ref and a packed branch together; check that a held packed-ref lock causes failure and preserves both refs.

Quoted 6 lines

AI assistance was used to generate the patch, tests, and commit message, including this revision.

The root-ref deletion regression fails without the fix. With the fix, 123 test scripts / 4078 tests pass, along with 249 unit tests and t0600 with SHA-256 (one platform skip in t0600).

Huh? I hope that _all_ tests pass with this, not only 4078, and I would assume that you verified that this is the case at least on your machine.

Quoted 7 lines
t/t0600-reffiles-backend.sh +0 -0
index 74bfa2e9ba..65ca19e84b 100755
@@ -519,4 +519,47 @@ test_expect_success 'symref transaction supports false symlink config' '
 	test_cmp expect actual
 '

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 530

Quoted 10 lines
t/t0600-reffiles-backend.sh +9 -0
 
+test_expect_success 'deleting a root ref does not lock packed-refs' '
+	test_when_finished "rm -rf root-ref" &&
+	git init root-ref &&
+	(
+		cd root-ref &&
+		test_commit initial &&
+		git pack-refs --all &&
+		cp .git/packed-refs expect &&
+		git update-ref AUTO_MERGE HEAD &&

For added benefit we could even execute git-pack-refs(1) after having created AUTO_MERGE and then execute test_path_is_file for it just to prove that it really doesn't get packed. But other than that the tests look good to me.

Quoted 9 lines
t/t0600-reffiles-backend.sh +9 -0
+		: >.git/packed-refs.lock &&
+		git update-ref --no-deref -d AUTO_MERGE &&
+		test_path_is_missing .git/AUTO_MERGE &&
+		test_path_is_file .git/packed-refs.lock &&
+		test_cmp expect .git/packed-refs
+	)
+'
+
+test_expect_success 'deleting root and packed refs in one transaction requires packed-refs lock' '

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 549

Quoted 10 lines
t/t0600-reffiles-backend.sh +10 -0
+	test_when_finished "rm -rf root-ref" &&
+	git init root-ref &&
+	(
+		cd root-ref &&
+		test_commit initial &&
+		git update-ref refs/heads/packed-branch HEAD &&
+		git pack-refs --all &&
+		test_path_is_missing .git/refs/heads/packed-branch &&
+		git update-ref AUTO_MERGE HEAD &&
+		git rev-parse AUTO_MERGE refs/heads/packed-branch >expect &&

We could strengthen this a bit by listing the state of all refs:

    git refs list --include-root-refs >expect

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 555

Quoted 6 lines
t/t0600-reffiles-backend.sh +6 -0
+		cat >stdin <<-EOF &&
+		start
+		delete AUTO_MERGE
+		delete refs/heads/packed-branch
+		prepare
+		commit

We can drop start/prepare/commit here, those are optional. We can also drop the extra file and just write the data into git-update-ref(1) directly via the heredoc.

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 563

Quoted 8 lines
t/t0600-reffiles-backend.sh +8 -0
+		EOF
+		: >.git/packed-refs.lock &&
+		test_must_fail git update-ref --no-deref --stdin <stdin 2>err &&
+		test_grep "Unable to create .*packed-refs.lock" err &&
+		git rev-parse AUTO_MERGE refs/heads/packed-branch >actual &&
+		test_cmp expect actual
+	)
+'

Thanks!

Patrick

#4 lore raw

Sorry, I accidentally sent v3 as a new thread. It is available here:

https://lore.kernel.org/git/[email protected]/

Quoted 113 lines · Patrick Steinhardt
Quoted 10 lines · Ariel Keselman

Deleting a root ref queues a packed-ref transaction in the files backend, even though root refs cannot be packed. For example, holding .git/packed-refs.lock makes "git update-ref --no-deref -d AUTO_MERGE" fail, whether or not AUTO_MERGE exists.

This also affects post-commit cleanup, which deletes AUTO_MERGE after updating HEAD. In a linked worktree with read-only shared metadata, commit succeeds but cleanup reports a packed-refs.lock error. Deleting CHERRY_PICK_HEAD and REVERT_HEAD is affected as well.

Quoted 6 lines

Skip the packed transaction for root-ref deletions. Keep loose-ref locking and packed-ref deletion for other refs unchanged.

Test deleting a root ref with packed-refs.lock held, and check that a transaction deleting both a root ref and a packed branch still fails without changing either ref.

Nit: this last paragraph doesn't really add any value, as it's trivially visible from the patch that we add tests.

Quoted 11 lines

---
Thanks for the review, Patrick.

Changes since v1:
- Keep the packed-ref deletion comment focused on its original purpose.
- Use one existing root ref and include a packed-refs file in the test.
- Drop the timeout overrides and redundant individual-ref cases.
- Add a transaction deleting a root ref and a packed branch together; check that a held packed-ref lock causes failure and preserves both refs.

Quoted 6 lines

AI assistance was used to generate the patch, tests, and commit message, including this revision.

The root-ref deletion regression fails without the fix. With the fix, 123 test scripts / 4078 tests pass, along with 249 unit tests and t0600 with SHA-256 (one platform skip in t0600).

Huh? I hope that _all_ tests pass with this, not only 4078, and I would assume that you verified that this is the case at least on your machine.

Quoted 7 lines
t/t0600-reffiles-backend.sh +0 -0
index 74bfa2e9ba..65ca19e84b 100755
@@ -519,4 +519,47 @@ test_expect_success 'symref transaction supports false symlink config' '
      test_cmp expect actual
 '

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 530

Quoted 10 lines
t/t0600-reffiles-backend.sh +9 -0
 
+test_expect_success 'deleting a root ref does not lock packed-refs' '
+     test_when_finished "rm -rf root-ref" &&
+     git init root-ref &&
+     (
+             cd root-ref &&
+             test_commit initial &&
+             git pack-refs --all &&
+             cp .git/packed-refs expect &&
+             git update-ref AUTO_MERGE HEAD &&

For added benefit we could even execute git-pack-refs(1) after having created AUTO_MERGE and then execute test_path_is_file for it just to prove that it really doesn't get packed. But other than that the tests look good to me.

Quoted 9 lines
t/t0600-reffiles-backend.sh +9 -0
+             : >.git/packed-refs.lock &&
+             git update-ref --no-deref -d AUTO_MERGE &&
+             test_path_is_missing .git/AUTO_MERGE &&
+             test_path_is_file .git/packed-refs.lock &&
+             test_cmp expect .git/packed-refs
+     )
+'
+
+test_expect_success 'deleting root and packed refs in one transaction requires packed-refs lock' '

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 549

Quoted 10 lines
t/t0600-reffiles-backend.sh +10 -0
+     test_when_finished "rm -rf root-ref" &&
+     git init root-ref &&
+     (
+             cd root-ref &&
+             test_commit initial &&
+             git update-ref refs/heads/packed-branch HEAD &&
+             git pack-refs --all &&
+             test_path_is_missing .git/refs/heads/packed-branch &&
+             git update-ref AUTO_MERGE HEAD &&
+             git rev-parse AUTO_MERGE refs/heads/packed-branch >expect &&

We could strengthen this a bit by listing the state of all refs:

    git refs list --include-root-refs >expect

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 555

Quoted 6 lines
t/t0600-reffiles-backend.sh +6 -0
+             cat >stdin <<-EOF &&
+             start
+             delete AUTO_MERGE
+             delete refs/heads/packed-branch
+             prepare
+             commit

We can drop start/prepare/commit here, those are optional. We can also drop the extra file and just write the data into git-update-ref(1) directly via the heredoc.

t/t0600-reffiles-backend.sh test_expect_success 'symref transaction supports false symlink config' ' · line 563

Quoted 8 lines
t/t0600-reffiles-backend.sh +8 -0
+             EOF
+             : >.git/packed-refs.lock &&
+             test_must_fail git update-ref --no-deref --stdin <stdin 2>err &&
+             test_grep "Unable to create .*packed-refs.lock" err &&
+             git rev-parse AUTO_MERGE refs/heads/packed-branch >actual &&
+             test_cmp expect actual
+     )
+'

Thanks!

Patrick