Skip to content

gh-116909: fix data race with versions in typeobject #134651

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 @@
Fix crash upon importing a static type in a subinterpreter.
32 changes: 27 additions & 5 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class object "PyObject *" "&PyBaseObject_Type"
PyUnicode_CheckExact(name) && \
(PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE)

#define NEXT_GLOBAL_VERSION_TAG _PyRuntime.types.next_version_tag
#define NEXT_VERSION_TAG(interp) \
(interp)->types.next_version_tag

Expand Down Expand Up @@ -1258,6 +1257,25 @@ _PyType_GetVersionForCurrentState(PyTypeObject *tp)
#error "_Py_ATTR_CACHE_UNUSED must be bigger than max"
#endif

static int
get_next_global_version(unsigned int *dest)
{
unsigned int v;
while (1) {
v = _Py_atomic_load_uint_relaxed(&_PyRuntime.types.next_version_tag);

/* Stop if passed the maximum or we successfully updated the field */
if (v > _Py_MAX_GLOBAL_TYPE_VERSION_TAG ||
_Py_atomic_compare_exchange_uint(&_PyRuntime.types.next_version_tag,
&v, v + 1)) {
break;
}
}

*dest = v;
return v <= _Py_MAX_GLOBAL_TYPE_VERSION_TAG;
}

static int
assign_version_tag(PyInterpreterState *interp, PyTypeObject *type)
{
Expand Down Expand Up @@ -1288,11 +1306,12 @@ assign_version_tag(PyInterpreterState *interp, PyTypeObject *type)
}
if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
/* static types */
if (NEXT_GLOBAL_VERSION_TAG > _Py_MAX_GLOBAL_TYPE_VERSION_TAG) {
unsigned int version;
if (!get_next_global_version(&version)) {
/* We have run out of version numbers */
return 0;
}
set_version_unlocked(type, NEXT_GLOBAL_VERSION_TAG++);
set_version_unlocked(type, version);
assert (type->tp_version_tag <= _Py_MAX_GLOBAL_TYPE_VERSION_TAG);
}
else {
Expand Down Expand Up @@ -8877,9 +8896,12 @@ init_static_type(PyInterpreterState *interp, PyTypeObject *self,
type_add_flags(self, _Py_TPFLAGS_STATIC_BUILTIN);
type_add_flags(self, Py_TPFLAGS_IMMUTABLETYPE);

assert(NEXT_GLOBAL_VERSION_TAG <= _Py_MAX_GLOBAL_TYPE_VERSION_TAG);
if (self->tp_version_tag == 0) {
_PyType_SetVersion(self, NEXT_GLOBAL_VERSION_TAG++);
unsigned int version;
if (!get_next_global_version(&version)) {
assert(0 && "we have run out of version numbers");
}
_PyType_SetVersion(self, version);
}
}
else {
Expand Down
Loading