[3.13] gh-128679: Fix tracemalloc.stop() race conditions (#128897)

tracemalloc_alloc(), tracemalloc_realloc(), PyTraceMalloc_Track(),
PyTraceMalloc_Untrack() and _PyTraceMalloc_TraceRef() now check
tracemalloc_config.tracing after calling TABLES_LOCK().

_PyTraceMalloc_Stop() now protects more code with TABLES_LOCK(),
especially setting tracemalloc_config.tracing to 1.

Add a test using PyTraceMalloc_Track() to test tracemalloc.stop()
race condition.

Call _PyTraceMalloc_Init() at Python startup.
This commit is contained in:
Victor Stinner 2025-01-19 00:39:07 +01:00 committed by GitHub
parent ef9961840b
commit 6b47499510
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 250 additions and 117 deletions

View file

@ -706,6 +706,11 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
return _PyStatus_NO_MEMORY();
}
status = _PyTraceMalloc_Init();
if (_PyStatus_EXCEPTION(status)) {
return status;
}
PyThreadState *tstate = _PyThreadState_New(interp,
_PyThreadState_WHENCE_INIT);
if (tstate == NULL) {