Skip to content

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

Open
@eduardo-elizondo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions