Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
duaneg wants to merge5 commits intopython:main
base:main
Choose a base branch
Loading
fromduaneg:gh-116909
Open
Show file tree
Hide file tree
Changes from4 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Access and update ``_PyRuntime.types.next_version_tag`` in a thread-safe
manner.
32 changes: 27 additions & 5 deletionsObjects/typeobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp