From: Pieter Eendebak Date: Sun, 13 Apr 2025 07:56:58 +0000 (+0200) Subject: gh-123471: Make concurrent iteration over itertools.repeat safe under free-threading... X-Git-Tag: v3.14.0b1~505 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9d127e83b9e6fbbb50ee047a0444c189a8770ada;p=thirdparty%2FPython%2Fcpython.git gh-123471: Make concurrent iteration over itertools.repeat safe under free-threading (#131247) --- diff --git a/Misc/NEWS.d/next/Library/2025-03-14-14-18-49.gh-issue-123471.sduBKk.rst b/Misc/NEWS.d/next/Library/2025-03-14-14-18-49.gh-issue-123471.sduBKk.rst new file mode 100644 index 000000000000..b3829c72e5ca --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-14-14-18-49.gh-issue-123471.sduBKk.rst @@ -0,0 +1 @@ +Make concurrent iterations over :class:`itertools.repeat` safe under free-threading. diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index c0448efed19a..943c1e8607b3 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -3628,10 +3628,14 @@ static PyObject * repeat_next(PyObject *op) { repeatobject *ro = repeatobject_CAST(op); - if (ro->cnt == 0) + Py_ssize_t cnt = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->cnt); + if (cnt == 0) { return NULL; - if (ro->cnt > 0) - ro->cnt--; + } + if (cnt > 0) { + cnt--; + FT_ATOMIC_STORE_SSIZE_RELAXED(ro->cnt, cnt); + } return Py_NewRef(ro->element); }