Git development
 help / color / mirror / Atom feed
From: "Onur Gündüz via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Onur Gündüz" <ogunduz@gmail.com>, "Onur Gündüz" <ogunduz@gmail.com>
Subject: [PATCH] refs/debug: release the wrapped ref store
Date: Thu, 26 Nov 2026 03:37:59 +0000	[thread overview]
Message-ID: <pull.2331.git.1795664279088.gitgitgadget@gmail.com> (raw)

From: =?UTF-8?q?Onur=20G=C3=BCnd=C3=BCz?= <ogunduz@gmail.com>

The debug ref store owns both the wrapped store and a copy of its
backend descriptor. However, debug_release() only invokes the
wrapped backend's release callback. This leaves the wrapped store,
its gitdir and the copied descriptor allocated when the debug store
is freed.

Release the wrapped store through ref_store_release() to free its
gitdir as well as its backend-specific resources, then free the
store itself. Embed the copied backend descriptor in the debug
store so that it shares the wrapper's lifetime.

Add a test helper that reads HEAD through a repository's main ref
store and then clears the repository. Exercise it with tracing on
and off. With GIT_TRACE_REFS=1, the test fails under LeakSanitizer
without this fix for both the files and reftable backends.

Signed-off-by: Onur Gündüz <ogunduz@gmail.com>
---
    refs/debug: release the wrapped ref store

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2331%2Ftetsuo%2Fsubmit%2Frefs-debug-lifetime-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2331/tetsuo/submit/refs-debug-lifetime-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2331

 refs/debug.c               | 13 ++++++-------
 t/helper/test-repository.c | 19 ++++++++++++++++++-
 t/t1405-main-ref-store.sh  |  7 +++++++
 3 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/refs/debug.c b/refs/debug.c
index 639db0f26e..78d1eb5a98 100644
--- a/refs/debug.c
+++ b/refs/debug.c
@@ -8,6 +8,7 @@ static struct trace_key trace_refs = TRACE_KEY_INIT(REFS);
 struct debug_ref_store {
 	struct ref_store base;
 	struct ref_store *refs;
+	struct ref_storage_be backend;
 };
 
 extern struct ref_storage_be refs_be_debug;
@@ -15,27 +16,25 @@ extern struct ref_storage_be refs_be_debug;
 struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store)
 {
 	struct debug_ref_store *res;
-	struct ref_storage_be *be_copy;
 
 	if (!trace_want(&trace_refs)) {
 		return store;
 	}
 	res = xmalloc(sizeof(struct debug_ref_store));
-	be_copy = xmalloc(sizeof(*be_copy));
-	*be_copy = refs_be_debug;
-	/* we never deallocate backends, so safe to copy the pointer. */
-	be_copy->name = store->be->name;
+	res->backend = refs_be_debug;
+	res->backend.name = store->be->name;
 	trace_printf_key(&trace_refs, "ref_store for %s\n", gitdir);
 	res->refs = store;
 	base_ref_store_init((struct ref_store *)res, store->repo, gitdir,
-			    be_copy);
+			    &res->backend);
 	return (struct ref_store *)res;
 }
 
 static void debug_release(struct ref_store *refs)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
-	drefs->refs->be->release(drefs->refs);
+	ref_store_release(drefs->refs);
+	free(drefs->refs);
 	trace_printf_key(&trace_refs, "release\n");
 }
 
diff --git a/t/helper/test-repository.c b/t/helper/test-repository.c
index 9ba94cdffa..6184db863f 100644
--- a/t/helper/test-repository.c
+++ b/t/helper/test-repository.c
@@ -6,6 +6,7 @@
 #include "environment.h"
 #include "hex.h"
 #include "object.h"
+#include "refs.h"
 #include "repository.h"
 #include "setup.h"
 #include "tree.h"
@@ -65,11 +66,27 @@ static void test_get_commit_tree_in_graph(const char *gitdir,
 	repo_clear(&r);
 }
 
+static void test_ref_store(const char *gitdir)
+{
+	struct repository r;
+	struct object_id oid;
+
+	if (repo_init(&r, gitdir, NULL))
+		die("cannot open repository %s", gitdir);
+	if (refs_read_ref(get_main_ref_store(&r), "HEAD", &oid))
+		die("cannot read HEAD");
+	repo_clear(&r);
+}
+
 int cmd__repository(int argc, const char **argv)
 {
 	if (argc < 2)
 		die("must have at least 2 arguments");
-	if (!strcmp(argv[1], "parse_commit_in_graph")) {
+	if (!strcmp(argv[1], "ref-store")) {
+		if (argc != 3)
+			die("usage: test-tool repository ref-store <gitdir>");
+		test_ref_store(argv[2]);
+	} else if (!strcmp(argv[1], "parse_commit_in_graph")) {
 		struct object_id oid;
 		if (argc < 5)
 			die("not enough arguments");
diff --git a/t/t1405-main-ref-store.sh b/t/t1405-main-ref-store.sh
index 6d8f401a2a..fca3b7415b 100755
--- a/t/t1405-main-ref-store.sh
+++ b/t/t1405-main-ref-store.sh
@@ -14,6 +14,13 @@ test_expect_success 'setup' '
 	test_commit one
 '
 
+for trace in 0 1
+do
+	test_expect_success "release main ref store with GIT_TRACE_REFS=$trace" '
+		GIT_TRACE_REFS=$trace test-tool repository ref-store .git
+	'
+done
+
 test_expect_success 'create_symref(FOO, refs/heads/main)' '
 	$RUN create-symref FOO refs/heads/main nothing &&
 	echo refs/heads/main >expected &&

base-commit: 03b41523c576f26ad34085378717a0c1b287470f
-- 
gitgitgadget

                 reply	other threads:[~2026-11-26  3:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=pull.2331.git.1795664279088.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=ogunduz@gmail.com \
    /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