@@ -850,7 +850,7 @@ _PyTraceMalloc_Start(int max_nframe)
850850
851851 /* everything is ready: start tracing Python memory allocations */
852852 TABLES_LOCK ();
853- tracemalloc_config .tracing = 1 ;
853+ _Py_atomic_store_int_relaxed ( & tracemalloc_config .tracing , 1 ) ;
854854 TABLES_UNLOCK ();
855855
856856 return 0 ;
@@ -867,7 +867,7 @@ _PyTraceMalloc_Stop(void)
867867 }
868868
869869 /* stop tracing Python memory allocations */
870- tracemalloc_config .tracing = 0 ;
870+ _Py_atomic_store_int_relaxed ( & tracemalloc_config .tracing , 0 ) ;
871871
872872 /* unregister the hook on memory allocators */
873873 PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & allocators .raw );
@@ -1207,6 +1207,10 @@ int
12071207PyTraceMalloc_Track (unsigned int domain , uintptr_t ptr ,
12081208 size_t size )
12091209{
1210+ if (_Py_atomic_load_int_relaxed (& tracemalloc_config .tracing ) == 0 ) {
1211+ /* tracemalloc is not tracing: do nothing */
1212+ return -2 ;
1213+ }
12101214 PyGILState_STATE gil_state = PyGILState_Ensure ();
12111215 TABLES_LOCK ();
12121216
@@ -1228,6 +1232,11 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
12281232int
12291233PyTraceMalloc_Untrack (unsigned int domain , uintptr_t ptr )
12301234{
1235+ if (_Py_atomic_load_int_relaxed (& tracemalloc_config .tracing ) == 0 ) {
1236+ /* tracemalloc is not tracing: do nothing */
1237+ return -2 ;
1238+ }
1239+
12311240 TABLES_LOCK ();
12321241
12331242 int result ;
0 commit comments