Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-125174: Make immortality "sticky" #131184

Merged
merged 3 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Include/refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ having all the lower 32 bits set, which will avoid the reference count to go
beyond the refcount limit. Immortality checks for reference count decreases will
be done by checking the bit sign flag in the lower 32 bits.

To ensure that once an object becomes immortal, it remains immortal, the threshold
for omitting increfs is much higher than for omitting decrefs. Consequently, once
the refcount for an object exceeds _Py_IMMORTAL_MINIMUM_REFCNT it will gradually
increase over time until it reaches _Py_IMMORTAL_INITIAL_REFCNT.
*/
#define _Py_IMMORTAL_INITIAL_REFCNT (3UL << 30)
#define _Py_STATIC_FLAG_BITS ((Py_ssize_t)(_Py_STATICALLY_ALLOCATED_FLAG | _Py_IMMORTAL_FLAGS))
Expand Down Expand Up @@ -287,7 +291,7 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
}
#elif SIZEOF_VOID_P > 4
PY_UINT32_T cur_refcnt = op->ob_refcnt;
if (((int32_t)cur_refcnt) < 0) {
if (cur_refcnt >= _Py_IMMORTAL_INITIAL_REFCNT) {
// the object is immortal
_Py_INCREF_IMMORTAL_STAT_INC();
return;
Expand Down
24 changes: 23 additions & 1 deletion Lib/test/test_bigmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"""

from test import support
from test.support import bigmemtest, _1G, _2G, _4G
from test.support import bigmemtest, _1G, _2G, _4G, import_helper
_testcapi = import_helper.import_module('_testcapi')

import unittest
import operator
Expand Down Expand Up @@ -1257,6 +1258,27 @@ def test_dict(self, size):
d[size] = 1


class ImmortalityTest(unittest.TestCase):

@bigmemtest(size=_2G, memuse=pointer_size * 9/8)
def test_stickiness(self, size):
"""Check that immortality is "sticky", so that
once an object is immortal it remains so."""
if size < _2G:
# Not enough memory to cause immortality on overflow
return
o1 = o2 = o3 = o4 = o5 = o6 = o7 = o8 = object()
l = [o1] * (size-20)
self.assertFalse(_testcapi.is_immortal(o1))
for _ in range(30):
l.append(l[0])
self.assertTrue(_testcapi.is_immortal(o1))
del o2, o3, o4, o5, o6, o7, o8
self.assertTrue(_testcapi.is_immortal(o1))
del l
self.assertTrue(_testcapi.is_immortal(o1))


if __name__ == '__main__':
if len(sys.argv) > 1:
support.set_memlimit(sys.argv[1])
Expand Down
Loading