Skip to content

blame: free the suspects slab at scoreboard teardown - #2330

Open
tetsuo wants to merge 1 commit into
gitgitgadget:maintfrom
tetsuo:submit/blame-lifetime
Open

tetsuo wants to merge 1 commit into
gitgitgadget:maintfrom
tetsuo:submit/blame-lifetime

Conversation

@tetsuo

@tetsuo tetsuo commented Nov 25, 2026 •

Copy link
Copy Markdown

cc: Patrick Steinhardt ps@pks.im

setup_scoreboard() initializes the file-static suspects slab each
time it runs, but cleanup_scoreboard() doesn't free the slab. A
second setup overwrites the pointer to the previous slab and leaks
its allocations.

Clear the slab during cleanup. Each final blame entry holds a
reference to its origin, so release those references first:
blame_origin_decref() uses the slab to unlink an origin when its
reference count reaches zero.

Add a regression test that sets up and cleans up two scoreboards
in one process. Without the fix, LeakSanitizer reports a leaked
slab; with the fix the test passes for both files and reftable.

Signed-off-by: Onur Gündüz <ogunduz@gmail.com>
@gitgitgadget

gitgitgadget Bot commented Nov 25, 2026

Copy link
Copy Markdown

Welcome to GitGitGadget

Hi @tetsuo, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that either:

  • Your Pull Request has a good description, if it consists of multiple commits, as it will be used as cover letter.
  • Your Pull Request description is empty, if it consists of a single commit, as the commit message should be descriptive enough by itself.

You can CC potential reviewers by adding a footer to the PR description with the following syntax:

CC: Revi Ewer <revi.ewer@example.com>, Ill Takalook <ill.takalook@example.net>

NOTE: DO NOT copy/paste your CC list from a previous GGG PR's description,
because it will result in a malformed CC list on the mailing list. See
example.

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "revisions:" to state which subsystem the change is about, and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the Libera Chat IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this. Note that any reviewers CC'd via the list in the PR description will not actually be sent emails.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join git-mentoring@googlegroups.com, where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Libera Chat. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

@dscho

dscho commented Nov 25, 2026

Copy link
Copy Markdown
Member

/allow

@gitgitgadget

gitgitgadget Bot commented Nov 25, 2026

Copy link
Copy Markdown

User tetsuo already allowed to use GitGitGadget.

@tetsuo

tetsuo commented Nov 26, 2026

Copy link
Copy Markdown
Author

/preview

@gitgitgadget

gitgitgadget Bot commented Nov 26, 2026

Copy link
Copy Markdown

Error: Could not determine full name of tetsuo

@tetsuo

tetsuo commented Nov 26, 2026

Copy link
Copy Markdown
Author

/preview

@gitgitgadget

gitgitgadget Bot commented Nov 26, 2026

Copy link
Copy Markdown

Preview email sent as pull.2330.git.1795660230857.gitgitgadget@gmail.com

@tetsuo

tetsuo commented Nov 26, 2026

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Nov 26, 2026

Copy link
Copy Markdown

Submitted as pull.2330.git.1795664230820.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2330/tetsuo/submit/blame-lifetime-v1

To fetch this version to local tag pr-2330/tetsuo/submit/blame-lifetime-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2330/tetsuo/submit/blame-lifetime-v1

@gitgitgadget

gitgitgadget Bot commented Dec 1, 2026

Copy link
Copy Markdown

Patrick Steinhardt wrote on the Git mailing list (how to reply to this email):

On Thu, Nov 26, 2026 at 03:37:10AM +0000, Onur Gündüz via GitGitGadget wrote:
> From: =?UTF-8?q?Onur=20G=C3=BCnd=C3=BCz?= <ogunduz@gmail.com>
> 
> setup_scoreboard() initializes the file-static suspects slab each
> time it runs, but cleanup_scoreboard() doesn't free the slab. A
> second setup overwrites the pointer to the previous slab and leaks
> its allocations.
> 
> Clear the slab during cleanup. Each final blame entry holds a
> reference to its origin, so release those references first:
> blame_origin_decref() uses the slab to unlink an origin when its
> reference count reaches zero.
> 
> Add a regression test that sets up and cleans up two scoreboards
> in one process. Without the fix, LeakSanitizer reports a leaked
> slab; with the fix the test passes for both files and reftable.

So this is a theoretical concern that cannot happen in practice because
we never blame multiple times? Doesn't mean it's not worth fixing of
course, also to help potential future users of this interface.

> diff --git a/blame.c b/blame.c
> index 126e232416..61e1a45650 100644
> --- a/blame.c
> +++ b/blame.c
> @@ -2935,6 +2935,7 @@ void setup_blame_bloom_data(struct blame_scoreboard *sb)
>  
>  void cleanup_scoreboard(struct blame_scoreboard *sb)
>  {
> +	clear_blame_suspects(&blame_suspects);
>  	free(sb->lineno);
>  	free(sb->final_buf);
>  	clear_prio_queue(&sb->commits);

It's of course quite ugly that we clean up global state in a function
that pretends to act on a passed-in parameter. So ideally, we'd make
the blame suspects a member of the scoreboard so that we can fix its
lifecycle.

But that may be a bigger change, and seeing the symmetry between
`setup_scoreboard()` and `cleanup_scoreboard` makes me feel a bit more
generous towards the change.

> diff --git a/blame.h b/blame.h
> index 3b34be0e5c..75a910bb67 100644
> --- a/blame.h
> +++ b/blame.h
> @@ -180,6 +180,7 @@ void init_scoreboard(struct blame_scoreboard *sb);
>  void setup_scoreboard(struct blame_scoreboard *sb,
>  		      struct blame_origin **orig);
>  void setup_blame_bloom_data(struct blame_scoreboard *sb);
> +/* Release all entry and caller references to origins before clearing the slab. */
>  void cleanup_scoreboard(struct blame_scoreboard *sb);

I don't think this comment adds much. It might have made sense if it
explicitly mentioned that we also release global state, but it doesn't.

> diff --git a/builtin/blame.c b/builtin/blame.c
> index 48d5251c6d..332795d18f 100644
> --- a/builtin/blame.c
> +++ b/builtin/blame.c
> @@ -1323,6 +1323,7 @@ parse_done:
>  cleanup:
>  	for (ent = sb.ent; ent; ) {
>  		struct blame_entry *e = ent->next;
> +		blame_origin_decref(ent->suspect);
>  		free(ent);
>  		ent = e;
>  	}

And I guess that these were previously held alive by the global blame
suspects?

> diff --git a/t/helper/test-repository.c b/t/helper/test-repository.c
> index 9ba94cdffa..245253efbd 100644
> --- a/t/helper/test-repository.c
> +++ b/t/helper/test-repository.c
> @@ -65,11 +67,41 @@ static void test_get_commit_tree_in_graph(const char *gitdir,
>  	repo_clear(&r);
>  }
>  
> +static void test_blame_scoreboard(const char *path)
> +{
> +	struct repository *repo = the_repository;
> +
> +	setup_git_directory(repo);
> +	/* A second setup exposes leaks left by the first scoreboard. */
> +	for (int i = 0; i < 2; i++) {
> +		const char *args[] = { "blame", "HEAD", NULL };
> +		struct blame_scoreboard sb;
> +		struct blame_origin *origin;
> +		struct rev_info revs;
> +
> +		repo_init_revisions(repo, &revs, NULL);
> +		setup_revisions(ARRAY_SIZE(args) - 1, args, &revs, NULL);
> +		init_scoreboard(&sb);
> +		sb.repo = repo;
> +		sb.revs = &revs;
> +		sb.path = path;
> +		setup_scoreboard(&sb, &origin);
> +		blame_origin_decref(origin);
> +		cleanup_scoreboard(&sb);
> +		release_revisions(&revs);
> +	}
> +	repo_clear(repo);
> +}
> +
>  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], "blame-scoreboard")) {
> +		if (argc != 3)
> +			die("usage: test-tool repository blame-scoreboard <path>");
> +		test_blame_scoreboard(argv[2]);
> +	} else if (!strcmp(argv[1], "parse_commit_in_graph")) {
>  		struct object_id oid;
>  		if (argc < 5)
>  			die("not enough arguments");

Instead of adding another helper, can this maybe be turned into a proper
unit test?

Hmm, it does require some test setup. But now that we have an in-memory
object database I wonder whether we can abuse that for our tests and
create a repository that sits purely in memory. It would of course also
require an in-memory refdb, but that's probably not all that hard to do.

In any case, if we had that, then writing unit tests for specific
repository functionality that doesn't require a worktree may become more
feasible.

That being said, it feels a bit like a stretch for this particular
commit, as that likely requires a bunch of work. But I feel like the
boilerplate added here just to exercise a memory leak is way too
excessive. So ultimately, if we cannot trigger this via normal plumbing
commands, I'd lean towards not having tests at all.

Thanks!

Patrick

@gitgitgadget

gitgitgadget Bot commented Dec 1, 2026

Copy link
Copy Markdown

User Patrick Steinhardt <ps@pks.im> has been added to the cc: list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants