Skip to content

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

@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

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy