python / python/cpython

Expose tracemalloc hook into `_Py_NewReference` for other tracers

Open
#93,502 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.13 interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

When new objects are created, the allocation code ends calling into _Py_NewReference (with some exceptions). tracemalloc has a hook into this function to correlate allocations and traces (that do not know anything about objects or references) with newly created objects. This happens here:

https://github.com/python/cpython/blob/3d647e70cf4fd0e7cef68ed6662de3cb2cb0d63d/Objects/object.c#L2018-L2020

This allows tracemalloc to be able to return the traceback where objects where allocated, which is great. Unfortunately other debuggers and profilers do not have this capability as there is no way to leverage trace malloc functionality or to hook into object creation.

I propose to expose an API (unclear what layer of the C-API this should be in) where callbacks can be registered to be called in _Py_NewReference and switch tracemalloc to use this API.

Linked PRs
  • gh-115945

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Objects/object.c around the _Py_NewReference call and review how tracemalloc currently hooks there. Read linked PR gh-115945 and the surrounding C-API discussion before determining the appropriate API layer. Done means other tracers can register callbacks and tracemalloc uses that API without losing allocation trace correlation.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
backend, devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.