Skip to content

Leaks on Python's standard library at runtime shutdown #100911

Closed

Description

@eduardo-elizondo

Problem

Wanted to surface a couple of existing leaks in the standard library that happen at runtime shutdown. Simple statements like import unittest or import itertools already trigger these leaks. In general, these make it much harder to guarantee a correct shutdown, specially in cases where we want to explicitly clean up the entire allocated memory space before the runtime gets reinitialized (i.e in embedded applications).

Repro

Build on debug mode:

git clone https://github.com/python/cpython && cd cpython
./configure --with-pydebug && make -j

By default, the runtime and various imports do not leak:

eelizondo@cpython -> ./python.exe -X showrefcount -c 'import sys'
[0 refs, 0 blocks]

However, a good chunk of imports in the standard library leak. Examples of these include: argparse, itertools, etc. Example:

eelizondo@cpython -> ./python.exe -X showrefcount -c 'import argparse'
[1014 refs, 337 blocks]

eelizondo@cpython -> ./python.exe -X showrefcount -c 'import itertools'
[688 refs, 238 blocks]

Future Prevention

While each of these leaks has to be addressed on its own (and this bug report does not contain a proposed fix), there should probably add stronger guarantees that at least the core runtime and its standard library will not leak. This could start with a test that goes through all imports and guarantees that they won't leak and it can already start with an allowlist of the various libraries that do not leak.

Activity

  1. eduardo-elizondo commented on Jan 10, 2023

    @eduardo-elizondo
    ContributorAuthor
  2. corona10 commented on Jan 10, 2023

    @corona10
    Member
  3. vstinner commented on Apr 5, 2023

    @vstinner
    Member

    However, a good chunk of imports in the standard library leak

    It will work as expected once more extensions will be converted to the multi-phase initialization API. Recent work by @erlend-aasland: commit fee7a99.

  4. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    and removed
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Apr 6, 2023
  5. erlend-aasland commented on Apr 27, 2023

    @erlend-aasland
    Contributor

    Do you have a list of extension modules that leak at shutdown?

  6. vstinner commented on May 4, 2023

    @vstinner
    Member

    Do you have a list of extension modules that leak at shutdown?

    When I modified Python to clear static types and static exceptions, I wrote a patch to list types and exceptions which cannot be deallocated because there are still referenced somehow:
    #90575 (comment)

    In that list, you can see some C extension names:

    • _datetime
    • _ctypes
    • _io
    • _abc
    • itertools
    • collections
    • _pickle
    • _testcapi
    • _asyncio
    • ...

    Some C extensions are already converted to heap types and multi-phase initialization. But this list is not easy to use: you have to identify to "root objects" which keeps all other objects alive indirectly. For example, the _io module still uses static types.

  7. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 25, 2023
  8. kumaraditya303 commented on Jul 22, 2025

    @kumaraditya303
    Contributor

    The leaks are all fixed now as modules were ported to heap types and static types are cleaned up at exit with _PyStaticType_FiniBuiltin.

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

    stdlibStandard Library Python modules in the Lib/ directorytopic-subinterpreterstype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions