https://github.com/python/cpython/commit/9e5ab7e13f4fe9924055b624fec04de15c4561aa
commit: 9e5ab7e13f4fe9924055b624fec04de15c4561aa
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-11-20T17:48:42+05:30
summary:

gh-152276: Fix assertion failure when an object becomes immortal while queued 
for BRC merging (#162316)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-11-20-12-00-00.gh-issue-152276.bRcImm.rst
M Lib/test/test_free_threading/test_races.py
M Objects/object.c

diff --git a/Lib/test/test_free_threading/test_races.py 
b/Lib/test/test_free_threading/test_races.py
index f08de7496eb526d..dc1617512f8795d 100644
--- a/Lib/test/test_free_threading/test_races.py
+++ b/Lib/test/test_free_threading/test_races.py
@@ -288,6 +288,22 @@ def set_recursion_limit():
 
         do_race(something_recursive, set_recursion_limit)
 
+    def test_racing_intern_queued_for_merge(self):
+        # gh-152276: an object may become immortal while it is queued for
+        # its owning thread to merge its reference count.
+        # The same strings are used on every run, so that reruns do not
+        # create new immortal strings.
+        for i in range(10):
+            strings = [f"test_racing_intern_queued_for_merge_{i}_{j}"
+                       for j in range(1000)]
+            shared = list(strings)
+            # Dropping the references from another thread queues the strings
+            # to be merged by this thread, which is busy interning them.
+            thread = threading.Thread(target=shared.clear)
+            thread.start()
+            list(map(sys.intern, strings))
+            thread.join()
+
 
 @threading_helper.requires_working_threading()
 class TestWarningsRaces(TestBase):
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-20-12-00-00.gh-issue-152276.bRcImm.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-20-12-00-00.gh-issue-152276.bRcImm.rst
new file mode 100644
index 000000000000000..9cc5d98dae0d0f1
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-20-12-00-00.gh-issue-152276.bRcImm.rst
@@ -0,0 +1,2 @@
+For the :term:`free-threaded build`, avoid an assertion failure when an object
+becomes immortal while queued for biased reference counting (BRC) merging.
diff --git a/Objects/object.c b/Objects/object.c
index 0695cc090c979c4..1589e3523e62053 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -468,7 +468,12 @@ _Py_MergeZeroLocalRefcount(PyObject *op)
 Py_ssize_t
 _Py_ExplicitMergeRefcount(PyObject *op, Py_ssize_t extra)
 {
-    assert(!_Py_IsImmortal(op));
+    if (_Py_IsImmortal(op)) {
+        // The object may have become immortal after it was queued to be
+        // merged (for example, by sys.intern()). There is nothing to merge
+        // and the reference total no longer includes the object.
+        return _Py_IMMORTAL_INITIAL_REFCNT;
+    }
 
 #ifdef Py_REF_DEBUG
     _Py_AddRefTotal(_PyThreadState_GET(), extra);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to