Skip to content

Py_DECREF() in interpreter doesn't call _PyReftracerTrack(op, PyRefTracer_DESTROY) #130382

Closed

Description

@colesbury

Bug report

cpython/Python/ceval.c

Lines 72 to 85 in 6c450f4

#undef Py_DECREF
#define Py_DECREF(arg) \
do { \
PyObject *op = _PyObject_CAST(arg); \
if (_Py_IsImmortal(op)) { \
_Py_DECREF_IMMORTAL_STAT_INC(); \
break; \
} \
_Py_DECREF_STAT_INC(); \
if (--op->ob_refcnt == 0) { \
destructor dealloc = Py_TYPE(op)->tp_dealloc; \
(*dealloc)(op); \
} \
} while (0)

Tracemalloc only handles PyRefTracer_CREATE events and we don't have any tests for PyRefTracer_DESTROY events.

EDIT: _testcapimodule.c tests PyRefTracer_DESTROY events, but only through the C API and not the Python interpreter.

Linked PRs

Activity

  1. added
    3.13only security fixes
    3.14bugs and security fixes
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    type-bugAn unexpected behavior, bug, or error
    on Feb 20, 2025
  2. tom-pytel commented on Feb 24, 2025

    @tom-pytel
    Contributor

    There is a super-basic reftracer test in _testcapimodule.c, but no python interface that I can see, so added one to tracemalloc module to keep track of created and destroyed refs. Do you want this?

    Example:

    import tracemalloc
    
    tracemalloc.start()
    
    refs = tracemalloc.get_traced_refs()
    print(refs)
    
    tracemalloc.reset_peak()
    i = []
    refs = tracemalloc.get_traced_refs()
    print(refs)
    
    tracemalloc.reset_peak()
    j = [], []
    del j
    refs = tracemalloc.get_traced_refs()
    print(refs)
    
    import ssl
    refs = tracemalloc.get_traced_refs()
    print(refs)
    
    tracemalloc.stop()

    Run:

    $ ./python reftrace.py
    (0, 0)
    (1, 0)
    (3, 3)
    (16722, 8671)
    
  3. added a commit that references this issue on Feb 28, 2025
  4. added a commit that references this issue on Feb 28, 2025
  5. added 3 commits that reference this issue on Mar 13, 2025
  6. added a commit that references this issue on Mar 17, 2025
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.13only security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions