Skip to content

Commit 54c8f9e

Browse files
committed
Call _PyTraceMalloc_Init() at Python startup
So TABLES_LOCK() can be called in _PyMem_DumpTraceback() even if tracemalloc is not tracing.
1 parent 99d27fe commit 54c8f9e

File tree

4 files changed

+15
-31
lines changed

4 files changed

+15
-31
lines changed

Include/internal/pycore_tracemalloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ extern PyObject* _PyTraceMalloc_GetTraces(void);
144144
extern PyObject* _PyTraceMalloc_GetObjectTraceback(PyObject *obj);
145145

146146
/* Initialize tracemalloc */
147-
extern int _PyTraceMalloc_Init(void);
147+
extern PyStatus _PyTraceMalloc_Init(void);
148148

149149
/* Start tracemalloc */
150150
extern int _PyTraceMalloc_Start(int max_nframe);

Modules/_tracemalloc.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,5 @@ PyInit__tracemalloc(void)
223223
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
224224
#endif
225225

226-
if (_PyTraceMalloc_Init() < 0) {
227-
Py_DECREF(m);
228-
return NULL;
229-
}
230-
231226
return m;
232227
}

Python/pylifecycle.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,11 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
706706
return _PyStatus_NO_MEMORY();
707707
}
708708

709+
status = _PyTraceMalloc_Init();
710+
if (_PyStatus_EXCEPTION(status)) {
711+
return status;
712+
}
713+
709714
PyThreadState *tstate = _PyThreadState_New(interp,
710715
_PyThreadState_WHENCE_INIT);
711716
if (tstate == NULL) {

Python/tracemalloc.c

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "pycore_fileutils.h" // _Py_write_noraise()
33
#include "pycore_gc.h" // PyGC_Head
44
#include "pycore_hashtable.h" // _Py_hashtable_t
5+
#include "pycore_initconfig.h" // _PyStatus_NO_MEMORY()
56
#include "pycore_object.h" // _PyType_PreHeaderSize()
67
#include "pycore_pymem.h" // _Py_tracemalloc_config
78
#include "pycore_runtime.h" // _Py_ID()
@@ -815,37 +816,24 @@ tracemalloc_clear_traces_unlocked(void)
815816
}
816817

817818

818-
int
819+
PyStatus
819820
_PyTraceMalloc_Init(void)
820821
{
821-
if (tracemalloc_config.initialized == TRACEMALLOC_FINALIZED) {
822-
PyErr_SetString(PyExc_RuntimeError,
823-
"the tracemalloc module has been unloaded");
824-
return -1;
825-
}
826-
827-
if (tracemalloc_config.initialized == TRACEMALLOC_INITIALIZED)
828-
return 0;
822+
assert(tracemalloc_config.initialized == TRACEMALLOC_NOT_INITIALIZED);
829823

830824
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
831825

832826
#ifdef REENTRANT_THREADLOCAL
833827
if (PyThread_tss_create(&tracemalloc_reentrant_key) != 0) {
834-
#ifdef MS_WINDOWS
835-
PyErr_SetFromWindowsErr(0);
836-
#else
837-
PyErr_SetFromErrno(PyExc_OSError);
838-
#endif
839-
return -1;
828+
return _PyStatus_NO_MEMORY();
840829
}
841830
#endif
842831

843832
#if defined(TRACE_RAW_MALLOC)
844833
if (tables_lock == NULL) {
845834
tables_lock = PyThread_allocate_lock();
846835
if (tables_lock == NULL) {
847-
PyErr_SetString(PyExc_RuntimeError, "cannot allocate lock");
848-
return -1;
836+
return _PyStatus_NO_MEMORY();
849837
}
850838
}
851839
#endif
@@ -862,9 +850,9 @@ _PyTraceMalloc_Init(void)
862850
tracemalloc_domains = tracemalloc_create_domains_table();
863851

864852
if (tracemalloc_filenames == NULL || tracemalloc_tracebacks == NULL
865-
|| tracemalloc_traces == NULL || tracemalloc_domains == NULL) {
866-
PyErr_NoMemory();
867-
return -1;
853+
|| tracemalloc_traces == NULL || tracemalloc_domains == NULL)
854+
{
855+
return _PyStatus_NO_MEMORY();
868856
}
869857

870858
tracemalloc_empty_traceback.nframe = 1;
@@ -875,7 +863,7 @@ _PyTraceMalloc_Init(void)
875863
tracemalloc_empty_traceback.hash = traceback_hash(&tracemalloc_empty_traceback);
876864

877865
tracemalloc_config.initialized = TRACEMALLOC_INITIALIZED;
878-
return 0;
866+
return _PyStatus_OK();
879867
}
880868

881869

@@ -920,10 +908,6 @@ _PyTraceMalloc_Start(int max_nframe)
920908
return -1;
921909
}
922910

923-
if (_PyTraceMalloc_Init() < 0) {
924-
return -1;
925-
}
926-
927911
if (PyRefTracer_SetTracer(_PyTraceMalloc_TraceRef, NULL) < 0) {
928912
return -1;
929913
}

0 commit comments

Comments
 (0)