Skip to content

Unsafe DECREFs of borrowed references in the interpreter. #125323

Closed
@markshannon

Description

@markshannon

Bug report

Bug description:

It is unsafe to borrow a PyObject * reference from a _PyStackRef and Py_DECREF the PyObject * reference and not close the _PyStackRef. This is quite a common pattern in bytecodes.c, and prevents any optimizations based on reference lifetimes as the inferred lifetime is incorrect.

The fix for this is to change the incorrect pattern:

PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
use(left_o);
Py_DECREF(left_o);

to the correct pattern:

PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
use(left_o);
PyStackRef_CLOSE(left);

This is causing problems, as optimizations to exploit stack refs don't work: https://github.com/python/cpython/compare/main...faster-cpython:cpython:use-stackrefs-opt-experiment?expand=1

CPython versions tested on:

CPython main branch

Operating systems tested on:

Other

Linked PRs

Metadata

Metadata

Labels

interpreter-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