Skip to content

Draft: gh-123471: Make concurrent iteration over itertools.pairwise safe under free-threading #125417

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
42 changes: 42 additions & 0 deletions Lib/test/test_free_threading/test_itertools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest
from threading import Thread

from test.support import threading_helper

from itertools import zip_longest

class PairwiseThreading(unittest.TestCase):
@staticmethod
def work(enum):
while True:
try:
next(enum)
except StopIteration:
break

@threading_helper.reap_threads
@threading_helper.requires_working_threading()
def test_zip_longest(self):
number_of_threads = 8
number_of_iterations = 40
n = 200
enum = zip_longest(range(n), range(2*n))
for _ in range(number_of_iterations):
worker_threads = []
for ii in range(number_of_threads):
worker_threads.append(
Thread(
target=self.work,
args=[
enum,
],
)
)
for t in worker_threads:
t.start()
for t in worker_threads:
t.join()


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make concurrent iterations over the same :func:`itertools.pairwise` iterator safe under free-threading.
38 changes: 29 additions & 9 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,37 +327,54 @@ pairwise_traverse(pairwiseobject *po, visitproc visit, void *arg)
static PyObject *
pairwise_next(pairwiseobject *po)
{
#ifdef Py_GIL_DISABLED
PyObject *it = Py_XNewRef(po->it);
#else
PyObject *it = po->it;
PyObject *old = po->old;
PyObject *new, *result;

#endif
if (it == NULL) {
return NULL;
}

PyObject *old = Py_XNewRef(po->old);
PyObject *new, *result;

if (old == NULL) {
old = (*Py_TYPE(it)->tp_iternext)(it);
Py_XSETREF(po->old, old);
if (old == NULL) {
Py_CLEAR(po->it);
#ifdef Py_GIL_DISABLED
Py_DECREF(it);
#endif
return NULL;
}
it = po->it;
if (it == NULL) {
Py_XSETREF(po->old, Py_NewRef(old));
if (po->it == NULL) {
// gh-109786: special case for re-entrant calls to pairwise next. the actual behavior is not
// important and this does not avoid any bugs (or does it?)
// the reason for having it is to make the behaviour equal to the python implementation
Py_CLEAR(po->old);
Py_DECREF(old);
#ifdef Py_GIL_DISABLED
Py_DECREF(it);
#endif
return NULL;
}
}
Py_INCREF(old);

new = (*Py_TYPE(it)->tp_iternext)(it);
if (new == NULL) {
Py_CLEAR(po->it);
Py_CLEAR(po->old);
#ifdef Py_GIL_DISABLED
Py_DECREF(it);
#endif
Py_DECREF(old);
return NULL;
}

result = po->result;
if (Py_REFCNT(result) == 1) {
if (_PyObject_IsUniquelyReferenced(result)) {
Py_INCREF(result);
PyObject *last_old = PyTuple_GET_ITEM(result, 0);
PyObject *last_new = PyTuple_GET_ITEM(result, 1);
Expand All @@ -380,7 +397,10 @@ pairwise_next(pairwiseobject *po)
}

Py_XSETREF(po->old, new);
Py_DECREF(old);
Py_DECREF(old); // instead of the decref here we could borrow the reference above
#ifdef Py_GIL_DISABLED
Py_DECREF(it);
#endif
return result;
}

Expand Down
Loading