https://github.com/python/cpython/commit/e728b006de984ab11532f5c9cf60c57671ec37ab
commit: e728b006de984ab11532f5c9cf60c57671ec37ab
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-12-22T21:08:07+05:30
summary:

gh-143057: avoid locking in `tracemalloc` C-APIs when it is not enabled 
(#143065)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-12-22-12-03-09.gh-issue-143057.Majsre.rst
M Include/internal/pycore_tracemalloc.h
M Python/tracemalloc.c

diff --git a/Include/internal/pycore_tracemalloc.h 
b/Include/internal/pycore_tracemalloc.h
index 693385f9a46d12..9974ea3c4143fa 100644
--- a/Include/internal/pycore_tracemalloc.h
+++ b/Include/internal/pycore_tracemalloc.h
@@ -21,7 +21,10 @@ struct _PyTraceMalloc_Config {
     } initialized;
 
     /* Is tracemalloc tracing memory allocations?
-       Variable protected by the TABLES_LOCK(). */
+       Variable protected by the TABLES_LOCK() and stored atomically.
+       Atomic store is used so that it can read without locking for the
+       general case of checking if tracemalloc is enabled.
+       */
     int tracing;
 
     /* limit of the number of frames in a traceback, 1 by default.
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-12-22-12-03-09.gh-issue-143057.Majsre.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-12-22-12-03-09.gh-issue-143057.Majsre.rst
new file mode 100644
index 00000000000000..2eac8c1cfdc10c
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-12-22-12-03-09.gh-issue-143057.Majsre.rst
@@ -0,0 +1 @@
+Avoid locking in :c:func:`PyTraceMalloc_Track` and 
:c:func:`PyTraceMalloc_Untrack` when :mod:`tracemalloc` is not enabled.
diff --git a/Python/tracemalloc.c b/Python/tracemalloc.c
index 20351618721c3b..cdd96925d1f27a 100644
--- a/Python/tracemalloc.c
+++ b/Python/tracemalloc.c
@@ -850,7 +850,7 @@ _PyTraceMalloc_Start(int max_nframe)
 
     /* everything is ready: start tracing Python memory allocations */
     TABLES_LOCK();
-    tracemalloc_config.tracing = 1;
+    _Py_atomic_store_int_relaxed(&tracemalloc_config.tracing, 1);
     TABLES_UNLOCK();
 
     return 0;
@@ -867,7 +867,7 @@ _PyTraceMalloc_Stop(void)
     }
 
     /* stop tracing Python memory allocations */
-    tracemalloc_config.tracing = 0;
+    _Py_atomic_store_int_relaxed(&tracemalloc_config.tracing, 0);
 
     /* unregister the hook on memory allocators */
     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
@@ -1207,6 +1207,10 @@ int
 PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
                     size_t size)
 {
+    if (_Py_atomic_load_int_relaxed(&tracemalloc_config.tracing) == 0) {
+        /* tracemalloc is not tracing: do nothing */
+        return -2;
+    }
     PyGILState_STATE gil_state = PyGILState_Ensure();
     TABLES_LOCK();
 
@@ -1228,6 +1232,11 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
 int
 PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
 {
+    if (_Py_atomic_load_int_relaxed(&tracemalloc_config.tracing) == 0) {
+        /* tracemalloc is not tracing: do nothing */
+        return -2;
+    }
+
     TABLES_LOCK();
 
     int result;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to