Closed
Description
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
- gh-109496: Detect Py_DECREF() after dealloc in debug mode #109539
- [3.12] gh-109496: Detect Py_DECREF() after dealloc in debug mode (GH-109539) #109545
- gh-109496: Skip test_capi.test_decref_freed_object() on ASAN #109573
- [3.12] gh-109496: Skip test_capi.test_decref_freed_object() on ASAN (GH-109573) #109578