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

[PATCH] refs/debug: release the wrapped ref store

From
OGOnur Gündüz via GitGitGadget <gitgitgadget@gmail.com>
Date
Nov 26, 2026, 03:37 UTC
Message-ID
<pull.2331.git.1795664279088.gitgitgadget@gmail.com>
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
Message 1 of 1 in “refs/debug: release the wrapped ref store”
  1. refs/debug: release the wrapped ref storeOnur Gündüz via GitGitGadget, Nov 26, 2026

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.