From ca22147547413229a933e3d9cac21cbecf1183fe Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 20 Feb 2025 11:31:15 -0500 Subject: gh-111924: Fix data races when swapping allocators (gh-130287) 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. This adds new internal-only functions like `_PyMem_DefaultRawMalloc` that aren't affected by calls to `PyMem_SetAllocator()`, so they're appropriate for Python runtime initialization and finalization. Use these calls in places where we previously swapped to the default raw allocator. --- Python/pystate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/pystate.c') diff --git a/Python/pystate.c b/Python/pystate.c index 4caef2260ae..09b83cdeb1f 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -16,7 +16,7 @@ #include "pycore_parking_lot.h" // _PyParkingLot_AfterFork() #include "pycore_pyerrors.h" // _PyErr_Clear() #include "pycore_pylifecycle.h" // _PyAST_Fini() -#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator() +#include "pycore_pymem.h" // _PyMem_DebugEnabled() #include "pycore_pystate.h" #include "pycore_runtime_init.h" // _PyRuntimeState_INIT #include "pycore_stackref.h" // Py_STACKREF_DEBUG -- cgit v1.2.3