Skip to content

gh-152276: Fix assert(!_Py_IsImmortal(op)) failure when BRC merge queue races with sys.intern - #152277

Closed
AnnaAr321 wants to merge 3 commits into
python:mainfrom
AnnaAr321:brc-race-fix
Closed

AnnaAr321 wants to merge 3 commits into
python:mainfrom
AnnaAr321:brc-race-fix

Conversation

@AnnaAr321

@AnnaAr321 AnnaAr321 commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

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.

…_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.
@bedevere-app

bedevere-app Bot commented Jun 26, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@nascheme

nascheme commented Jul 3, 2026

Copy link
Copy Markdown
Member

Some comments about this:

  • This looks like a real race and we should probably fix it.
  • Need to check other places we merge, could have same issue
  • On those lines, I think it makes sense to do the _Py_IsImmortal() check inside _Py_ExplicitMergeRefcount(). That would change the function contract from “must not be immortal” to “safe no-op for immortal objects except
    debug accounting.”
  • Need to do the Py_REF_DEBUG accounting in the immortal case, otherwise refcount totals may be off
  • Adding a new test case is good, try to make it not too expensive if at all possible (time to run test suite generally grows over time but we should try to keep it efficient if we can). Time it and see how much it adds. Do we actually need 1m strings? Maybe 100k would trigger the error most of the time. It doesn't need to be totally reliable at triggering the race, since the test suite gets run so many times.
  • For your news/blurb entry, I'd suggest simpler language. If someone is not familiar with this part of the code, it will be hard to understand what the change is. Something like "For the free-threaded build, avoid an assertion failure when an object becomes immortal while queued for biased reference counting (BRC) merging."

@@ -0,0 +1 @@
Fix "assert(!_Py_IsImmortal(op))" failure when BRC merge queue races with objects that were recently made immortal.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fix "assert(!_Py_IsImmortal(op))" failure when BRC merge queue races with objects that were recently made immortal.
Fix non-immortal assertion failure when BRC merge queue races with objects that were made immortal later.

# 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)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is probably too slow, I would prefer to have test which takes less time.

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants