-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-129185: Use PyMutex in tracemalloc #129246
Conversation
vstinner
commented
Jan 23, 2025
•
edited by bedevere-app
bot
Loading
edited by bedevere-app
bot
- Issue: Segmentation fault during interpreter finalization in PyTraceMalloc_Untrack #129185
@@ -37,8 +38,8 @@ static int _PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event, | |||
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it | |||
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */ | |||
#define tables_lock _PyRuntime.tracemalloc.tables_lock | |||
#define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1) | |||
#define TABLES_UNLOCK() PyThread_release_lock(tables_lock) | |||
#define TABLES_LOCK() PyMutex_LockFlags(&tables_lock, _Py_LOCK_DONT_DETACH) |
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.
PyMutex_Lock(&tables_lock)
cannot be used "free" allocator hooks which don't acquire the GIL.
Maybe PyMutex_Lock(&tables_lock)
should be preferred in functions which hold the GIL?
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.
I don't think it matters. TABLES_LOCK()
didn't detach the thread state before, no need to now. (In fact, some unpredictable things might happen if another thread takes the GIL and then uses tracemalloc.)
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.
LGTM. We should also consider using _Py_atomic
for some fields (especially tracing
).