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
85 changes: 85 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,91 @@ def read_set():
for t in threads:
t.join()

def test_length_hint_used_race(self):
s = set(range(2000))
it = iter(s)

NUM_LOOPS = 50_000
barrier = Barrier(2)

def reader():
barrier.wait()
for _ in range(NUM_LOOPS):
it.__length_hint__()

def writer():
barrier.wait()
i = 0
for _ in range(NUM_LOOPS):
s.add(i)
s.discard(i - 1)
i += 1

t1 = Thread(target=reader)
t2 = Thread(target=writer)
t1.start(); t2.start()
t1.join(); t2.join()

def test_length_hint_exhaust_race(self):
NUM_LOOPS = 10_000
INNER_HINTS = 20
barrier = Barrier(2)
box = {"it": None}

def exhauster():
for _ in range(NUM_LOOPS):
s = set(range(256))
box["it"] = iter(s)
barrier.wait() # start together
try:
while True:
next(box["it"])
except StopIteration:
pass
barrier.wait() # end iteration

def reader():
for _ in range(NUM_LOOPS):
barrier.wait()
it = box["it"]
for _ in range(INNER_HINTS):
it.__length_hint__()
barrier.wait()

t1 = Thread(target=reader)
t2 = Thread(target=exhauster)
t1.start(); t2.start()
t1.join(); t2.join()

def test_iternext_concurrent_exhaust_race(self):
NUM_LOOPS = 20_000
barrier = Barrier(3)
box = {"it": None}

def advancer():
for _ in range(NUM_LOOPS):
barrier.wait()
it = box["it"]
while True:
try:
next(it)
except StopIteration:
break
barrier.wait()

def producer():
for _ in range(NUM_LOOPS):
s = set(range(64))
box["it"] = iter(s)
barrier.wait()
barrier.wait()

t1 = Thread(target=advancer)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use the same style for starting and stopping threads as the other tests in this file (e.g. test_contains_hash_mutate)

t2 = Thread(target=advancer)
t3 = Thread(target=producer)
t1.start(); t2.start(); t3.start()
t1.join(); t2.join(); t3.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``.
73 changes: 62 additions & 11 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,8 +1056,23 @@ 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)
#ifdef Py_GIL_DISABLED
Copy link
Contributor

Choose a reason for hiding this comment

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

This might work for setiter_len, but setiter_iternext itself is not yet thread safe (also because of setting si->si_set to zero).

For several other iterations the approach is to keep the reference si->si_set , but use another attribute to signal exhaustion of the iterator. For example for itertools.cycle or the reversed operator.

Note: I tried creating a minimal example where concurrent iteration fails, but I have succeeded yet (the example does not crash, although I have not run thread sanitizer on it yet)

Test for concurrent iteration on set iterator
import unittest
from threading import Thread, Barrier


class TestSetIter(unittest.TestCase):
    def test_set_iter(self):
        """Test concurrent iteration over a set"""

        NUM_LOOPS = 10_000
        NUM_THREADS = 4
        

        for ii in range(NUM_LOOPS):
            if ii % 1000 ==0:
                print(f'test_set_iter {ii}')
            barrier = Barrier(NUM_THREADS)
            
            # make sure the underlying set is unique referenced by the iterator
            iterator = iter(set((1,2,))) 
            
            def worker():
                barrier.wait()
                while True:
                    iterator.__length_hint__()
                    try:
                        next(iterator)
                    except StopIteration:
                        break

                
            threads = [Thread(target=worker) for _ in range(NUM_THREADS)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
                
            assert iterator.__length_hint__()==0

if __name__ == "__main__":
    unittest.main()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. I think your points make a lot of sense, and I really appreciate the two links you shared—they helped me get a more complete picture of the iterator-related data race.
I’ll try to construct the case you mentioned under a TSan environment.
If it turns out to be appropriate, we can address it fully in this PR, that would be great. Of course, this will take some time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we should fix this like we have fixed others and as Sam suggested only clear the associated set in non-free-threading builds. The current code is incorrect because it uses try incref which can fail spuriously if the set object is not marked to enable try incref.

PySetObject *so = si->si_set;
if (so != NULL) {
Py_BEGIN_CRITICAL_SECTION(so);
Py_ssize_t pos = FT_ATOMIC_LOAD_SSIZE_RELAXED(si->si_pos);
if (pos >= 0 &&
si->si_used == FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used))
{
len = si->len;
}
Py_END_CRITICAL_SECTION();
}
#else
if (si->si_set != NULL && si->si_used == si->si_set->used) {
len = si->len;
}
#endif
return PyLong_FromSsize_t(len);
}

Expand Down Expand Up @@ -1096,6 +1111,7 @@ static PyObject *setiter_iternext(PyObject *self)
Py_ssize_t i, mask;
setentry *entry;
PySetObject *so = si->si_set;
int exhausted = 0;

if (so == NULL)
return NULL;
Expand All @@ -1111,24 +1127,59 @@ static PyObject *setiter_iternext(PyObject *self)
}

Py_BEGIN_CRITICAL_SECTION(so);
#ifdef Py_GIL_DISABLED
/* si_pos may be read outside the lock; keep it atomic in FT builds */
i = FT_ATOMIC_LOAD_SSIZE_RELAXED(si->si_pos);
if (i < 0) {
/* iterator already exhausted */
goto done;
}
#else
i = si->si_pos;
assert(i>=0);
entry = so->table;
mask = so->mask;
while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) {
i++;
if (i < 0) {
/* iterator already exhausted */
exhausted = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
exhausted = 1;
return NULL;

(the exhausted variable is then not needed any more I believe)

Copy link
Contributor

Choose a reason for hiding this comment

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

You cannot directly return as it would skip ending the critical section

}
if (i <= mask) {
key = Py_NewRef(entry[i].key);
#endif

if (!exhausted) {
assert(i >= 0);
entry = so->table;
mask = so->mask;
while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) {
i++;
}
if (i <= mask) {
key = Py_NewRef(entry[i].key);
#ifdef Py_GIL_DISABLED
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should follow the pattern that we use in other iterators: don't clear si->si_set when the iterator is exhausted in the free-threaded build.

That will keep other things simpler.

FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, i + 1);
#else
si->si_pos = i + 1;
#endif
Comment on lines +1154 to +1158
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#ifdef Py_GIL_DISABLED
FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, i + 1);
#else
si->si_pos = i + 1;
#endif
FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, i + 1);

On the normal build the macro will expand to si->si_pos = i + 1;

si->len--;
}
else {
#ifdef Py_GIL_DISABLED
/* free-threaded: keep si_set; just mark exhausted */
FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, -1);
Copy link
Contributor

Choose a reason for hiding this comment

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

The value -1 written here could be overwritten by a concurrent thread (at line 1155). Which means that over exhaustion of the set iterator it is restored back to life. This does not lead to overflows or other issues (afaic), but is a bit odd behaviour.

si->len = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

This (and some other places) should also be atomic?

#else
si->si_set = NULL;
#endif
}
}

#ifdef Py_GIL_DISABLED
done:
#endif
Py_END_CRITICAL_SECTION();
si->si_pos = i+1;

if (key == NULL) {
si->si_set = NULL;
#ifndef Py_GIL_DISABLED
Copy link
Contributor

Choose a reason for hiding this comment

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

I think in the normal build you still have to do si->si_set = NULL;, otherwise the si->si_set is decref'ed again in setiter_dealloc.

Py_DECREF(so);
#endif
return NULL;
}
si->len--;
return key;
}

Expand Down
Loading