From 9d127e83b9e6fbbb50ee047a0444c189a8770ada Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 13 Apr 2025 09:56:58 +0200 Subject: gh-123471: Make concurrent iteration over itertools.repeat safe under free-threading (#131247) --- .../Library/2025-03-14-14-18-49.gh-issue-123471.sduBKk.rst | 1 + Modules/itertoolsmodule.c | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) 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 0000000..b3829c7 --- /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 c0448ef..943c1e8 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); } -- cgit v0.12