]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-139116: tracemalloc: Detach thread state when acquiring tables_lock (GH-139449)
authorPetr Viktorin <encukou@gmail.com>
Tue, 30 Sep 2025 14:43:49 +0000 (16:43 +0200)
committerGitHub <noreply@github.com>
Tue, 30 Sep 2025 14:43:49 +0000 (16:43 +0200)
* gh-139116: tracemalloc: Detach thread state when acquiring tables_lock

This prevents a deadlock when:

- One thread is in `_PyTraceMalloc_Stop`, with `TABLES_LOCK` held, calling
  `PyRefTracer_SetTracer` which wants to stop the world
- Another is thread in `PyTraceMalloc_Track`, just attached thread state, waiting
  for `TABLES_LOCK`

Detaching the thread state while waiting for `TABLES_LOCK` allows
`PyRefTracer_SetTracer` to stop the world.

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Misc/NEWS.d/next/Core_and_Builtins/2025-09-30-14-57-19.gh-issue-139116.nlVf40.rst [new file with mode: 0644]
Python/tracemalloc.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-30-14-57-19.gh-issue-139116.nlVf40.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-30-14-57-19.gh-issue-139116.nlVf40.rst
new file mode 100644 (file)
index 0000000..00692e9
--- /dev/null
@@ -0,0 +1,2 @@
+Prevent a deadlock when multiple threads start, stop and use :mod:`tracemalloc`
+simultaneously.
index 7066a214f1065bac435ab70fa94026acfa160b9c..7a0853b6a07f550294c75c2b9be2c7b86fbb6a0b 100644 (file)
@@ -36,7 +36,7 @@ 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() PyMutex_LockFlags(&tables_lock, _Py_LOCK_DONT_DETACH)
+#define TABLES_LOCK() PyMutex_Lock(&tables_lock)
 #define TABLES_UNLOCK() PyMutex_Unlock(&tables_lock)