Description
Bug report
CPython current temporarily changes PYMEM_DOMAIN_RAW
to the default allocator during initialization and shutdown. The motivation is to ensure that core runtime structures are allocated and freed using the same allocator. However, modifying the current allocator changes global state and is not thread-safe even with the GIL. Other threads may be allocating or freeing objects use PYMEM_DOMAIN_RAW
; they are not required to hold the GIL to call PyMem_RawMalloc
/PyMem_RawFree
.
We can avoid changing global state while still ensuring that we use a consistent allocator during initialization and shutdown.
PyThread_type_lock
Many of the runtime structures are PyThread_type_lock
objects. We can avoid allocation/freeing entirely for these locks by using PyMutex
or PyRawMutex
instead.
Lines 396 to 418 in b9f814c
Calls to PyMem_RawMalloc
, PyMem_RawCalloc
, PyMem_RawFree
, etc.
For the other calls to PyMem_RawMalloc
, etc. where we know we want to use the default allocator, we should directly call a new internal-only function that always uses the default allocator. This will avoid unnecessarily modifying global state.
For example, we can add a new function _PyMem_DefaultRawMalloc
that behaves like PyMem_RawMalloc
, except that it is not modifiable by _PyMem_SetDefaultAllocator
.
For an example implementation in the nogil-3.12 fork, see colesbury/nogil-3.12@d13c63dee9.