{"thread":{"id":"66936","subject":"[PATCH] blame: free the suspects slab at scoreboard teardown","startedAt":"2026-11-26T03:37:10Z","lastAt":"2026-12-01T15:41:47Z","messageCount":2,"participants":["Onur Gündüz via GitGitGadget","Patrick Steinhardt"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559432","messageId":"pull.2330.git.1795664230820.gitgitgadget@gmail.com","threadId":"66936","inReplyTo":null,"subject":"[PATCH] blame: free the suspects slab at scoreboard teardown","fromName":"Onur Gündüz via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-26T03:37:10Z","receivedAt":"2026-11-26T03:37:10Z","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\nsetup_scoreboard() initializes the file-static suspects slab each\ntime it runs, but cleanup_scoreboard() doesn't free the slab. A\nsecond setup overwrites the pointer to the previous slab and leaks\nits allocations.\n\nClear the slab during cleanup. Each final blame entry holds a\nreference to its origin, so release those references first:\nblame_origin_decref() uses the slab to unlink an origin when its\nreference count reaches zero.\n\nAdd a regression test that sets up and cleans up two scoreboards\nin one process. Without the fix, LeakSanitizer reports a leaked\nslab; with the fix the test passes for both files and reftable.\n\nSigned-off-by: Onur Gündüz <ogunduz@gmail.com>\n---\n    blame: free the suspects slab at scoreboard teardown\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2330%2Ftetsuo%2Fsubmit%2Fblame-lifetime-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2330/tetsuo/submit/blame-lifetime-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/2330\n\n blame.c                     |  1 +\n blame.h                     |  1 +\n builtin/blame.c             |  1 +\n t/helper/test-repository.c  | 34 +++++++++++++++++++++++++++++++++-\n t/meson.build               |  1 +\n t/t8016-blame-scoreboard.sh | 12 ++++++++++++\n 6 files changed, 49 insertions(+), 1 deletion(-)\n create mode 100755 t/t8016-blame-scoreboard.sh\n\ndiff --git a/blame.c b/blame.c\nindex 126e232416..61e1a45650 100644\n--- a/blame.c\n+++ b/blame.c\n@@ -2935,6 +2935,7 @@ void setup_blame_bloom_data(struct blame_scoreboard *sb)\n \n void cleanup_scoreboard(struct blame_scoreboard *sb)\n {\n+\tclear_blame_suspects(&blame_suspects);\n \tfree(sb->lineno);\n \tfree(sb->final_buf);\n \tclear_prio_queue(&sb->commits);\ndiff --git a/blame.h b/blame.h\nindex 3b34be0e5c..75a910bb67 100644\n--- a/blame.h\n+++ b/blame.h\n@@ -180,6 +180,7 @@ void init_scoreboard(struct blame_scoreboard *sb);\n void setup_scoreboard(struct blame_scoreboard *sb,\n \t\t      struct blame_origin **orig);\n void setup_blame_bloom_data(struct blame_scoreboard *sb);\n+/* Release all entry and caller references to origins before clearing the slab. */\n void cleanup_scoreboard(struct blame_scoreboard *sb);\n \n struct blame_entry *blame_entry_prepend(struct blame_entry *head,\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 48d5251c6d..332795d18f 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -1323,6 +1323,7 @@ parse_done:\n cleanup:\n \tfor (ent = sb.ent; ent; ) {\n \t\tstruct blame_entry *e = ent->next;\n+\t\tblame_origin_decref(ent->suspect);\n \t\tfree(ent);\n \t\tent = e;\n \t}\ndiff --git a/t/helper/test-repository.c b/t/helper/test-repository.c\nindex 9ba94cdffa..245253efbd 100644\n--- a/t/helper/test-repository.c\n+++ b/t/helper/test-repository.c\n@@ -1,12 +1,14 @@\n #define USE_THE_REPOSITORY_VARIABLE\n \n #include \"test-tool.h\"\n+#include \"blame.h\"\n #include \"commit-graph.h\"\n #include \"commit.h\"\n #include \"environment.h\"\n #include \"hex.h\"\n #include \"object.h\"\n #include \"repository.h\"\n+#include \"revision.h\"\n #include \"setup.h\"\n #include \"tree.h\"\n \n@@ -65,11 +67,41 @@ static void test_get_commit_tree_in_graph(const char *gitdir,\n \trepo_clear(&r);\n }\n \n+static void test_blame_scoreboard(const char *path)\n+{\n+\tstruct repository *repo = the_repository;\n+\n+\tsetup_git_directory(repo);\n+\t/* A second setup exposes leaks left by the first scoreboard. */\n+\tfor (int i = 0; i < 2; i++) {\n+\t\tconst char *args[] = { \"blame\", \"HEAD\", NULL };\n+\t\tstruct blame_scoreboard sb;\n+\t\tstruct blame_origin *origin;\n+\t\tstruct rev_info revs;\n+\n+\t\trepo_init_revisions(repo, &revs, NULL);\n+\t\tsetup_revisions(ARRAY_SIZE(args) - 1, args, &revs, NULL);\n+\t\tinit_scoreboard(&sb);\n+\t\tsb.repo = repo;\n+\t\tsb.revs = &revs;\n+\t\tsb.path = path;\n+\t\tsetup_scoreboard(&sb, &origin);\n+\t\tblame_origin_decref(origin);\n+\t\tcleanup_scoreboard(&sb);\n+\t\trelease_revisions(&revs);\n+\t}\n+\trepo_clear(repo);\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], \"blame-scoreboard\")) {\n+\t\tif (argc != 3)\n+\t\t\tdie(\"usage: test-tool repository blame-scoreboard <path>\");\n+\t\ttest_blame_scoreboard(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/meson.build b/t/meson.build\nindex 3ca7b27104..6188a0c4bc 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -993,6 +993,7 @@ integration_tests = [\n   't8013-blame-ignore-revs.sh',\n   't8014-blame-ignore-fuzzy.sh',\n   't8015-blame-diff-algorithm.sh',\n+  't8016-blame-scoreboard.sh',\n   't8020-last-modified.sh',\n   't9001-send-email.sh',\n   't9002-column.sh',\ndiff --git a/t/t8016-blame-scoreboard.sh b/t/t8016-blame-scoreboard.sh\nnew file mode 100755\nindex 0000000000..bc7f85c468\n--- /dev/null\n+++ b/t/t8016-blame-scoreboard.sh\n@@ -0,0 +1,12 @@\n+#!/bin/sh\n+\n+test_description='blame scoreboard teardown'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'set up and tear down two scoreboards in one process' '\n+\ttest_commit one file &&\n+\ttest-tool repository blame-scoreboard file\n+'\n+\n+test_done\n\nbase-commit: 03b41523c576f26ad34085378717a0c1b287470f\n-- \ngitgitgadget\n\n"},{"id":"559803","messageId":"aw7qu97ncV5Gifmh@pks.im","threadId":"66936","inReplyTo":"pull.2330.git.1795664230820.gitgitgadget@gmail.com","subject":"Re: [PATCH] blame: free the suspects slab at scoreboard teardown","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-12-01T15:41:47Z","receivedAt":"2026-12-01T15:41:47Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Nov 26, 2026 at 03:37:10AM +0000, Onur Gündüz via GitGitGadget wrote:\n> From: =?UTF-8?q?Onur=20G=C3=BCnd=C3=BCz?= <ogunduz@gmail.com>\n> \n> setup_scoreboard() initializes the file-static suspects slab each\n> time it runs, but cleanup_scoreboard() doesn't free the slab. A\n> second setup overwrites the pointer to the previous slab and leaks\n> its allocations.\n> \n> Clear the slab during cleanup. Each final blame entry holds a\n> reference to its origin, so release those references first:\n> blame_origin_decref() uses the slab to unlink an origin when its\n> reference count reaches zero.\n> \n> Add a regression test that sets up and cleans up two scoreboards\n> in one process. Without the fix, LeakSanitizer reports a leaked\n> slab; with the fix the test passes for both files and reftable.\n\nSo this is a theoretical concern that cannot happen in practice because\nwe never blame multiple times? Doesn't mean it's not worth fixing of\ncourse, also to help potential future users of this interface.\n\n> diff --git a/blame.c b/blame.c\n> index 126e232416..61e1a45650 100644\n> --- a/blame.c\n> +++ b/blame.c\n> @@ -2935,6 +2935,7 @@ void setup_blame_bloom_data(struct blame_scoreboard *sb)\n>  \n>  void cleanup_scoreboard(struct blame_scoreboard *sb)\n>  {\n> +\tclear_blame_suspects(&blame_suspects);\n>  \tfree(sb->lineno);\n>  \tfree(sb->final_buf);\n>  \tclear_prio_queue(&sb->commits);\n\nIt's of course quite ugly that we clean up global state in a function\nthat pretends to act on a passed-in parameter. So ideally, we'd make\nthe blame suspects a member of the scoreboard so that we can fix its\nlifecycle.\n\nBut that may be a bigger change, and seeing the symmetry between\n`setup_scoreboard()` and `cleanup_scoreboard` makes me feel a bit more\ngenerous towards the change.\n\n> diff --git a/blame.h b/blame.h\n> index 3b34be0e5c..75a910bb67 100644\n> --- a/blame.h\n> +++ b/blame.h\n> @@ -180,6 +180,7 @@ void init_scoreboard(struct blame_scoreboard *sb);\n>  void setup_scoreboard(struct blame_scoreboard *sb,\n>  \t\t      struct blame_origin **orig);\n>  void setup_blame_bloom_data(struct blame_scoreboard *sb);\n> +/* Release all entry and caller references to origins before clearing the slab. */\n>  void cleanup_scoreboard(struct blame_scoreboard *sb);\n\nI don't think this comment adds much. It might have made sense if it\nexplicitly mentioned that we also release global state, but it doesn't.\n\n> diff --git a/builtin/blame.c b/builtin/blame.c\n> index 48d5251c6d..332795d18f 100644\n> --- a/builtin/blame.c\n> +++ b/builtin/blame.c\n> @@ -1323,6 +1323,7 @@ parse_done:\n>  cleanup:\n>  \tfor (ent = sb.ent; ent; ) {\n>  \t\tstruct blame_entry *e = ent->next;\n> +\t\tblame_origin_decref(ent->suspect);\n>  \t\tfree(ent);\n>  \t\tent = e;\n>  \t}\n\nAnd I guess that these were previously held alive by the global blame\nsuspects?\n\n> diff --git a/t/helper/test-repository.c b/t/helper/test-repository.c\n> index 9ba94cdffa..245253efbd 100644\n> --- a/t/helper/test-repository.c\n> +++ b/t/helper/test-repository.c\n> @@ -65,11 +67,41 @@ static void test_get_commit_tree_in_graph(const char *gitdir,\n>  \trepo_clear(&r);\n>  }\n>  \n> +static void test_blame_scoreboard(const char *path)\n> +{\n> +\tstruct repository *repo = the_repository;\n> +\n> +\tsetup_git_directory(repo);\n> +\t/* A second setup exposes leaks left by the first scoreboard. */\n> +\tfor (int i = 0; i < 2; i++) {\n> +\t\tconst char *args[] = { \"blame\", \"HEAD\", NULL };\n> +\t\tstruct blame_scoreboard sb;\n> +\t\tstruct blame_origin *origin;\n> +\t\tstruct rev_info revs;\n> +\n> +\t\trepo_init_revisions(repo, &revs, NULL);\n> +\t\tsetup_revisions(ARRAY_SIZE(args) - 1, args, &revs, NULL);\n> +\t\tinit_scoreboard(&sb);\n> +\t\tsb.repo = repo;\n> +\t\tsb.revs = &revs;\n> +\t\tsb.path = path;\n> +\t\tsetup_scoreboard(&sb, &origin);\n> +\t\tblame_origin_decref(origin);\n> +\t\tcleanup_scoreboard(&sb);\n> +\t\trelease_revisions(&revs);\n> +\t}\n> +\trepo_clear(repo);\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], \"blame-scoreboard\")) {\n> +\t\tif (argc != 3)\n> +\t\t\tdie(\"usage: test-tool repository blame-scoreboard <path>\");\n> +\t\ttest_blame_scoreboard(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\");\n\nInstead of adding another helper, can this maybe be turned into a proper\nunit test?\n\nHmm, it does require some test setup. But now that we have an in-memory\nobject database I wonder whether we can abuse that for our tests and\ncreate a repository that sits purely in memory. It would of course also\nrequire an in-memory refdb, but that's probably not all that hard to do.\n\nIn any case, if we had that, then writing unit tests for specific\nrepository functionality that doesn't require a worktree may become more\nfeasible.\n\nThat being said, it feels a bit like a stretch for this particular\ncommit, as that likely requires a bunch of work. But I feel like the\nboilerplate added here just to exercise a memory leak is way too\nexcessive. So ultimately, if we cannot trigger this via normal plumbing\ncommands, I'd lean towards not having tests at all.\n\nThanks!\n\nPatrick\n\n"}]}