From 3823dd932c0a4ba9d4fce2a458b57fdb55698dd3 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Wed, 12 Mar 2025 09:47:10 +0000 Subject: [PATCH 1/5] Add some tolerance to no-gil immortality --- Include/refcount.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/refcount.h b/Include/refcount.h index 3efb7e5fee4a06..fd2f288f6a2ab2 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -288,7 +288,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) < 0) { // the object is immortal _Py_INCREF_IMMORTAL_STAT_INC(); return; From 9ae36339f74f1744c5964849431e7c982e1b7cec Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Wed, 12 Mar 2025 15:59:12 +0000 Subject: [PATCH 2/5] Add test --- Include/refcount.h | 6 +++++- Lib/test/test_bigmem.py | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Include/refcount.h b/Include/refcount.h index fd2f288f6a2ab2..7f09e0de0c84a7 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -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 (3ULL << 30) #define _Py_IMMORTAL_MINIMUM_REFCNT (1ULL << 31) @@ -288,7 +292,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 (cur_refcnt >= _Py_IMMORTAL_INITIAL_REFCNT) < 0) { + if (cur_refcnt >= _Py_IMMORTAL_INITIAL_REFCNT) { // the object is immortal _Py_INCREF_IMMORTAL_STAT_INC(); return; diff --git a/Lib/test/test_bigmem.py b/Lib/test/test_bigmem.py index c9ab1c1de9e186..33b819d0df17ce 100644 --- a/Lib/test/test_bigmem.py +++ b/Lib/test/test_bigmem.py @@ -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 @@ -1257,6 +1258,24 @@ 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.""" + 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]) From 147982174e35e243496f51eee3bb84b3bcf8de19 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 13 Mar 2025 13:54:42 +0000 Subject: [PATCH 3/5] Exit test early if not enough memory --- Lib/test/test_bigmem.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_bigmem.py b/Lib/test/test_bigmem.py index 33b819d0df17ce..6d0879ad65d53c 100644 --- a/Lib/test/test_bigmem.py +++ b/Lib/test/test_bigmem.py @@ -1264,6 +1264,9 @@ class ImmortalityTest(unittest.TestCase): 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)) From 7371bacf96c4d2ad944d88dec7273df595a8689e Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 17 Mar 2025 11:08:55 +0000 Subject: [PATCH 4/5] Fix refcount accounting --- Include/refcount.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Include/refcount.h b/Include/refcount.h index 7f09e0de0c84a7..23fea0e848ed15 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -307,7 +307,10 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) #endif _Py_INCREF_STAT_INC(); #ifdef Py_REF_DEBUG - _Py_INCREF_IncRefTotal(); + // Don't count the incref if the object is immortal. + if (_Py_IsImmortal(op)) { + _Py_INCREF_IncRefTotal(); + } #endif #endif } From 58225e4e69a6e12b125f77c14b3f0a23d4dd2919 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 17 Mar 2025 13:45:54 +0000 Subject: [PATCH 5/5] Get test right --- Include/refcount.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/refcount.h b/Include/refcount.h index 23fea0e848ed15..417d91bfa0da92 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -308,7 +308,7 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) _Py_INCREF_STAT_INC(); #ifdef Py_REF_DEBUG // Don't count the incref if the object is immortal. - if (_Py_IsImmortal(op)) { + if (!_Py_IsImmortal(op)) { _Py_INCREF_IncRefTotal(); } #endif