Skip to content

gh-155725: Remove PyGILState_Ensure usage from tracemalloc - #156409

Open
kumaraditya303 wants to merge 3 commits into
python:mainfrom
kumaraditya303:tracemalloc
Open

gh-155725: Remove PyGILState_Ensure usage from tracemalloc#156409
kumaraditya303 wants to merge 3 commits into
python:mainfrom
kumaraditya303:tracemalloc

Conversation

@kumaraditya303

@kumaraditya303 kumaraditya303 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

tracemalloc no longer acquires the GIL nor creates a temporary thread state when tracing memory allocations.

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.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34238669 | 📁 Comparing 69d37ec against main (7f0ccd6)

  🔍 Preview build  

2 files changed
± c-api/memory.html
± whatsnew/changelog.html

…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 ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a much better approach.

Comment thread Python/tracemalloc.c
Comment thread Python/tracemalloc.c
Comment on lines +952 to +954
filename_obj = PyUnicode_DecodeUTF8(filename,
(Py_ssize_t)strlen(filename),
"surrogatepass");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can call arbitrary Python code through the codec error handler, meaning this can encounter a re-entrancy deadlock with the tables lock. I'm okay with not fixing that since there's no reason an error handler should be invoking tracemalloc, but it's probably worth adding a comment.

Comment thread Python/tracemalloc.c
Comment on lines +420 to +423
if (_PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL) {
// non-attached thread states can be cleared during finalization
return tracemalloc_empty_traceback;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a subtle race condition here. The main thread can clear thread states after this check has passed.

You could try one of the new PyInterpreterView/PyInterpreterGuard APIs for this.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants