]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-123471: Make concurrent iteration over itertools.repeat safe under free-threading...
authorPieter Eendebak <pieter.eendebak@gmail.com>
Sun, 13 Apr 2025 07:56:58 +0000 (09:56 +0200)
committerGitHub <noreply@github.com>
Sun, 13 Apr 2025 07:56:58 +0000 (13:26 +0530)
Misc/NEWS.d/next/Library/2025-03-14-14-18-49.gh-issue-123471.sduBKk.rst [new file with mode: 0644]
Modules/itertoolsmodule.c

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 (file)
index 0000000..b3829c7
--- /dev/null
@@ -0,0 +1 @@
+Make concurrent iterations over :class:`itertools.repeat` safe under free-threading.
index c0448efed19a443e3bedd36838a48904117e81d8..943c1e8607b38ffff505b4a8b5675b30db5972f5 100644 (file)
@@ -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);
 }