You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[mypyc] Subclass overriding an inherited attribute default leaks the base default on every instantiation #22446
A native subclass that overrides an attribute default declared on its native base class leaks one object per
instance: the base class's default value. The same code interpreted leaks nothing.
Found through sqlglot's compiled build, sqlglot[c], which hits it on every .sql(dialect=...) call for most dialects. See this issue on sqlglot's repo.
To Reproduce
Compile with mypyc leakmod.py, then run measure.py.
All four classes print 0.0 B, as they do when leakmod.py runs interpreted. A subclass should either skip the
base initializer for an attribute it overrides, or release the previous value before the second store.
Actual Behavior
Compiled output:
Base 0.0 B leaked per instance
Inherits 0.0 B leaked per instance
Overrides 56.0 B leaked per instance
OverridesWithInit 56.0 B leaked per instance
The generated __mypyc_defaults_setup of Overrides first stores the base default (a fresh 2-tuple, 56 bytes
with its GC header), then overwrites the slot with the subclass default without a CPy_DECREF of the first
value:
charCPyDef_Overrides_____mypyc_defaults_setup(PyObject*cpy_r___mypyc_self__) {
...
cpy_r_r3=PyTuple_New(2);
...
((leakmod___OverridesObject*)cpy_r___mypyc_self__)->_KINDS=cpy_r_r3; /* base default */cpy_r_r4.empty_struct_error_flag=0;
cpy_r_r5=CPyTuple_LoadEmptyTupleConstant();
((leakmod___OverridesObject*)cpy_r___mypyc_self__)->_KINDS=cpy_r_r5; /* override, r3 never released */return1;
}
Impact in the wild: in a long-running service rendering large SQL statements with sqlglot[c], the leaked tuples
most likely keep pymalloc arenas from being released, and RSS grew by about 2 GiB a day per process group.
Workaround: declare the attribute as ClassVar[...], which compiles to a class attribute and does not leak.
Your Environment
Mypy version used: 2.4.1 and 2.4.0 (PyPI), and master 332915e (2.5.0+dev, 2026-11-12), all leak
Mypy command-line flags: none (mypyc leakmod.py)
Mypy configuration options from mypy.ini (and other config files): none
Python version used: CPython 3.11.13 (also leaks 64 B per instance on 3.14), macOS arm64
Activity