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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/internal/pycore_tracemalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct _tracemalloc_runtime_state {
PyMemAllocatorEx obj;
} allocators;

PyThread_type_lock tables_lock;
PyMutex tables_lock;
/* Size in bytes of currently traced memory.
Protected by TABLES_LOCK(). */
size_t traced_memory;
Expand Down
17 changes: 3 additions & 14 deletions Python/tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "pycore_gc.h" // PyGC_Head
#include "pycore_hashtable.h" // _Py_hashtable_t
#include "pycore_initconfig.h" // _PyStatus_NO_MEMORY()
#include "pycore_lock.h" // PyMutex_LockFlags()
#include "pycore_object.h" // _PyType_PreHeaderSize()
#include "pycore_pymem.h" // _Py_tracemalloc_config
#include "pycore_runtime.h" // _Py_ID()
Expand Down Expand Up @@ -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.)

#define TABLES_UNLOCK() PyMutex_Unlock(&tables_lock)


#define DEFAULT_DOMAIN 0
Expand Down Expand Up @@ -741,13 +742,6 @@ _PyTraceMalloc_Init(void)
return _PyStatus_NO_MEMORY();
}

if (tables_lock == NULL) {
tables_lock = PyThread_allocate_lock();
if (tables_lock == NULL) {
return _PyStatus_NO_MEMORY();
}
}

tracemalloc_filenames = hashtable_new(hashtable_hash_pyobject,
hashtable_compare_unicode,
tracemalloc_clear_filename, NULL);
Expand Down Expand Up @@ -792,11 +786,6 @@ tracemalloc_deinit(void)
_Py_hashtable_destroy(tracemalloc_tracebacks);
_Py_hashtable_destroy(tracemalloc_filenames);

if (tables_lock != NULL) {
PyThread_free_lock(tables_lock);
tables_lock = NULL;
}

PyThread_tss_delete(&tracemalloc_reentrant_key);
}

Expand Down
Loading