Skip to content

TypeAliasType can leak memory if name is a str subclass #140000

Description

@JelleZijlstra

Bug report

Bug description:

TypeAliasType's traverse implementation does not visit its name. This means that we can leak memory if name is passed an instance of a mutable str subclass.

fromtypingimportTypeAliasTypeimportweakrefimportgcclassS(str):
passdefstill_alive(ref):
obj=ref()
ifnotisinstance(obj, S):
returnFalseT=obj.Tifnotisinstance(T, TypeAliasType):
returnFalsereturnTname=S("name")
T=TypeAliasType(name, int)
name.T=Tref=weakref.ref(name)
assertstill_alive(ref)
delnamedelTgc.collect()
gc.collect()
assertstill_alive(ref)
delref().Tassertnotstill_alive(ref)

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions