Skip to content

gh-123471: Make itertools.chain thread-safe #135689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion Lib/test/test_free_threading/test_itertools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from threading import Thread, Barrier
from itertools import batched, cycle
from itertools import batched, chain, cycle
from test.support import threading_helper


Expand Down Expand Up @@ -62,6 +62,35 @@ def work(it):

barrier.reset()

@threading_helper.reap_threads
def test_chain(self):
number_of_threads = 6
number_of_iterations = 20

barrier = Barrier(number_of_threads)
def work(it):
barrier.wait()
while True:
try:
_ = next(it)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the extra assignment to _?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some linters complain if the value is not assigned. I can remove it though, it is not needed here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linters tend to complain about a lot of things in the source. I'd just remove it.

except StopIteration:
break


data = [(1, )] * 200
for it in range(number_of_iterations):
chain_iterator = chain(*data)
worker_threads = []
for ii in range(number_of_threads):
worker_threads.append(
Thread(target=work, args=[chain_iterator]))

with threading_helper.start_threads(worker_threads):
pass

barrier.reset()



if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make concurrent iterations over :class:`itertools.chain` safe under free-threading.
14 changes: 12 additions & 2 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1880,8 +1880,8 @@ chain_traverse(PyObject *op, visitproc visit, void *arg)
return 0;
}

static PyObject *
chain_next(PyObject *op)
static inline PyObject *
chain_next_lock_held(PyObject *op)
{
chainobject *lz = chainobject_CAST(op);
PyObject *item;
Expand Down Expand Up @@ -1919,6 +1919,16 @@ chain_next(PyObject *op)
return NULL;
}

static PyObject *
chain_next(PyObject *op)
{
PyObject * result;
Py_BEGIN_CRITICAL_SECTION(op);
result = chain_next_lock_held(op);
Py_END_CRITICAL_SECTION()
return result;
}

PyDoc_STRVAR(chain_doc,
"chain(*iterables)\n\
--\n\
Expand Down
Loading