Skip to content

Commit e24d0fa

Browse files
committed
fix: _Py_RefcntAdd missing calls to _Py_INCREF_STAT_INC/_Py_INCREF_IMMORTAL_STAT_INC.
1 parent 72dca6c commit e24d0fa

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Include/cpython/pystats.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
//
1919
// Define _PY_INTERPRETER macro to increment interpreter_increfs and
2020
// interpreter_decrefs. Otherwise, increment increfs and decrefs.
21+
//
22+
// The number of incref operations counted by `incref` and
23+
// `interpreter_incref` is the number of increment operations, which is
24+
// not equal to the total of all reference counts. A single increment
25+
// operation may increase the reference count of an object by more than
26+
// one. For example, see `_Py_RefcntAdd`.
2127

2228
#ifndef Py_CPYTHON_PYSTATS_H
2329
# error "this header file must not be included directly"

Include/internal/pycore_object.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ extern void _Py_DecRefTotal(PyThreadState *);
121121
static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n)
122122
{
123123
if (_Py_IsImmortal(op)) {
124+
_Py_INCREF_IMMORTAL_STAT_INC();
124125
return;
125126
}
126127
#ifdef Py_REF_DEBUG
@@ -145,6 +146,10 @@ static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n)
145146
_Py_atomic_add_ssize(&op->ob_ref_shared, (n << _Py_REF_SHARED_SHIFT));
146147
}
147148
#endif
149+
// Although the ref count was increased by `n` (which may be greater than 1)
150+
// it is only a single increment (i.e. addition) operation, so only 1 refcnt
151+
// increment operation is counted.
152+
_Py_INCREF_STAT_INC();
148153
}
149154
#define _Py_RefcntAdd(op, n) _Py_RefcntAdd(_PyObject_CAST(op), n)
150155

0 commit comments

Comments
 (0)