From 123a05eb125723ba439e02bff0920bac98941b0d Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 26 Jun 2026 15:11:03 +0200 Subject: [PATCH 1/3] Fix `assert.h assertion failed at Objects/object.c:467 in Py_ssize_t _Py_ExplicitMergeRefcount(PyObject *, Py_ssize_t): !_Py_IsImmortal(op)` Immortal objects don't use refcounts. In the free-threaded build, when `_Py_DecRefShared` reduces an object's shared reference count to zero, it places the object into the interpreter's BRC merge queue. If another thread concurrently makes the object immortal (for example, via `sys.intern()`), `_Py_SetImmortalUntracked()` sets the object's reference fields to immortal constants. Later, when the original thread checks the eval breaker and attempts to merge the queued objects, it encounters the immortalized object and crashes on the assertion. --- Lib/test/test_free_threading/test_races.py | 27 ++++++++++++++++++++++ Python/brc.c | 3 +++ 2 files changed, 30 insertions(+) diff --git a/Lib/test/test_free_threading/test_races.py b/Lib/test/test_free_threading/test_races.py index 23b48c76195408..edcdc8e2944bb9 100644 --- a/Lib/test/test_free_threading/test_races.py +++ b/Lib/test/test_free_threading/test_races.py @@ -318,6 +318,33 @@ def emit_warning(): do_race(modify_filters, emit_warning) + def test_brc_queue_sys_intern_race(self): + # 1 million strings will take Thread A significant time to process in map() + # giving Thread B plenty of time to drop references to strings that Thread A + # hasn't interned yet! + strings = ["race_string_massive_" + str(j) for j in range(1000000)] + + # Give Thread B a copy of the list. + # Thread A still holds 'strings' so local refcount > 0. + shared = list(strings) + + def thread_b_func(): + # Thread B simply clears its list, which decrements the shared refcount + # of every string. Since Thread B is not the owner, this calls _Py_DecRefShared. + # Since shared refcount drops to 0, they get queued to Thread A! + shared.clear() + + tb = threading.Thread(target=thread_b_func) + tb.start() + + # Thread A enters map() which runs entirely in C! + # While Thread A iterates over the 1,000,000 strings, Thread B is concurrently + # clearing its list and queueing them. + # Any string that Thread B queues BEFORE Thread A reaches it in map() + # will become queued, AND THEN made immortal by sys.intern! + list(map(sys.intern, strings)) + tb.join() + if __name__ == "__main__": unittest.main() diff --git a/Python/brc.c b/Python/brc.c index d27687052aec19..a456545b50d2d3 100644 --- a/Python/brc.c +++ b/Python/brc.c @@ -104,6 +104,9 @@ merge_queued_objects(_PyObjectStack *to_merge) { PyObject *ob; while ((ob = _PyObjectStack_Pop(to_merge)) != NULL) { + if (_Py_IsImmortal(ob)) { + continue; + } // Subtract one when merging because the queue had a reference. Py_ssize_t refcount = _Py_ExplicitMergeRefcount(ob, -1); if (refcount == 0) { From 67a68f179a00643b7fa980bb53230d8c16c48fdb Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 26 Jun 2026 13:26:00 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst diff --git a/Misc/NEWS.d/next/Library/2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst b/Misc/NEWS.d/next/Library/2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst new file mode 100644 index 00000000000000..0bd6164db5b330 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst @@ -0,0 +1 @@ +Fix `assert(!_Py_IsImmortal(op))` failure when BRC merge queue races with objects that were recently made immortal. From 04e21843582be605176d88cecbe51a918ddb8807 Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 26 Jun 2026 15:35:26 +0200 Subject: [PATCH 3/3] Fix linting errors --- Lib/test/test_free_threading/test_races.py | 13 +++++++------ .../2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_free_threading/test_races.py b/Lib/test/test_free_threading/test_races.py index edcdc8e2944bb9..62e1b0385dc03b 100644 --- a/Lib/test/test_free_threading/test_races.py +++ b/Lib/test/test_free_threading/test_races.py @@ -323,23 +323,24 @@ def test_brc_queue_sys_intern_race(self): # giving Thread B plenty of time to drop references to strings that Thread A # hasn't interned yet! strings = ["race_string_massive_" + str(j) for j in range(1000000)] - + # Give Thread B a copy of the list. # Thread A still holds 'strings' so local refcount > 0. shared = list(strings) - + def thread_b_func(): # Thread B simply clears its list, which decrements the shared refcount - # of every string. Since Thread B is not the owner, this calls _Py_DecRefShared. - # Since shared refcount drops to 0, they get queued to Thread A! + # of every string. Since Thread B is not the owner, this calls + # _Py_DecRefShared. Since shared refcount drops to 0, they get queued + # to Thread A! shared.clear() tb = threading.Thread(target=thread_b_func) tb.start() # Thread A enters map() which runs entirely in C! - # While Thread A iterates over the 1,000,000 strings, Thread B is concurrently - # clearing its list and queueing them. + # While Thread A iterates over the 1,000,000 strings, Thread B is + # concurrently clearing its list and queueing them. # Any string that Thread B queues BEFORE Thread A reaches it in map() # will become queued, AND THEN made immortal by sys.intern! list(map(sys.intern, strings)) diff --git a/Misc/NEWS.d/next/Library/2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst b/Misc/NEWS.d/next/Library/2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst index 0bd6164db5b330..05fbb89a535138 100644 --- a/Misc/NEWS.d/next/Library/2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst +++ b/Misc/NEWS.d/next/Library/2026-06-26-13-25-59.gh-issue-152276.LGiipk.rst @@ -1 +1 @@ -Fix `assert(!_Py_IsImmortal(op))` failure when BRC merge queue races with objects that were recently made immortal. +Fix "assert(!_Py_IsImmortal(op))" failure when BRC merge queue races with objects that were recently made immortal.