Skip to content

[mypyc] Fix leak of an overridden attribute default in non-separate builds - #22480

Open
hnwg wants to merge 1 commit into
python:masterfrom
hnwg:fix-mypyc-overridden-default-leak
Open

hnwg wants to merge 1 commit into
python:masterfrom
hnwg:fix-mypyc-overridden-default-leak

Conversation

@hnwg

@hnwg hnwg commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Part of #22446.

Without separate compilation, find_attr_initializers would collect the defaults for all classes in the MRO. If a subclass overrode a base default, __mypyc_defaults_setup would store both: the base value, then its own. As an initializer store does not release the previous value, this would cause the base default to leak (once per instance). With this change there is only one entry per name, for the last default. Default values from a base class that have been overridden are no longer evaluated at all. When using the leakmod.py from the issue (built by mypyc leakmod.py on Linux with CPython 3.13.16), measure.py now prints 0.0 B for all four classes. At commit 0952029, Overrides and OverridesWithInit print 56.0 B.

When separate=True, overriding an inherited default will still cause a leak. This is because the subclass's setup method calls the base class's setup method, causing the base default to be stored first. To resolve this we need to know the base class's defaults while building the subclass. However, separate compilation doesn't have them before the IR is built. I left this for a follow-up, and the issue remains open.

The new test testDefaultVarsOverriddenInSubclass fails on master. This is because B.__mypyc_defaults_setup stores x twice there.

…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.
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.

1 participant