Skip to content
Open
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
26 changes: 26 additions & 0 deletions Lib/test/test_free_threading/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,32 @@ def read_set():
for t in threads:
t.join()

def test_iter_length_hint_mutate(self):
s = set(range(2000))
it = iter(s)
stop = Event()

def reader():
while not stop.is_set():
it.__length_hint__()

def writer():
i = 0
while not stop.is_set():
s.add(i)
s.discard(i - 1)
i += 1

threads = [Thread(target=reader) for _ in range(4)]
threads.append(Thread(target=writer))

for t in threads:
t.start()

stop.set()
Copy link
Contributor

Choose a reason for hiding this comment

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

This means the threads will stop right after they have started. I would prefer the pattern that is used in some other tests in this file: set a constant NUM_LOOPS (determined so that the test < 0.1 seconds, but there still is a decent number of mutations)


for t in threads:
t.join()

@threading_helper.requires_working_threading()
class SmallSetTest(RaceTestBase, unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a data race in ``set_iterator.__length_hint__`` under ``Py_GIL_DISABLED``.
3 changes: 2 additions & 1 deletion Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,8 @@ setiter_len(PyObject *op, PyObject *Py_UNUSED(ignored))
{
setiterobject *si = (setiterobject*)op;
Py_ssize_t len = 0;
if (si->si_set != NULL && si->si_used == si->si_set->used)
PySetObject *so = si->si_set;
Copy link
Contributor

Choose a reason for hiding this comment

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

Here so is a borrowed reference to si->si_set. But si->si_set can be cleared in setiter_iternext (if the iterator is exhausted) outside the critical section.

This is a different mechanism than the corresponding issue, so maybe something to address in another PR. But solving both together is something to consider.

if (so != NULL && si->si_used == FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used))
len = si->len;
return PyLong_FromSsize_t(len);
}
Expand Down
Loading