Skip to content

Commit 01157e0

Browse files
gh-139116: tracemalloc: Detach thread state when acquiring tables_lock (GH-139449)
* 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 <[email protected]>
1 parent b2f5ad0 commit 01157e0

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Prevent a deadlock when multiple threads start, stop and use :mod:`tracemalloc`
2+
simultaneously.

Python/tracemalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static int _PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event,
3636
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
3737
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
3838
#define tables_lock _PyRuntime.tracemalloc.tables_lock
39-
#define TABLES_LOCK() PyMutex_LockFlags(&tables_lock, _Py_LOCK_DONT_DETACH)
39+
#define TABLES_LOCK() PyMutex_Lock(&tables_lock)
4040
#define TABLES_UNLOCK() PyMutex_Unlock(&tables_lock)
4141

4242

0 commit comments

Comments
 (0)