Skip to content

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

Open

Description

@arthurlaquieze

Bug Report

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.

# leakmod.py
class Base:
    KINDS: tuple[str, ...] = ("STRUCT", "VALUE")

    def __init__(self) -> None:
        self.x = 1


class Inherits(Base):
    pass


class Overrides(Base):
    KINDS: tuple[str, ...] = ()


class OverridesWithInit(Base):
    KINDS: tuple[str, ...] = ()

    def __init__(self) -> None:
        super().__init__()
# measure.py
import gc
import tracemalloc

import leakmod

for cls in (leakmod.Base, leakmod.Inherits, leakmod.Overrides, leakmod.OverridesWithInit):
    for _ in range(100):
        cls()
    gc.collect()
    tracemalloc.start()
    before = tracemalloc.get_traced_memory()[0]
    for _ in range(10_000):
        cls()
    gc.collect()
    after = tracemalloc.get_traced_memory()[0]
    tracemalloc.stop()
    print(f"{cls.__name__:18s} {(after - before) / 10_000:5.1f} B leaked per instance")

Expected Behavior

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:

char CPyDef_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 */
    return 1;
}

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      Sponsor
      SponsoredKunjungi sekarang
      Promo