mirror of
https://github.com/python/cpython.git
synced 2025-08-23 02:04:56 +00:00
gh-129185: Simplify PyTraceMalloc_Track() (#129256)
Since tracemalloc uses PyMutex, it becomes safe to use TABLES_LOCK() even after _PyTraceMalloc_Fini(): remove the "pre-check" in PyTraceMalloc_Track() and PyTraceMalloc_Untrack(). PyTraceMalloc_Untrack() no longer needs to acquire the GIL. _PyTraceMalloc_Fini() can be called earlier during Python finalization.
This commit is contained in:
parent
233fd00f0a
commit
fc6bc1e4e3
2 changed files with 3 additions and 26 deletions
|
@ -2113,7 +2113,7 @@ _Py_Finalize(_PyRuntimeState *runtime)
|
||||||
|
|
||||||
/* Disable tracemalloc after all Python objects have been destroyed,
|
/* Disable tracemalloc after all Python objects have been destroyed,
|
||||||
so it is possible to use tracemalloc in objects destructor. */
|
so it is possible to use tracemalloc in objects destructor. */
|
||||||
_PyTraceMalloc_Stop();
|
_PyTraceMalloc_Fini();
|
||||||
|
|
||||||
/* Finalize any remaining import state */
|
/* Finalize any remaining import state */
|
||||||
// XXX Move these up to where finalize_modules() is currently.
|
// XXX Move these up to where finalize_modules() is currently.
|
||||||
|
@ -2166,7 +2166,6 @@ _Py_Finalize(_PyRuntimeState *runtime)
|
||||||
|
|
||||||
finalize_interp_clear(tstate);
|
finalize_interp_clear(tstate);
|
||||||
|
|
||||||
_PyTraceMalloc_Fini();
|
|
||||||
|
|
||||||
#ifdef Py_TRACE_REFS
|
#ifdef Py_TRACE_REFS
|
||||||
/* Display addresses (& refcnts) of all objects still alive.
|
/* Display addresses (& refcnts) of all objects still alive.
|
||||||
|
|
|
@ -1203,17 +1203,9 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
|
||||||
size_t size)
|
size_t size)
|
||||||
{
|
{
|
||||||
PyGILState_STATE gil_state = PyGILState_Ensure();
|
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();
|
TABLES_LOCK();
|
||||||
|
|
||||||
|
int result;
|
||||||
if (tracemalloc_config.tracing) {
|
if (tracemalloc_config.tracing) {
|
||||||
result = tracemalloc_add_trace_unlocked(domain, ptr, size);
|
result = tracemalloc_add_trace_unlocked(domain, ptr, size);
|
||||||
}
|
}
|
||||||
|
@ -1223,9 +1215,7 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
|
||||||
}
|
}
|
||||||
|
|
||||||
TABLES_UNLOCK();
|
TABLES_UNLOCK();
|
||||||
done:
|
|
||||||
PyGILState_Release(gil_state);
|
PyGILState_Release(gil_state);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1233,19 +1223,9 @@ done:
|
||||||
int
|
int
|
||||||
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
|
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();
|
TABLES_LOCK();
|
||||||
|
|
||||||
|
int result;
|
||||||
if (tracemalloc_config.tracing) {
|
if (tracemalloc_config.tracing) {
|
||||||
tracemalloc_remove_trace_unlocked(domain, ptr);
|
tracemalloc_remove_trace_unlocked(domain, ptr);
|
||||||
result = 0;
|
result = 0;
|
||||||
|
@ -1256,8 +1236,6 @@ PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
TABLES_UNLOCK();
|
TABLES_UNLOCK();
|
||||||
done:
|
|
||||||
PyGILState_Release(gil_state);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue