-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-144356: Make set iterator __length_hint__ and iternext race-safe under no-gil
#144357
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
base: main
Are you sure you want to change the base?
Changes from all commits
47d75fe
3e3785c
229ced3
cdcf88a
21f1478
a18c698
79b5fbc
6ac15e0
3222eef
7fb39bc
78241a8
52285a2
c9ece31
e41e852
b32b5ec
d14856c
2a512a8
1ce898a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix potential races in set iterators (``__length_hint__`` and iteration) in free-threaded builds. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1056,8 +1056,22 @@ setiter_len(PyObject *op, PyObject *Py_UNUSED(ignored)) | |||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| setiterobject *si = (setiterobject*)op; | ||||||||||||||||||||||||||||||||||||||||||||
| Py_ssize_t len = 0; | ||||||||||||||||||||||||||||||||||||||||||||
| if (si->si_set != NULL && si->si_used == si->si_set->used) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #ifdef Py_GIL_DISABLED | ||||||||||||||||||||||||||||||||||||||||||||
hyongtao-code marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||
| PySetObject *so = si->si_set; | ||||||||||||||||||||||||||||||||||||||||||||
| assert(so != NULL); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Py_BEGIN_CRITICAL_SECTION2(op, so); | ||||||||||||||||||||||||||||||||||||||||||||
| if (si->si_pos >= 0 && si->si_used == so->used) { | ||||||||||||||||||||||||||||||||||||||||||||
| len = si->len; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| Py_END_CRITICAL_SECTION2(); | ||||||||||||||||||||||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||||||||||||||||||||||
| if (si->si_set != NULL && si->si_used == si->si_set->used) { | ||||||||||||||||||||||||||||||||||||||||||||
| len = si->len; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1060
to
+1074
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This is less code and a bit more efficient (only a single lock): From the set object @hyongtao-code I did not test the above suggestion |
||||||||||||||||||||||||||||||||||||||||||||
| return PyLong_FromSsize_t(len); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1089,17 +1103,23 @@ static PyMethodDef setiter_methods[] = { | |||||||||||||||||||||||||||||||||||||||||||
| {NULL, NULL} /* sentinel */ | ||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| static PyObject *setiter_iternext(PyObject *self) | ||||||||||||||||||||||||||||||||||||||||||||
| static PyObject * | ||||||||||||||||||||||||||||||||||||||||||||
| setiter_iternext(PyObject *self) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| setiterobject *si = (setiterobject*)self; | ||||||||||||||||||||||||||||||||||||||||||||
| PyObject *key = NULL; | ||||||||||||||||||||||||||||||||||||||||||||
| Py_ssize_t i, mask; | ||||||||||||||||||||||||||||||||||||||||||||
| setentry *entry; | ||||||||||||||||||||||||||||||||||||||||||||
| PySetObject *so = si->si_set; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if (so == NULL) | ||||||||||||||||||||||||||||||||||||||||||||
| #ifdef Py_GIL_DISABLED | ||||||||||||||||||||||||||||||||||||||||||||
| assert(so != NULL); | ||||||||||||||||||||||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||||||||||||||||||||||
| if (so == NULL) { | ||||||||||||||||||||||||||||||||||||||||||||
| return NULL; | ||||||||||||||||||||||||||||||||||||||||||||
| assert (PyAnySet_Check(so)); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||
| assert(PyAnySet_Check(so)); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Py_ssize_t so_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used); | ||||||||||||||||||||||||||||||||||||||||||||
| Py_ssize_t si_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(si->si_used); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1110,26 +1130,53 @@ static PyObject *setiter_iternext(PyObject *self) | |||||||||||||||||||||||||||||||||||||||||||
| return NULL; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #ifdef Py_GIL_DISABLED | ||||||||||||||||||||||||||||||||||||||||||||
| Py_BEGIN_CRITICAL_SECTION2(self, so); | ||||||||||||||||||||||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||||||||||||||||||||||
| Py_BEGIN_CRITICAL_SECTION(so); | ||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| i = si->si_pos; | ||||||||||||||||||||||||||||||||||||||||||||
| assert(i>=0); | ||||||||||||||||||||||||||||||||||||||||||||
| #ifdef Py_GIL_DISABLED | ||||||||||||||||||||||||||||||||||||||||||||
| if (i < 0) { | ||||||||||||||||||||||||||||||||||||||||||||
| /* iterator already exhausted */ | ||||||||||||||||||||||||||||||||||||||||||||
| goto done; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| entry = so->table; | ||||||||||||||||||||||||||||||||||||||||||||
| mask = so->mask; | ||||||||||||||||||||||||||||||||||||||||||||
| while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) { | ||||||||||||||||||||||||||||||||||||||||||||
| i++; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (i <= mask) { | ||||||||||||||||||||||||||||||||||||||||||||
| key = Py_NewRef(entry[i].key); | ||||||||||||||||||||||||||||||||||||||||||||
| si->si_pos = i + 1; | ||||||||||||||||||||||||||||||||||||||||||||
| si->len--; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||||||
| /* exhausted */ | ||||||||||||||||||||||||||||||||||||||||||||
| si->si_pos = -1; | ||||||||||||||||||||||||||||||||||||||||||||
| si->len = 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed? |
||||||||||||||||||||||||||||||||||||||||||||
| #ifndef Py_GIL_DISABLED | ||||||||||||||||||||||||||||||||||||||||||||
| si->si_set = NULL; | ||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #ifdef Py_GIL_DISABLED | ||||||||||||||||||||||||||||||||||||||||||||
| done: | ||||||||||||||||||||||||||||||||||||||||||||
| Py_END_CRITICAL_SECTION2(); | ||||||||||||||||||||||||||||||||||||||||||||
| return key; | ||||||||||||||||||||||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||||||||||||||||||||||
| Py_END_CRITICAL_SECTION(); | ||||||||||||||||||||||||||||||||||||||||||||
| si->si_pos = i+1; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if (key == NULL) { | ||||||||||||||||||||||||||||||||||||||||||||
| si->si_set = NULL; | ||||||||||||||||||||||||||||||||||||||||||||
| /* exhausted */ | ||||||||||||||||||||||||||||||||||||||||||||
| Py_DECREF(so); | ||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move the decref to the section ? |
||||||||||||||||||||||||||||||||||||||||||||
| return NULL; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| si->len--; | ||||||||||||||||||||||||||||||||||||||||||||
| return key; | ||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| PyTypeObject PySetIter_Type = { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.