From b5ee79a0f9ab05448d1d557fe8487667a23e7f88 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 30 Sep 2025 14:55:31 +0200 Subject: [PATCH 1/3] 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. --- .../2025-09-30-14-57-19.gh-issue-139116.nlVf40.rst | 2 ++ Python/tracemalloc.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-09-30-14-57-19.gh-issue-139116.nlVf40.rst 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 index 00000000000000..89cba0908a942e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-30-14-57-19.gh-issue-139116.nlVf40.rst @@ -0,0 +1,2 @@ +Prevent a deadlock when multiple threads start, stop and use tracemalloc +simultaneously. diff --git a/Python/tracemalloc.c b/Python/tracemalloc.c index 7066a214f1065b..d946f5b388b7ae 100644 --- a/Python/tracemalloc.c +++ b/Python/tracemalloc.c @@ -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_LockFlags(&tables_lock, _PY_LOCK_DETACH) #define TABLES_UNLOCK() PyMutex_Unlock(&tables_lock) From 955208dc9c90b3544583f8e81ffe8f054280caca Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 30 Sep 2025 15:20:01 +0200 Subject: [PATCH 2/3] ReST link --- .../2025-09-30-14-57-19.gh-issue-139116.nlVf40.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 89cba0908a942e..00692e97c44fb5 100644 --- 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 @@ -1,2 +1,2 @@ -Prevent a deadlock when multiple threads start, stop and use tracemalloc +Prevent a deadlock when multiple threads start, stop and use :mod:`tracemalloc` simultaneously. From add31a7b5e911491c90fc9192de66f75598e559e Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 30 Sep 2025 15:20:33 +0200 Subject: [PATCH 3/3] Use PyMutex_Lock Co-authored-by: Peter Bierma --- Python/tracemalloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/tracemalloc.c b/Python/tracemalloc.c index d946f5b388b7ae..7a0853b6a07f55 100644 --- a/Python/tracemalloc.c +++ b/Python/tracemalloc.c @@ -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_DETACH) +#define TABLES_LOCK() PyMutex_Lock(&tables_lock) #define TABLES_UNLOCK() PyMutex_Unlock(&tables_lock)