Skip to content

Commit 702ea95

Browse files
fix it
1 parent aa08d9d commit 702ea95

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Python/tracemalloc.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -866,9 +866,6 @@ _PyTraceMalloc_Stop(void)
866866
goto done;
867867
}
868868

869-
/* stop tracing Python memory allocations */
870-
_Py_atomic_store_int_relaxed(&tracemalloc_config.tracing, 0);
871-
872869
/* unregister the hook on memory allocators */
873870
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
874871
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem);
@@ -882,6 +879,9 @@ _PyTraceMalloc_Stop(void)
882879

883880
(void)PyRefTracer_SetTracer(NULL, NULL);
884881

882+
/* stop tracing Python memory allocations */
883+
_Py_atomic_store_int_relaxed(&tracemalloc_config.tracing, 0);
884+
885885
done:
886886
TABLES_UNLOCK();
887887
}
@@ -1213,6 +1213,13 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12131213
}
12141214
PyGILState_STATE gil_state = PyGILState_Ensure();
12151215
TABLES_LOCK();
1216+
// Check again now that we hold the lock
1217+
if (!tracemalloc_config.tracing) {
1218+
TABLES_UNLOCK();
1219+
PyGILState_Release(gil_state);
1220+
/* tracemalloc is not tracing: do nothing */
1221+
return -2;
1222+
}
12161223
int result = tracemalloc_add_trace_unlocked(domain, ptr, size);
12171224
TABLES_UNLOCK();
12181225
PyGILState_Release(gil_state);

0 commit comments

Comments
 (0)