Skip to content

[mypyc] Fix leak of an overridden attribute default - #22480

Merged
p-sawicki merged 2 commits into
python:masterfrom
hnwg:fix-mypyc-overridden-default-leak
Nov 24, 2026
Merged

p-sawicki merged 2 commits into
python:masterfrom
hnwg:fix-mypyc-overridden-default-leak

Conversation

@hnwg

@hnwg hnwg commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #22446.

__mypyc_defaults_setup uses initializer stores, which assume that the attribute is undefined. Hence they don't release an old value. Previously, if a subclass redefined the default for an attribute that had one in its parent, the parent's default was stored and then replaced. This resulted in a leak per instance.

Previously, without separate compilation, find_attr_initializers gathered the defaults of every class in the MRO. So the setup method stored both the parent's and the subclass's defaults. Now there is only one entry per name, for the last default.

With separate=True, the subclass's setup method calls the parent's first. But while the subclass is built, the parent's defaults are not known. So when the setup chains to a parent, the default for an attribute that an ancestor declares is now handled by a regular store. Such a store releases the old value. As separate builds don't treat any attribute as always defined, a NULL check is made before the decref, which is the XDECREF suggested in the review. Attributes that only the subclass declares still use initializer stores.

With the leakmod.py from the issue (built on Linux using CPython 3.13.16), measure.py now shows 0.0 B for all four classes. This is the case both with mypycify(["leakmod.py"]) and with separate=True. At commit 0952029, Overrides and OverridesWithInit print 56.0 B in both modes. I did not test the free-threaded build or the issue's macOS and CPython 3.11 setup.

The test testDefaultVarsOverriddenInSubclass fails on master. There, B.__mypyc_defaults_setup stores x twice. The test testCrossModuleOverriddenAttrDefaultIsReleased_separate fails without the separate-mode change, because the refcount of the parent's default increases with every Child(). In addition, this test overrides an attribute that the parent declares without a default, which covers the NULL case.

…uilds

Without separate compilation, find_attr_initializers collected the
defaults of every class in the MRO, so a subclass that overrides a base
default got two initializer stores in __mypyc_defaults_setup. An
initializer store doesn't release the previous value, so the base
default leaked once per instance. Keep only the last default per name.

@p-sawicki p-sawicki left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good for non-separate builds. I think we could also fix separate builds by emitting XDECREF calls for the default attribute assignments in separate mode. It would make object initialization a little slower in separate mode but it could be avoided by using ClassVar so it seems ok.

Would you like to add this to close the issue with this PR?

…uilds

Under separate compilation, a subclass's __mypyc_defaults_setup calls the
parent's setup first, so the parent's default for an overridden attribute
is already stored. The initializer store that followed didn't release it.

When the setup chains to a parent, store defaults for attributes that an
ancestor declares with a regular store, which releases the old value.
Separate builds don't treat any attribute as always defined, so the store
checks for NULL first.
@hnwg hnwg changed the title [mypyc] Fix leak of an overridden attribute default in non-separate builds [mypyc] Fix leak of an overridden attribute default Nov 24, 2026
@hnwg

hnwg commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Pushed it in f3e360d. If the setup chains to a parent, a default for an inherited attribute is now a normal store, so whatever the parent stored gets decref'd (it's NULL-checked, since separate builds don't mark anything as always defined). Added a run test whose _separate variant fails without it, and changed the title and description to close the issue.

@p-sawicki p-sawicki left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@p-sawicki
p-sawicki merged commit 8674b65 into python:master Nov 24, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[mypyc] Subclass overriding an inherited attribute default leaks the base default on every instantiation

2 participants