Repository navigation
Conversation
PyMem_SetAllocator replaces the allocator struct while other threads can read it without a lock, so a thread can pair one of our hooks with the previous allocator's ctx, which is NULL for CPython's defaults. Our hooks used ctx to find the original allocator and crashed. Any thread that allocates while tracking starts or stops can hit this, and it is easiest to hit on free-threaded builds. Install the hooks with the original ctx and look up the original allocators per domain instead, so every combination a thread can observe is valid. Signed-off-by: Kirill Ignatev <kignatev@bloomberg.net>
Signed-off-by: Kirill Ignatev <kignatev@bloomberg.net>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1101 +/- ##
==========================================
- Coverage 92.38% 92.36% -0.03%
==========================================
Files 103 103
Lines 13428 13445 +17
Branches 479 480 +1
==========================================
+ Hits 12406 12418 +12
- Misses 1022 1027 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
godlygeek
requested changes
Nov 24, 2026
Signed-off-by: Kirill Ignatev <kignatev@bloomberg.net>
kiri11
marked this pull request as ready for review
November 25, 2026 15:03
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue number: #1095
Describe your changes
PyMem_SetAllocatorreplaces the allocator struct while holding a lock, butPyMem_Malloc,PyMem_RawFreeand friends readctxand the function pointerwithout it. A thread allocating while the hooks are swapped can therefore call
one of our hooks with the previous allocator's
ctx, which isNULLforCPython's default allocators. Our hooks used
ctxto find the originalallocator, so they crashed.
The hooks are now installed with the original allocator's
ctx, and eachdomain gets its own hook functions, which call the saved original allocator
instead of reading through
ctx. Every combination a racing thread canobserve is then valid. Installing and removing the hooks moved into
hooks::registerPymallocHook()/hooks::unregisterPymallocHook(), which checkper domain whether our hooks are already installed.
Testing performed
Ran the reproducer from the issue against
mainand this branch:mainPyMem_RawMalloc/PyMem_RawFreewithout the GILThe crashes on
mainwere all inpymalloc_free/pymalloc_malloc,dereferencing a
NULLctx.Additional context
python/cpython#143143 (comment)