Skip to content

Commit 9d127e8

Browse files
authored
gh-123471: Make concurrent iteration over itertools.repeat safe under free-threading (#131247)
1 parent 5863cd7 commit 9d127e8

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make concurrent iterations over :class:`itertools.repeat` safe under free-threading.

Modules/itertoolsmodule.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,10 +3628,14 @@ static PyObject *
36283628
repeat_next(PyObject *op)
36293629
{
36303630
repeatobject *ro = repeatobject_CAST(op);
3631-
if (ro->cnt == 0)
3631+
Py_ssize_t cnt = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->cnt);
3632+
if (cnt == 0) {
36323633
return NULL;
3633-
if (ro->cnt > 0)
3634-
ro->cnt--;
3634+
}
3635+
if (cnt > 0) {
3636+
cnt--;
3637+
FT_ATOMIC_STORE_SSIZE_RELAXED(ro->cnt, cnt);
3638+
}
36353639
return Py_NewRef(ro->element);
36363640
}
36373641

0 commit comments

Comments
 (0)