Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
eduardo-elizondo opened this issue Jan 10, 2023 · 5 comments
Open

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

eduardo-elizondo opened this issue Jan 10, 2023 · 5 comments
Labels
stdlib Python modules in the Lib dir topic-subinterpreters type-bug An unexpected behavior, bug, or error

Comments

@eduardo-elizondo
Copy link
Contributor

eduardo-elizondo commented Jan 10, 2023

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.

@eduardo-elizondo eduardo-elizondo added the type-bug An unexpected behavior, bug, or error label Jan 10, 2023
@eduardo-elizondo
Copy link
Contributor Author

cc @ericsnowcurrently

@corona10
Copy link
Member

cc @vstinner

@vstinner
Copy link
Member

vstinner commented Apr 5, 2023

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.

@arhadthedev arhadthedev added interpreter-core (Objects, Python, Grammar, and Parser dirs) and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Apr 6, 2023
@erlend-aasland
Copy link
Contributor

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

@vstinner
Copy link
Member

vstinner commented May 4, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-subinterpreters type-bug An unexpected behavior, bug, or error
Projects
Status: Todo
Development

No branches or pull requests

7 participants