[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

@ -110,7 +110,7 @@ should_intern_string(PyObject *o)
// unless we've disabled immortalizing objects that use deferred reference
// counting.
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->gc.immortalize.enable_on_thread_created) {
if (_Py_atomic_load_int(&interp->gc.immortalize) < 0) {
return 1;
}
#endif
@ -240,7 +240,7 @@ intern_constants(PyObject *tuple, int *modified)
PyThreadState *tstate = PyThreadState_GET();
if (!_Py_IsImmortal(v) && !PyCode_Check(v) &&
!PyUnicode_CheckExact(v) &&
tstate->interp->gc.immortalize.enable_on_thread_created)
_Py_atomic_load_int(&tstate->interp->gc.immortalize) >= 0)
{
PyObject *interned = intern_one_constant(v);
if (interned == NULL) {