[3.13] gh-117657: Fix race involving immortalizing objects (GH-119927) (#120005)

The free-threaded build currently immortalizes objects that use deferred
reference counting (see gh-117783). This typically happens once the
first non-main thread is created, but the behavior can be suppressed for
tests, in subinterpreters, or during a compile() call.

This fixes a race condition involving the tracking of whether the
behavior is suppressed.

(cherry picked from commit 47fb4327b5)
This commit is contained in:
Sam Gross 2024-06-03 18:21:32 -04:00 committed by GitHub
parent ca37034baa
commit ae705319fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 30 additions and 44 deletions

View file

@ -2433,7 +2433,7 @@ _PyObject_SetDeferredRefcount(PyObject *op)
assert(op->ob_ref_shared == 0);
_PyObject_SET_GC_BITS(op, _PyGC_BITS_DEFERRED);
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->gc.immortalize.enabled) {
if (_Py_atomic_load_int_relaxed(&interp->gc.immortalize) == 1) {
// gh-117696: immortalize objects instead of using deferred reference
// counting for now.
_Py_SetImmortal(op);