Skip to content

[3.13] gh-119525: Fix deadlock with _PyType_Lookup and the GIL (GH-119527) #119746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix deadlock involving ``_PyType_Lookup()`` cache in the free-threaded build
when the GIL is dynamically enabled at runtime.
11 changes: 7 additions & 4 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5062,7 +5062,7 @@ is_dunder_name(PyObject *name)
return 0;
}

static void
static PyObject *
update_cache(struct type_cache_entry *entry, PyObject *name, unsigned int version_tag, PyObject *value)
{
_Py_atomic_store_uint32_relaxed(&entry->version, version_tag);
Expand All @@ -5073,7 +5073,7 @@ update_cache(struct type_cache_entry *entry, PyObject *name, unsigned int versio
// exact unicode object or Py_None so it's safe to do so.
PyObject *old_name = entry->name;
_Py_atomic_store_ptr_relaxed(&entry->name, Py_NewRef(name));
Py_DECREF(old_name);
return old_name;
}

#if Py_GIL_DISABLED
Expand All @@ -5093,10 +5093,12 @@ update_cache_gil_disabled(struct type_cache_entry *entry, PyObject *name,
return;
}

update_cache(entry, name, version_tag, value);
PyObject *old_value = update_cache(entry, name, version_tag, value);

// Then update sequence to the next valid value
_PySeqLock_UnlockWrite(&entry->sequence);

Py_DECREF(old_value);
}

#endif
Expand Down Expand Up @@ -5208,7 +5210,8 @@ _PyType_LookupRef(PyTypeObject *type, PyObject *name)
#if Py_GIL_DISABLED
update_cache_gil_disabled(entry, name, version, res);
#else
update_cache(entry, name, version, res);
PyObject *old_value = update_cache(entry, name, version, res);
Py_DECREF(old_value);
#endif
}
return res;
Expand Down
Loading