Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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):
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
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
Comment thread
eendebakpt marked this conversation as resolved.
Outdated

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):
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
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)
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
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
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
PySetObject *so = si->si_set;
Comment thread
hyongtao-code marked this conversation as resolved.
if (so != NULL) {
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
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))
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
{
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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On normal builds Py_BEGIN_CRITICAL_SECTION is a no-op, you either want Py_BEGIN_CRITICAL_SECTION or Py_BEGIN_CRITICAL_SECTION2, not both.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi, thanks for your review. I switched this to always use Py_BEGIN_CRITICAL_SECTION2(self, so) / Py_END_CRITICAL_SECTION2().

The remaining #ifdef Py_GIL_DISABLED around the done label is a bit awkward, but it preserves the existing control flow.

#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;
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
}
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
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, i + 1);
#else
si->si_pos = i + 1;
#endif
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
si->len--;
}
else {
#ifdef Py_GIL_DISABLED
/* free-threaded: keep si_set; just mark exhausted */
FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, -1);
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
si->len = 0;
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
#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
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
Py_DECREF(so);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you move the decref to the section

       /* exhausted */
        si->si_pos = -1;
        si->len = 0;

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I’d prefer not to move the Py_DECREF into the Py_BEGIN_CRITICAL_SECTION block.

#endif
return NULL;
}
si->len--;
return key;
}

Expand Down
Loading