You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Leaks on Python's standard library at runtime shutdown #100911
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:
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.
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.
Activity
eduardo-elizondo commentedon Jan 10, 2023
cc @ericsnowcurrently
corona10 commentedon Jan 10, 2023
cc @vstinner
vstinner commentedon Apr 5, 2023
It will work as expected once more extensions will be converted to the multi-phase initialization API. Recent work by @erlend-aasland: commit fee7a99.
erlend-aasland commentedon Apr 27, 2023
Do you have a list of extension modules that leak at shutdown?
vstinner commentedon May 4, 2023
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:
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.
kumaraditya303 commentedon Jul 22, 2025
The leaks are all fixed now as modules were ported to heap types and static types are cleaned up at exit with
_PyStaticType_FiniBuiltin.