Skip to content

_tracemalloc__get_object_traceback doesn't handle objects with pre-headers correctly #101430

Closed

Description

@colesbury

In the main branch, the function _tracemalloc__get_object_traceback doesn't account for objects with preheaders:

cpython/Modules/_tracemalloc.c

Lines 1406 to 1414 in ea23271

type = Py_TYPE(obj);
if (PyType_IS_GC(type)) {
ptr = (void *)((char *)obj - sizeof(PyGC_Head));
}
else {
ptr = (void *)obj;
}
traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr);

This means that when running with tracemalloc some allocation tracebacks are missing:

import warnings

class MyClass:
    def __del__(self):
        warnings.warn("Uh oh", ResourceWarning, source=self)

def func():
    m = MyClass()

func()
python3 -Wd -X tracemalloc=2 example.py

cc @markshannon

Linked PRs

Activity

  1. added
    type-bug
    3.12
    on Jan 30, 2023
  2. colesbury commented on Jan 30, 2023

    @colesbury
    ContributorAuthor

    Also _PyTraceMalloc_NewReference:

    cpython/Modules/_tracemalloc.c

    Lines 1726 to 1733 in 28db978

    uintptr_t ptr;
    PyTypeObject *type = Py_TYPE(op);
    if (PyType_IS_GC(type)) {
    ptr = (uintptr_t)((char *)op - sizeof(PyGC_Head));
    }
    else {
    ptr = (uintptr_t)op;
    }

  3. added 3 commits that reference this issue on Feb 9, 2023
  4. added a commit that references this issue on Feb 10, 2023
  5. vstinner commented on Apr 5, 2023

    @vstinner
    Member

    If someone is motivated, it would be nice to add an unit test to test_tracemalloc for this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions