Repository navigation
[mypyc] Fix leak of an overridden attribute default - #22480
Merged
Merged
Conversation
…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
reviewed
Nov 23, 2026
p-sawicki
left a comment
Collaborator
There was a problem hiding this comment.
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.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22446.
__mypyc_defaults_setupuses 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_initializersgathered 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 theXDECREFsuggested in the review. Attributes that only the subclass declares still use initializer stores.With the
leakmod.pyfrom the issue (built on Linux using CPython 3.13.16),measure.pynow shows 0.0 B for all four classes. This is the case both withmypycify(["leakmod.py"])and withseparate=True. At commit 0952029,OverridesandOverridesWithInitprint 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
testDefaultVarsOverriddenInSubclassfails on master. There,B.__mypyc_defaults_setupstoresxtwice. The testtestCrossModuleOverriddenAttrDefaultIsReleased_separatefails without the separate-mode change, because the refcount of the parent's default increases with everyChild(). In addition, this test overrides an attribute that the parent declares without a default, which covers the NULL case.