summaryrefslogtreecommitdiffstats
path: root/Python/tracemalloc.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2025-01-24 13:29:36 (GMT)
committerGitHub <noreply@github.com>2025-01-24 13:29:36 (GMT)
commitfc6bc1e4e30c8995cc4f68e56e70686d84495048 (patch)
tree2dafbcbcdd55b8aa16d177895461343ffecb888a /Python/tracemalloc.c
parent233fd00f0a19d33932e35f2fb6794ecae745b780 (diff)
downloadcpython-fc6bc1e4e30c8995cc4f68e56e70686d84495048.zip
cpython-fc6bc1e4e30c8995cc4f68e56e70686d84495048.tar.gz
cpython-fc6bc1e4e30c8995cc4f68e56e70686d84495048.tar.bz2
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.
Diffstat (limited to 'Python/tracemalloc.c')
-rw-r--r--Python/tracemalloc.c26
1 files changed, 2 insertions, 24 deletions
diff --git a/Python/tracemalloc.c b/Python/tracemalloc.c
index d27c2f9..d69b0eb 100644
--- a/Python/tracemalloc.c
+++ b/Python/tracemalloc.c
@@ -1203,17 +1203,9 @@ 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);
}
@@ -1223,9 +1215,7 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
}
TABLES_UNLOCK();
-done:
PyGILState_Release(gil_state);
-
return result;
}
@@ -1233,19 +1223,9 @@ done:
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;
@@ -1256,8 +1236,6 @@ PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
}
TABLES_UNLOCK();
-done:
- PyGILState_Release(gil_state);
return result;
}