Skip to content

Commit a91fc22

Browse files
committed
Fix race condition in _PyTraceMalloc_Stop()
Don't call PyRefTracer_SetTracer() while holding TABLES_LOCK().
1 parent 00c69d2 commit a91fc22

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Python/tracemalloc.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,8 @@ _PyTraceMalloc_Stop(void)
870870
TABLES_LOCK();
871871

872872
if (!tracemalloc_config.tracing) {
873-
goto done;
873+
TABLES_UNLOCK();
874+
return;
874875
}
875876

876877
/* stop tracing Python memory allocations */
@@ -887,10 +888,12 @@ _PyTraceMalloc_Stop(void)
887888
raw_free(tracemalloc_traceback);
888889
tracemalloc_traceback = NULL;
889890

890-
(void)PyRefTracer_SetTracer(NULL, NULL);
891-
892-
done:
893891
TABLES_UNLOCK();
892+
893+
// Call it after TABLES_UNLOCK() since it calls _PyEval_StopTheWorldAll()
894+
// which would lead to a deadlock with TABLES_LOCK() which doesn't release
895+
// the GIL.
896+
(void)PyRefTracer_SetTracer(NULL, NULL);
894897
}
895898

896899

0 commit comments

Comments
 (0)