gh-155725: Remove PyGILState_Ensure usage from tracemalloc - #156409
Merged
Conversation
tracemalloc no longer acquires the GIL nor creates a temporary thread state when tracing memory allocations. A thread with no attached thread state now records the trace with the "<unknown>" traceback instead of attaching a thread state to capture the Python traceback. Threads without a thread state used to pay for a GIL acquisition plus a full thread state creation and destruction on every traced raw allocation, only to record an empty traceback anyway.
Documentation build overview
|
…e attached Store traceback frame filenames as interned NUL terminated UTF-8 strings instead of Python str objects, so that capturing a traceback no longer uses or modifies Python objects. Threads without an attached thread state now capture their Python traceback by walking the frames of the thread state most recently bound to the thread; only threads which never had a thread state record the traceback as "<unknown>".
ZeroIntensity
left a comment
Member
There was a problem hiding this comment.
Thanks, this is a much better approach.
kumaraditya303
requested review from
FFY00 and
ericsnowcurrently
as code owners
August 28, 2026 14:57
gpshead
approved these changes
Aug 30, 2026
gpshead
left a comment
Member
There was a problem hiding this comment.
agreed that this is the right thing to do. maybe mention in the NEWS entry that the printed filename format changes slightly for higher value Unicode, it's a utf-8 value with \xNN escapes rather than using a mix of \xNN \uNNNN and \UNNNNNNNN. in practice it should not be a big deal, its debugging data and all the data is there.
|
Thanks @kumaraditya303 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-156628 is a backport of this pull request to the 3.15 branch. |
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.
tracemalloc no longer acquires the GIL nor creates a temporary thread state when tracing memory allocations.
tracemallocraw-domain allocator hook violatesPYMEM_DOMAIN_RAWcontract #155725