Skip to content
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

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jan 23, 2025

@vstinner
Copy link
Member Author

cc @ZeroIntensity

@@ -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)
Copy link
Member Author

@vstinner vstinner Jan 23, 2025

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?

Copy link
Member

@ZeroIntensity ZeroIntensity Jan 23, 2025

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.)

Copy link
Member

@ZeroIntensity ZeroIntensity left a 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).

@vstinner vstinner merged commit c005ea4 into python:main Jan 24, 2025
44 checks passed
@vstinner vstinner deleted the tracemalloc_pymutex branch January 24, 2025 10:25
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