mirror of
https://github.com/python/cpython.git
synced 2025-07-08 03:45:36 +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().
(cherry picked from commit 46c7e13c05
)
This commit is contained in:
parent
d674792ba7
commit
e3b3e01d6a
5 changed files with 78 additions and 14 deletions
|
@ -1319,29 +1319,48 @@ 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 res;
|
||||
if (tracemalloc_config.tracing) {
|
||||
res = tracemalloc_add_trace(domain, ptr, size);
|
||||
result = tracemalloc_add_trace(domain, ptr, size);
|
||||
}
|
||||
else {
|
||||
// gh-128679: tracemalloc.stop() was called by another thread
|
||||
res = -2;
|
||||
result = -2;
|
||||
}
|
||||
|
||||
TABLES_UNLOCK();
|
||||
done:
|
||||
PyGILState_Release(gil_state);
|
||||
return res;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
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(domain, ptr);
|
||||
result = 0;
|
||||
|
@ -1352,6 +1371,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