{"thread":{"id":"66938","subject":"[PATCH] refs/debug: release the wrapped ref store","startedAt":"2026-11-26T03:37:59Z","lastAt":"2026-11-26T03:37:59Z","messageCount":1,"participants":["Onur Gündüz via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559435","messageId":"pull.2331.git.1795664279088.gitgitgadget@gmail.com","threadId":"66938","inReplyTo":null,"subject":"[PATCH] refs/debug: release the wrapped ref store","fromName":"Onur Gündüz via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-26T03:37:59Z","receivedAt":"2026-11-26T03:37:59Z","isPatch":true,"sender":{"key":"name:Onur Gündüz","avatar":null},"body":"From: =?UTF-8?q?Onur=20G=C3=BCnd=C3=BCz?= <ogunduz@gmail.com>\n\nThe debug ref store owns both the wrapped store and a copy of its\nbackend descriptor. However, debug_release() only invokes the\nwrapped backend's release callback. This leaves the wrapped store,\nits gitdir and the copied descriptor allocated when the debug store\nis freed.\n\nRelease the wrapped store through ref_store_release() to free its\ngitdir as well as its backend-specific resources, then free the\nstore itself. Embed the copied backend descriptor in the debug\nstore so that it shares the wrapper's lifetime.\n\nAdd a test helper that reads HEAD through a repository's main ref\nstore and then clears the repository. Exercise it with tracing on\nand off. With GIT_TRACE_REFS=1, the test fails under LeakSanitizer\nwithout this fix for both the files and reftable backends.\n\nSigned-off-by: Onur Gündüz <ogunduz@gmail.com>\n---\n    refs/debug: release the wrapped ref store\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2331%2Ftetsuo%2Fsubmit%2Frefs-debug-lifetime-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2331/tetsuo/submit/refs-debug-lifetime-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/2331\n\n refs/debug.c               | 13 ++++++-------\n t/helper/test-repository.c | 19 ++++++++++++++++++-\n t/t1405-main-ref-store.sh  |  7 +++++++\n 3 files changed, 31 insertions(+), 8 deletions(-)\n\ndiff --git a/refs/debug.c b/refs/debug.c\nindex 639db0f26e..78d1eb5a98 100644\n--- a/refs/debug.c\n+++ b/refs/debug.c\n@@ -8,6 +8,7 @@ static struct trace_key trace_refs = TRACE_KEY_INIT(REFS);\n struct debug_ref_store {\n \tstruct ref_store base;\n \tstruct ref_store *refs;\n+\tstruct ref_storage_be backend;\n };\n \n extern struct ref_storage_be refs_be_debug;\n@@ -15,27 +16,25 @@ extern struct ref_storage_be refs_be_debug;\n struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store)\n {\n \tstruct debug_ref_store *res;\n-\tstruct ref_storage_be *be_copy;\n \n \tif (!trace_want(&trace_refs)) {\n \t\treturn store;\n \t}\n \tres = xmalloc(sizeof(struct debug_ref_store));\n-\tbe_copy = xmalloc(sizeof(*be_copy));\n-\t*be_copy = refs_be_debug;\n-\t/* we never deallocate backends, so safe to copy the pointer. */\n-\tbe_copy->name = store->be->name;\n+\tres->backend = refs_be_debug;\n+\tres->backend.name = store->be->name;\n \ttrace_printf_key(&trace_refs, \"ref_store for %s\\n\", gitdir);\n \tres->refs = store;\n \tbase_ref_store_init((struct ref_store *)res, store->repo, gitdir,\n-\t\t\t    be_copy);\n+\t\t\t    &res->backend);\n \treturn (struct ref_store *)res;\n }\n \n static void debug_release(struct ref_store *refs)\n {\n \tstruct debug_ref_store *drefs = (struct debug_ref_store *)refs;\n-\tdrefs->refs->be->release(drefs->refs);\n+\tref_store_release(drefs->refs);\n+\tfree(drefs->refs);\n \ttrace_printf_key(&trace_refs, \"release\\n\");\n }\n \ndiff --git a/t/helper/test-repository.c b/t/helper/test-repository.c\nindex 9ba94cdffa..6184db863f 100644\n--- a/t/helper/test-repository.c\n+++ b/t/helper/test-repository.c\n@@ -6,6 +6,7 @@\n #include \"environment.h\"\n #include \"hex.h\"\n #include \"object.h\"\n+#include \"refs.h\"\n #include \"repository.h\"\n #include \"setup.h\"\n #include \"tree.h\"\n@@ -65,11 +66,27 @@ static void test_get_commit_tree_in_graph(const char *gitdir,\n \trepo_clear(&r);\n }\n \n+static void test_ref_store(const char *gitdir)\n+{\n+\tstruct repository r;\n+\tstruct object_id oid;\n+\n+\tif (repo_init(&r, gitdir, NULL))\n+\t\tdie(\"cannot open repository %s\", gitdir);\n+\tif (refs_read_ref(get_main_ref_store(&r), \"HEAD\", &oid))\n+\t\tdie(\"cannot read HEAD\");\n+\trepo_clear(&r);\n+}\n+\n int cmd__repository(int argc, const char **argv)\n {\n \tif (argc < 2)\n \t\tdie(\"must have at least 2 arguments\");\n-\tif (!strcmp(argv[1], \"parse_commit_in_graph\")) {\n+\tif (!strcmp(argv[1], \"ref-store\")) {\n+\t\tif (argc != 3)\n+\t\t\tdie(\"usage: test-tool repository ref-store <gitdir>\");\n+\t\ttest_ref_store(argv[2]);\n+\t} else if (!strcmp(argv[1], \"parse_commit_in_graph\")) {\n \t\tstruct object_id oid;\n \t\tif (argc < 5)\n \t\t\tdie(\"not enough arguments\");\ndiff --git a/t/t1405-main-ref-store.sh b/t/t1405-main-ref-store.sh\nindex 6d8f401a2a..fca3b7415b 100755\n--- a/t/t1405-main-ref-store.sh\n+++ b/t/t1405-main-ref-store.sh\n@@ -14,6 +14,13 @@ test_expect_success 'setup' '\n \ttest_commit one\n '\n \n+for trace in 0 1\n+do\n+\ttest_expect_success \"release main ref store with GIT_TRACE_REFS=$trace\" '\n+\t\tGIT_TRACE_REFS=$trace test-tool repository ref-store .git\n+\t'\n+done\n+\n test_expect_success 'create_symref(FOO, refs/heads/main)' '\n \t$RUN create-symref FOO refs/heads/main nothing &&\n \techo refs/heads/main >expected &&\n\nbase-commit: 03b41523c576f26ad34085378717a0c1b287470f\n-- \ngitgitgadget\n\n"}]}