python / python/cpython

Deferring side effecting code in `_Py_Dealloc()`

Open
#98,260 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core performance
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Currently _Py_Dealloc() can call arbitrary code, and since _Py_Dealloc() can be called by any Py_DECREF() is means that Py_DECREF() can call arbitrary code. Py_DECREF() is everywhere, so it impairs our ability to reason about about the behavior of code.
This problem is not as severe as https://github.com/python/cpython/issues/97922, but is still worth fixing, especially as it will impair our ability to optimize traces for 3.12.

We can either make a Py_SAFE_DECREF() to be used internally, or change Py_DECREF() to defer all potentially side-effecting deallocation functions.

It is not clear which is the best option. Changing Py_DECREF() is simple, and certainly easy to reason about, but might delay deallocation of extension module objects for too long.

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 by examining the _Py_Dealloc() and Py_DECREF() entry points referenced in the issue, then read issue 97922 for related context. Compare the proposed Py_SAFE_DECREF() approach with changing Py_DECREF() itself, including the effect on extension module object deallocation. Done means selecting and implementing a justified design with coverage for the resulting deallocation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Refactor
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.