Skip to content

Calling Py_DECREF twice does not result in error with debug build #109496

Closed
@kevinAlbs

Description

@kevinAlbs

Bug report

Bug description:

Documented behavior of Py_REF_DEBUG includes:

Py_REF_DEBUG also checks after every decref to verify that the refcount hasn't
gone negative, and causes an immediate fatal error if it has.

Calling Py_DECREF twice does not result in the expected error:

static PyObject* spam_double_decref(PyObject *self, PyObject *args) {
    printf("spam_double_decref ... begin\n");
    PyObject *obj = Py_BuildValue("s", "foobar");
    Py_DECREF (obj);
    Py_DECREF (obj); // Expect error, but does not error when using cpython built with `--with-pydebug`.
    printf("spam_double_decref ... end\n");
    Py_RETURN_NONE;
}

To reproduce:

Build CPython with --with-pydebug:

cd /home/kevin/code/cpython
mkdir debug
cd debug/
../configure --with-pydebug
make -j16

Build this sample extension calling Py_DECREF twice using the debug build of CPython:

PYTHON=/home/kevin/code/cpython/debug/python
$PYTHON setup.py build

Run a test with this extension:

# Add path to built extension to `PYTHONPATH`.
export PYTHONPATH=/home/kevin/code/cpython/KEVINALBS/double_decref_extension/build/lib.linux-x86_64-cpython-313-pydebug
PYTHON=/home/kevin/code/cpython/debug/python
$PYTHON -c "import spam; spam.double_decref()"
# Prints:
# spam_double_decref ... begin
# spam_double_decref ... end

No error is indicated, but an error is expected.

Extension source is located here: https://github.com/kevinAlbs/double_decref_extension
Tested with cpython main branch on commit: 929cc4e.

If this issue is confirmed, I may be interested to investigate possible solutions.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    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