Skip to content

Fix pymalloc hook ctx - #1101

Open
kiri11 wants to merge 3 commits into
bloomberg:mainfrom
kiri11:fix-pymalloc-hook-ctx
Open

kiri11 wants to merge 3 commits into
bloomberg:mainfrom
kiri11:fix-pymalloc-hook-ctx

Conversation

@kiri11

@kiri11 kiri11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Issue number: #1095

Describe your changes
PyMem_SetAllocator replaces the allocator struct while holding a lock, but
PyMem_Malloc, PyMem_RawFree and friends read ctx and the function pointer
without it. A thread allocating while the hooks are swapped can therefore call
one of our hooks with the previous allocator's ctx, which is NULL for
CPython's default allocators. Our hooks used ctx to find the original
allocator, so they crashed.

The hooks are now installed with the original allocator's ctx, and each
domain gets its own hook functions, which call the saved original allocator
instead of reading through ctx. Every combination a racing thread can
observe is then valid. Installing and removing the hooks moved into
hooks::registerPymallocHook()/hooks::unregisterPymallocHook(), which check
per domain whether our hooks are already installed.

Testing performed
Ran the reproducer from the issue against main and this branch:

Platform Python Repro main This branch
Linux x86_64 3.14.10t Issue's repro 9/20 crashed 0/20
Linux x86_64 3.15.2t Issue's repro 9/20 crashed 0/20
Linux x86_64 3.14 (GIL) C extension calling PyMem_RawMalloc/PyMem_RawFree without the GIL 17/20 crashed 0/20

The crashes on main were all in pymalloc_free/pymalloc_malloc,
dereferencing a NULL ctx.

Additional context
python/cpython#143143 (comment)

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-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.96970% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.36%. Comparing base (4cd0b09) to head (2960801).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/memray/_memray/hooks.cpp 96.77% 1 Missing ⚠️
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     
Flag Coverage Δ
cpp 92.36% <96.96%> (-0.03%) ⬇️
python_and_cython 92.36% <96.96%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread news/1095.bugfix.rst Resolved
Comment thread src/memray/_memray/hooks.cpp Resolved
Comment thread src/memray/_memray/hooks.cpp Resolved
Comment thread src/memray/_memray/hooks.cpp Resolved
Comment thread src/memray/_memray/hooks.cpp Resolved
Signed-off-by: Kirill Ignatev <kignatev@bloomberg.net>
@kiri11
kiri11 marked this pull request as ready for review November 25, 2026 15:03
@kiri11
kiri11 requested a review from godlygeek November 25, 2026 15:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants