mirror of
https://github.com/python/cpython.git
synced 2025-08-31 22:18:28 +00:00
gh-129185: Fix PyTraceMalloc_Untrack() at Python exit (#129191)
Support calling PyTraceMalloc_Track() and PyTraceMalloc_Untrack() during late Python finalization. * Call _PyTraceMalloc_Fini() later in Python finalization. * Test also PyTraceMalloc_Untrack() without the GIL * PyTraceMalloc_Untrack() now gets the GIL. * Test also PyTraceMalloc_Untrack() in test_tracemalloc_track_race().
This commit is contained in:
parent
225296cd5b
commit
46c7e13c05
5 changed files with 75 additions and 11 deletions
|
@ -2113,7 +2113,7 @@ _Py_Finalize(_PyRuntimeState *runtime)
|
|||
|
||||
/* Disable tracemalloc after all Python objects have been destroyed,
|
||||
so it is possible to use tracemalloc in objects destructor. */
|
||||
_PyTraceMalloc_Fini();
|
||||
_PyTraceMalloc_Stop();
|
||||
|
||||
/* Finalize any remaining import state */
|
||||
// XXX Move these up to where finalize_modules() is currently.
|
||||
|
@ -2166,6 +2166,8 @@ _Py_Finalize(_PyRuntimeState *runtime)
|
|||
|
||||
finalize_interp_clear(tstate);
|
||||
|
||||
_PyTraceMalloc_Fini();
|
||||
|
||||
#ifdef Py_TRACE_REFS
|
||||
/* Display addresses (& refcnts) of all objects still alive.
|
||||
* An address can be used to find the repr of the object, printed
|
||||
|
|
|
@ -1256,9 +1256,17 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
|
|||
size_t size)
|
||||
{
|
||||
PyGILState_STATE gil_state = PyGILState_Ensure();
|
||||
int result;
|
||||
|
||||
// gh-129185: Check before TABLES_LOCK() to support calls after
|
||||
// _PyTraceMalloc_Fini().
|
||||
if (!tracemalloc_config.tracing) {
|
||||
result = -2;
|
||||
goto done;
|
||||
}
|
||||
|
||||
TABLES_LOCK();
|
||||
|
||||
int result;
|
||||
if (tracemalloc_config.tracing) {
|
||||
result = tracemalloc_add_trace_unlocked(domain, ptr, size);
|
||||
}
|
||||
|
@ -1268,6 +1276,7 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
|
|||
}
|
||||
|
||||
TABLES_UNLOCK();
|
||||
done:
|
||||
PyGILState_Release(gil_state);
|
||||
|
||||
return result;
|
||||
|
@ -1277,9 +1286,19 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
|
|||
int
|
||||
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
|
||||
{
|
||||
// Need the GIL to prevent races on the first 'tracing' test
|
||||
PyGILState_STATE gil_state = PyGILState_Ensure();
|
||||
int result;
|
||||
|
||||
// gh-129185: Check before TABLES_LOCK() to support calls after
|
||||
// _PyTraceMalloc_Fini()
|
||||
if (!tracemalloc_config.tracing) {
|
||||
result = -2;
|
||||
goto done;
|
||||
}
|
||||
|
||||
TABLES_LOCK();
|
||||
|
||||
int result;
|
||||
if (tracemalloc_config.tracing) {
|
||||
tracemalloc_remove_trace_unlocked(domain, ptr);
|
||||
result = 0;
|
||||
|
@ -1290,6 +1309,8 @@ PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
|
|||
}
|
||||
|
||||
TABLES_UNLOCK();
|
||||
done:
|
||||
PyGILState_Release(gil_state);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue