]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-111924: Fix data races when swapping allocators (gh-130287)
authorSam Gross <colesbury@gmail.com>
Thu, 20 Feb 2025 16:31:15 +0000 (11:31 -0500)
committerGitHub <noreply@github.com>
Thu, 20 Feb 2025 16:31:15 +0000 (11:31 -0500)
commitca22147547413229a933e3d9cac21cbecf1183fe
treed7b4da0377507121dd38266ce5049d5c7d4cb5f4
parent568db400ff07240a5ed6f263af281405ccaec716
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.
Include/internal/pycore_pymem.h
Objects/obmalloc.c
Python/import.c
Python/initconfig.c
Python/pathconfig.c
Python/pystate.c
Python/sysmodule.c
Tools/tsan/suppressions.txt
Tools/tsan/suppressions_free_threading.txt