Skip to content

Commit d510567

Browse files
committed
PyTraceMalloc_Track() checks tracing with the GIL
1 parent f3ec095 commit d510567

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Python/tracemalloc.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,15 +1255,17 @@ int
12551255
PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12561256
size_t size)
12571257
{
1258-
// gh-129185: Pre-check to support calls after _PyTraceMalloc_Fini()
1258+
PyGILState_STATE gil_state = PyGILState_Ensure();
1259+
// gh-129185: Check before TABLES_LOCK() to support calls after
1260+
// _PyTraceMalloc_Fini().
1261+
int result;
12591262
if (!tracemalloc_config.tracing) {
1260-
return -2;
1263+
result = -2;
1264+
goto unlock_gil;
12611265
}
12621266

1263-
PyGILState_STATE gil_state = PyGILState_Ensure();
12641267
TABLES_LOCK();
12651268

1266-
int result;
12671269
if (tracemalloc_config.tracing) {
12681270
result = tracemalloc_add_trace_unlocked(domain, ptr, size);
12691271
}
@@ -1273,6 +1275,7 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12731275
}
12741276

12751277
TABLES_UNLOCK();
1278+
unlock_gil:
12761279
PyGILState_Release(gil_state);
12771280

12781281
return result;
@@ -1282,7 +1285,9 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12821285
int
12831286
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
12841287
{
1285-
// gh-129185: Pre-check to support calls after _PyTraceMalloc_Fini()
1288+
// gh-129185: Check before TABLES_LOCK() to support calls after
1289+
// _PyTraceMalloc_Fini(). This check is prone to race if another thread
1290+
// calls _PyTraceMalloc_Fini() in parallel.
12861291
if (!tracemalloc_config.tracing) {
12871292
return -2;
12881293
}

0 commit comments

Comments
 (0)