Skip to content

Commit 314f28d

Browse files
113xiaojiclaude
andcommitted
Lock only the deque when advancing a deque iterator
In the free-threaded build, dequeiter_next(), dequereviter_next() and dequeiter_reduce() entered a two-object critical section on the iterator and its deque. All writes to the iterator's state already happen while holding the deque's lock, so the deque's critical section alone is enough to serialize concurrent next() calls on a shared iterator. This halves the atomic read-modify-write operations per item. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent eb21dea commit 314f28d

2 files changed

Lines changed: 57 additions & 10 deletions

File tree

‎Lib/test/test_free_threading/test_collections.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,43 @@ def iterate():
8787
)
8888

8989

90+
def test_shared_iterator(self):
91+
# Deque iterators are advanced under the deque's lock, so threads
92+
# sharing one iterator must together see every item exactly once.
93+
n = 20000
94+
for make_iter in (iter, reversed):
95+
with self.subTest(make_iter=make_iter.__name__):
96+
d = deque(range(n))
97+
it = make_iter(d)
98+
results = []
99+
100+
def consume():
101+
results.append(list(x for x in it))
102+
103+
threading_helper.run_concurrently([consume] * 4)
104+
self.assertEqual(sorted(x for r in results for x in r),
105+
list(range(n)))
106+
107+
def test_iterate_while_mutating(self):
108+
d = deque(range(100))
109+
110+
def mutate():
111+
for i in range(5000):
112+
d.append(i)
113+
d.popleft()
114+
115+
def iterate():
116+
for _ in range(500):
117+
it = iter(d)
118+
try:
119+
for _ in it:
120+
pass
121+
except RuntimeError:
122+
pass
123+
it.__length_hint__()
124+
it.__reduce__()
125+
126+
threading_helper.run_concurrently([mutate, iterate, iterate])
127+
90128
if __name__ == "__main__":
91129
unittest.main()

‎Modules/_collectionsmodule.c‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,7 @@ dequeiter_next_lock_held(dequeiterobject *it, dequeobject *deque)
19661966
PyObject *item;
19671967

19681968
if (it->deque->state != it->state) {
1969-
it->counter = 0;
1969+
FT_ATOMIC_STORE_SSIZE_RELAXED(it->counter, 0);
19701970
PyErr_SetString(PyExc_RuntimeError,
19711971
"deque mutated during iteration");
19721972
return NULL;
@@ -1978,7 +1978,7 @@ dequeiter_next_lock_held(dequeiterobject *it, dequeobject *deque)
19781978

19791979
item = it->b->data[it->index];
19801980
it->index++;
1981-
it->counter--;
1981+
FT_ATOMIC_STORE_SSIZE_RELAXED(it->counter, it->counter - 1);
19821982
if (it->index == BLOCKLEN && it->counter > 0) {
19831983
CHECK_NOT_END(it->b->rightlink);
19841984
it->b = it->b->rightlink;
@@ -1987,6 +1987,14 @@ dequeiter_next_lock_held(dequeiterobject *it, dequeobject *deque)
19871987
return Py_NewRef(item);
19881988
}
19891989

1990+
/* The mutable fields of a deque iterator (b, index and counter) are only
1991+
* written while holding the critical section of the deque it iterates over,
1992+
* never the iterator's own lock. Concurrent next() calls on a shared
1993+
* iterator are therefore serialized by the deque's lock, and locking the
1994+
* iterator as well is unnecessary. This halves the number of atomic
1995+
* operations per item in the free-threaded build. __length_hint__ reads
1996+
* counter without locking, so writes to it use relaxed atomic stores.
1997+
*/
19901998
static PyObject *
19911999
dequeiter_next(PyObject *op)
19922000
{
@@ -1995,9 +2003,9 @@ dequeiter_next(PyObject *op)
19952003
// It's safe to access it->deque without holding the per-object lock for it
19962004
// here; it->deque is only assigned during construction of it.
19972005
dequeobject *deque = it->deque;
1998-
Py_BEGIN_CRITICAL_SECTION2(it, deque);
2006+
Py_BEGIN_CRITICAL_SECTION(deque);
19992007
result = dequeiter_next_lock_held(it, deque);
2000-
Py_END_CRITICAL_SECTION2();
2008+
Py_END_CRITICAL_SECTION();
20012009

20022010
return result;
20032011
}
@@ -2056,10 +2064,10 @@ dequeiter_reduce(PyObject *op, PyObject *Py_UNUSED(dummy))
20562064
// here; it->deque is only assigned during construction of it.
20572065
dequeobject *deque = it->deque;
20582066
Py_ssize_t size, counter;
2059-
Py_BEGIN_CRITICAL_SECTION2(it, deque);
2067+
Py_BEGIN_CRITICAL_SECTION(deque);
20602068
size = Py_SIZE(deque);
20612069
counter = it->counter;
2062-
Py_END_CRITICAL_SECTION2();
2070+
Py_END_CRITICAL_SECTION();
20632071
return Py_BuildValue("O(On)", ty, deque, size - counter);
20642072
}
20652073

@@ -2119,7 +2127,7 @@ dequereviter_next_lock_held(dequeiterobject *it, dequeobject *deque)
21192127
return NULL;
21202128

21212129
if (it->deque->state != it->state) {
2122-
it->counter = 0;
2130+
FT_ATOMIC_STORE_SSIZE_RELAXED(it->counter, 0);
21232131
PyErr_SetString(PyExc_RuntimeError,
21242132
"deque mutated during iteration");
21252133
return NULL;
@@ -2129,7 +2137,7 @@ dequereviter_next_lock_held(dequeiterobject *it, dequeobject *deque)
21292137

21302138
item = it->b->data[it->index];
21312139
it->index--;
2132-
it->counter--;
2140+
FT_ATOMIC_STORE_SSIZE_RELAXED(it->counter, it->counter - 1);
21332141
if (it->index < 0 && it->counter > 0) {
21342142
CHECK_NOT_END(it->b->leftlink);
21352143
it->b = it->b->leftlink;
@@ -2146,9 +2154,10 @@ dequereviter_next(PyObject *self)
21462154
// It's safe to access it->deque without holding the per-object lock for it
21472155
// here; it->deque is only assigned during construction of it.
21482156
dequeobject *deque = it->deque;
2149-
Py_BEGIN_CRITICAL_SECTION2(it, deque);
2157+
// See the comment above dequeiter_next().
2158+
Py_BEGIN_CRITICAL_SECTION(deque);
21502159
item = dequereviter_next_lock_held(it, deque);
2151-
Py_END_CRITICAL_SECTION2();
2160+
Py_END_CRITICAL_SECTION();
21522161
return item;
21532162
}
21542163

0 commit comments

Comments
 (0)