GeezerVison is showing github.com — gh-128679: Redesign tracemalloc locking by vstinner · Pull Request #128888 · python/cpython
Conversation
vstinner
commented
Jan 15, 2025
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
- Use TABLES_LOCK() to protect 'tracemalloc_config.tracing'.
- Hold TABLES_LOCK() longer while accessing tables.
- tracemalloc_realloc_gil() and tracemalloc_raw_realloc() no longer remove the trace on reentrant call.
- _PyTraceMalloc_Stop() unregisters _PyTraceMalloc_TraceRef().
- _PyTraceMalloc_GetTraces() sets the reentrant flag.
- tracemalloc_clear_traces_unlocked() sets the reentrant flag.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
* Use TABLES_LOCK() to protect 'tracemalloc_config.tracing'.
* Hold TABLES_LOCK() longer while accessing tables.
* tracemalloc_realloc_gil() and tracemalloc_raw_realloc() no longer
remove the trace on reentrant call.
* _PyTraceMalloc_Stop() unregisters _PyTraceMalloc_TraceRef().
* _PyTraceMalloc_GetTraces() sets the reentrant flag.
* tracemalloc_clear_traces_unlocked() sets the reentrant flag.
_PyTraceMalloc_Stop() unregisters _PyTraceMalloc_TraceRef().
This change cannot be backported to Python 3.12 which doesn't have the PyRefTracer_SetTracer() function.
Anyway, I don't plan to backport this major redesign to Python 3.13 neither.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Don't remove the trace if it's a reentrant call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a much better solution. A few notes:
- I think it's worth trying to migrate away from
PyThread, because it's a lot more difficult to work with than the modern APIs. We can go with PyMutex and _Py_thread_local instead.
- We need to be careful with holding locks while acquiring the GIL, because that can lead to lock-ordering deadlocks.
- I'll need to look into what kind of subinterpreter problems might come up because of
PyGILState_Ensure, as it doesn't work very well. What would happen if e.g. a thread calls PyTraceMalloc_Track, which sets the GILstate for the main interpreter, then enters a different interpreter?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
I think it's worth trying to migrate away from PyThread, because it's a lot more difficult to work with than the modern APIs. We can go with PyMutex and _Py_thread_local instead.
Why not. It can be done afterwards.
We need to be careful with holding locks while acquiring the GIL, because that can lead to lock-ordering deadlocks.
Right. The order is: first get the GIL, and then call TABLES_LOCK().
I'll need to look into what kind of subinterpreter problems might come up because of PyGILState_Ensure, as #123728. What would happen if e.g. a thread calls PyTraceMalloc_Track, which sets the GILstate for the main interpreter, then enters a different interpreter?
This PR doesn't change that. But right, there should be corner cases when sub-interpreters are involved.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
ericsnowcurrently
Awaiting requested review from ericsnowcurrently
ericsnowcurrently is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|