python/cpython · warning

tracemalloc:

Error message

tracemalloc: 

What it means

This is the 'tracemalloc: ' prefix of tracemalloc_error(), a diagnostic helper in Python/tracemalloc.c that only compiles when TRACE_DEBUG is defined (it is not present in release or even normal --with-trace-refs builds). Messages prefixed with it report internal tracemalloc assertion/consistency problems (e.g. hash table corruption, unbalanced remove/get calls) while debugging the allocator tracer itself.

Source

Thrown at Python/tracemalloc.c:85

#define tracemalloc_traced_memory _PyRuntime.tracemalloc.traced_memory
#define tracemalloc_peak_traced_memory _PyRuntime.tracemalloc.peak_traced_memory
#define tracemalloc_filenames _PyRuntime.tracemalloc.filenames
#define tracemalloc_traceback _PyRuntime.tracemalloc.traceback
#define tracemalloc_tracebacks _PyRuntime.tracemalloc.tracebacks
#define tracemalloc_traces _PyRuntime.tracemalloc.traces
#define tracemalloc_domains _PyRuntime.tracemalloc.domains


#ifdef TRACE_DEBUG
static void
tracemalloc_error(const char *format, ...)
{
    va_list ap;
    fprintf(stderr, "tracemalloc: ");
    va_start(ap, format);
    vfprintf(stderr, format, ap);
    va_end(ap);
    fprintf(stderr, "\n");
    fflush(stderr);
}
#endif


#define tracemalloc_reentrant_key _PyRuntime.tracemalloc.reentrant_key

/* Any non-NULL pointer can be used */
#define REENTRANT Py_True

static int
get_reentrant(void)
{
    assert(PyThread_tss_is_created(&tracemalloc_reentrant_key));

    void *ptr = PyThread_tss_get(&tracemalloc_reentrant_key);
    if (ptr != NULL) {
        assert(ptr == REENTRANT);

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. If you are not developing CPython itself, rebuild without -DTRACE_DEBUG
  2. For core dev: reproduce under a debugger, note the exact message after the prefix, and inspect the tracemalloc hash table state at the failure site
  3. Check for memory corruption from third-party C extensions corrupting tracemalloc's tables
Defensive patterns

Strategy: fallback

Prevention

When it happens

Trigger: Building CPython with -DTRACE_DEBUG and running with tracemalloc active (PYTHONTRACEMALLOC or -X tracemalloc); any internal invariant failure inside tracemalloc's hash table or filter logic prints via this helper.

Common situations: CPython core development debugging tracemalloc itself; custom patches to tracemalloc that broke invariants; almost never seen by application developers because the macro is undefined by default.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/706c0e4d108b95a6. Report an issue: GitHub.