GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — TypeAliasType can leak memory if name is a str subclass · Issue #140000 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
TypeAliasType can leak memory if name is a str subclass#140000
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@JelleZijlstra
JelleZijlstra
opened on Oct 12, 2025
Issue body actions

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.

from typing import TypeAliasType
import weakref
import gc

class S(str):
    pass

def still_alive(ref):
    obj = ref()
    if not isinstance(obj, S):
        return False
    T = obj.T
    if not isinstance(T, TypeAliasType):
        return False
    return T

name = S("name")
T = TypeAliasType(name, int)
name.T = T
ref = weakref.ref(name)
assert still_alive(ref)
del name
del T
gc.collect()
gc.collect()
assert still_alive(ref)

del ref().T
assert not still_alive(ref)

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

  • gh-140000: Traverse name attribute for TypeVar, TypeVarTuple, TypeAliasType, ParamSpec #140016
  • [3.14] gh-140000: Traverse name attribute for TypeVar, TypeVarTuple, TypeAliasType, ParamSpec (GH-140016) #140063
  • [3.13] gh-140000: Traverse name attribute for TypeVar, TypeVarTuple, TypeAliasType, ParamSpec (GH-140016) #140073

Activity

  1. JelleZijlstra
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 12, 2025
  2. JelleZijlstra commented on Oct 12, 2025

    @JelleZijlstra
    JelleZijlstra
    on Oct 12, 2025
    MemberAuthor
    More actions

    This also affects TypeVar, TypeVarTuple, and ParamSpec.

  3. picnixz
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-typing
    on Oct 12, 2025
  4. efimov-mikhail commented on Oct 12, 2025

    @efimov-mikhail
    efimov-mikhail
    on Oct 12, 2025
    Member
    More actions

    Leak for all 4 types is confirmed on main, 3.14 and 3.13 branches. PR with fix is submitted.

    Reacted by Sergey Miryanov
  5. JelleZijlstra
    added a commit that references this issue on Oct 13, 2025

    gh-140000: Traverse `name` attribute for `TypeVar`, `TypeVarTuple`, `…

    be60e4b
  6. miss-islington
    added a commit that references this issue on Oct 13, 2025

    pythongh-140000: Traverse `name` attribute for `TypeVar`, `TypeVarTup…

    6e92029
  7. JelleZijlstra
    added 2 commits that reference this issue on Oct 13, 2025

    [3.14] gh-140000: Traverse `name` attribute for `TypeVar`, `TypeVarTu…

    7a13953

    [3.13] gh-140000: Traverse `name` attribute for `TypeVar`, `TypeVarTu…

    cbcee9a
  8. JelleZijlstra
    closed this as completedon Oct 14, 2025
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

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.