Skip to content

Commit 555c70d

Browse files
committed
Get the GIL in Untrack
1 parent 31bfe5b commit 555c70d

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Python/tracemalloc.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,12 +1256,13 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12561256
size_t size)
12571257
{
12581258
PyGILState_STATE gil_state = PyGILState_Ensure();
1259+
int result;
1260+
12591261
// gh-129185: Check before TABLES_LOCK() to support calls after
12601262
// _PyTraceMalloc_Fini().
1261-
int result;
12621263
if (!tracemalloc_config.tracing) {
12631264
result = -2;
1264-
goto unlock_gil;
1265+
goto done;
12651266
}
12661267

12671268
TABLES_LOCK();
@@ -1275,7 +1276,7 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12751276
}
12761277

12771278
TABLES_UNLOCK();
1278-
unlock_gil:
1279+
done:
12791280
PyGILState_Release(gil_state);
12801281

12811282
return result;
@@ -1285,16 +1286,19 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12851286
int
12861287
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
12871288
{
1289+
// Need the GIL to prevent races on the first 'tracing' test
1290+
PyGILState_STATE gil_state = PyGILState_Ensure();
1291+
int result;
1292+
12881293
// 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.
1294+
// _PyTraceMalloc_Fini()
12911295
if (!tracemalloc_config.tracing) {
1292-
return -2;
1296+
result = -2;
1297+
goto done;
12931298
}
12941299

12951300
TABLES_LOCK();
12961301

1297-
int result;
12981302
if (tracemalloc_config.tracing) {
12991303
tracemalloc_remove_trace_unlocked(domain, ptr);
13001304
result = 0;
@@ -1305,6 +1309,8 @@ PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
13051309
}
13061310

13071311
TABLES_UNLOCK();
1312+
done:
1313+
PyGILState_Release(gil_state);
13081314
return result;
13091315
}
13101316

0 commit comments

Comments
 (0)