Skip to content

Commit fc6bc1e

Browse files
authored
gh-129185: Simplify PyTraceMalloc_Track() (#129256)
Since tracemalloc uses PyMutex, it becomes safe to use TABLES_LOCK() even after _PyTraceMalloc_Fini(): remove the "pre-check" in PyTraceMalloc_Track() and PyTraceMalloc_Untrack(). PyTraceMalloc_Untrack() no longer needs to acquire the GIL. _PyTraceMalloc_Fini() can be called earlier during Python finalization.
1 parent 233fd00 commit fc6bc1e

File tree

2 files changed

+3
-26
lines changed

2 files changed

+3
-26
lines changed

Python/pylifecycle.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ _Py_Finalize(_PyRuntimeState *runtime)
21132113

21142114
/* Disable tracemalloc after all Python objects have been destroyed,
21152115
so it is possible to use tracemalloc in objects destructor. */
2116-
_PyTraceMalloc_Stop();
2116+
_PyTraceMalloc_Fini();
21172117

21182118
/* Finalize any remaining import state */
21192119
// XXX Move these up to where finalize_modules() is currently.
@@ -2166,7 +2166,6 @@ _Py_Finalize(_PyRuntimeState *runtime)
21662166

21672167
finalize_interp_clear(tstate);
21682168

2169-
_PyTraceMalloc_Fini();
21702169

21712170
#ifdef Py_TRACE_REFS
21722171
/* Display addresses (& refcnts) of all objects still alive.

Python/tracemalloc.c

+2-24
Original file line numberDiff line numberDiff line change
@@ -1203,17 +1203,9 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12031203
size_t size)
12041204
{
12051205
PyGILState_STATE gil_state = PyGILState_Ensure();
1206-
int result;
1207-
1208-
// gh-129185: Check before TABLES_LOCK() to support calls after
1209-
// _PyTraceMalloc_Fini().
1210-
if (!tracemalloc_config.tracing) {
1211-
result = -2;
1212-
goto done;
1213-
}
1214-
12151206
TABLES_LOCK();
12161207

1208+
int result;
12171209
if (tracemalloc_config.tracing) {
12181210
result = tracemalloc_add_trace_unlocked(domain, ptr, size);
12191211
}
@@ -1223,29 +1215,17 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12231215
}
12241216

12251217
TABLES_UNLOCK();
1226-
done:
12271218
PyGILState_Release(gil_state);
1228-
12291219
return result;
12301220
}
12311221

12321222

12331223
int
12341224
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
12351225
{
1236-
// Need the GIL to prevent races on the first 'tracing' test
1237-
PyGILState_STATE gil_state = PyGILState_Ensure();
1238-
int result;
1239-
1240-
// gh-129185: Check before TABLES_LOCK() to support calls after
1241-
// _PyTraceMalloc_Fini()
1242-
if (!tracemalloc_config.tracing) {
1243-
result = -2;
1244-
goto done;
1245-
}
1246-
12471226
TABLES_LOCK();
12481227

1228+
int result;
12491229
if (tracemalloc_config.tracing) {
12501230
tracemalloc_remove_trace_unlocked(domain, ptr);
12511231
result = 0;
@@ -1256,8 +1236,6 @@ PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
12561236
}
12571237

12581238
TABLES_UNLOCK();
1259-
done:
1260-
PyGILState_Release(gil_state);
12611239
return result;
12621240
}
12631241

0 commit comments

Comments
 (0)