Skip to content

Commit ab05beb

Browse files
authored
gh-127599: Fix _Py_RefcntAdd missing calls to _Py_INCREF_STAT_INC/_Py_INCREF_IMMORTAL_STAT_INC (#127717)
Previously, `_Py_RefcntAdd` hasn't called `_Py_INCREF_STAT_INC/_Py_INCREF_IMMORTAL_STAT_INC` which is incorrect. Now it has been fixed.
1 parent 7900a85 commit ab05beb

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-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
@@ -131,6 +131,7 @@ extern void _Py_DecRefTotal(PyThreadState *);
131131
static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n)
132132
{
133133
if (_Py_IsImmortal(op)) {
134+
_Py_INCREF_IMMORTAL_STAT_INC();
134135
return;
135136
}
136137
#ifdef Py_REF_DEBUG
@@ -159,6 +160,10 @@ static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n)
159160
_Py_atomic_add_ssize(&op->ob_ref_shared, (n << _Py_REF_SHARED_SHIFT));
160161
}
161162
#endif
163+
// Although the ref count was increased by `n` (which may be greater than 1)
164+
// it is only a single increment (i.e. addition) operation, so only 1 refcnt
165+
// increment operation is counted.
166+
_Py_INCREF_STAT_INC();
162167
}
163168
#define _Py_RefcntAdd(op, n) _Py_RefcntAdd(_PyObject_CAST(op), n)
164169

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix statistics for increments of object reference counts (in particular, when
2+
a reference count was increased by more than 1 in a single operation).

0 commit comments

Comments
 (0)