From bc3093092e60c9621ca1cb6e1a59fb45591dc0b6 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 14 Mar 2025 14:49:13 +0100 Subject: [PATCH 1/3] make repeat_next ft safe --- Modules/itertoolsmodule.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 9c9a965ce74feb..9fae367ae01508 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -3630,10 +3630,13 @@ 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--; + } + cnt--; + assert(cnt >=0); + FT_ATOMIC_STORE_SSIZE_RELAXED(ro->cnt, cnt); return Py_NewRef(ro->element); } From 2037e3dd7fa7c0d8b667f77946d5a54e2305a8ea Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 14 Mar 2025 14:18:52 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-03-14-14-18-49.gh-issue-123471.sduBKk.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-03-14-14-18-49.gh-issue-123471.sduBKk.rst 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 00000000000000..b3829c72e5cad7 --- /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. From 65daaeec797f75b812ff289422acfb3425c2d86f Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 14 Mar 2025 15:33:23 +0100 Subject: [PATCH 3/3] fix --- Modules/itertoolsmodule.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 9fae367ae01508..9387889f265376 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -3634,9 +3634,10 @@ repeat_next(PyObject *op) if (cnt == 0) { return NULL; } - cnt--; - assert(cnt >=0); - FT_ATOMIC_STORE_SSIZE_RELAXED(ro->cnt, cnt); + if (cnt > 0) { + cnt--; + FT_ATOMIC_STORE_SSIZE_RELAXED(ro->cnt, cnt); + } return Py_NewRef(ro->element); }