Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
colesbury opened this issue Jan 30, 2023 · 2 comments
Labels
3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@colesbury
Copy link
Contributor

colesbury commented Jan 30, 2023

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

@colesbury colesbury added type-bug An unexpected behavior, bug, or error 3.12 bugs and security fixes labels Jan 30, 2023
@colesbury
Copy link
Contributor Author

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;
}

corona10 added a commit to corona10/cpython that referenced this issue Feb 9, 2023
corona10 added a commit to corona10/cpython that referenced this issue Feb 9, 2023
@corona10 corona10 closed this as completed Feb 9, 2023
carljm added a commit to carljm/cpython that referenced this issue Feb 10, 2023
* main:
  Fix some typos in asdl_c.py (pythonGH-101757)
  pythongh-101747: Fix refleak in new `OrderedDict` repr (pythonGH-101748)
  pythongh-101430: Update tracemalloc to handle presize properly. (pythongh-101745)
  pythonGH-101228: Fix typo in docstring for read method of `_io.TextIOWrapper` class (python#101227)
  Fix typo in `test_fstring.py` (python#101600)
  pythongh-101726: Update the OpenSSL version to 1.1.1t (pythonGH-101727)
  pythongh-101283: Fix 'versionchanged' for the shell=True fallback on Windows in 3.12 (pythonGH-101728)
  LibFFI build requires x64 Cygwin, and skip the ARM build (pythonGH-101743)
@vstinner
Copy link
Member

vstinner commented Apr 5, 2023

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

zhuyifei1999 added a commit to zhuyifei1999/guppy3 that referenced this issue May 13, 2023
Python added _PyType_PreHeaderSize for the preheader and it's
no longer just sizeof(PyGC_Head)

See python/cpython#101430

For #41
zhuyifei1999 added a commit to zhuyifei1999/guppy3 that referenced this issue May 13, 2023
Python added _PyType_PreHeaderSize for the preheader and it's
no longer just sizeof(PyGC_Head)

See python/cpython#101430

For #41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants