Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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
97 changes: 97 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,103 @@ 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

threads = [
Thread(target=reader),
Thread(target=writer),
]
for t in threads:
t.start()
for t in threads:
t.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()

threads = [
Thread(target=reader),
Thread(target=exhauster),
]
for t in threads:
t.start()
for t in threads:
t.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()

threads = [
Thread(target=advancer),
Thread(target=advancer),
Thread(target=producer),
]
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
for t in threads:
t.start()
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``.
69 changes: 59 additions & 10 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,8 +1056,24 @@ 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_SECTION2(op, so);
if (si->si_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_SECTION2();
}
#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 @@ -1089,17 +1105,22 @@ static PyMethodDef setiter_methods[] = {
{NULL, NULL} /* sentinel */
};

static PyObject *setiter_iternext(PyObject *self)
static PyObject *
setiter_iternext(PyObject *self)
{
setiterobject *si = (setiterobject*)self;
PyObject *key = NULL;
Py_ssize_t i, mask;
setentry *entry;
PySetObject *so = si->si_set;
#ifndef Py_GIL_DISABLED
int decref_so = 0;
#endif

if (so == NULL)
if (so == NULL) {
return NULL;
assert (PyAnySet_Check(so));
}
assert(PyAnySet_Check(so));

Py_ssize_t so_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used);
Py_ssize_t si_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(si->si_used);
Expand All @@ -1110,26 +1131,54 @@ static PyObject *setiter_iternext(PyObject *self)
return NULL;
}

#ifdef Py_GIL_DISABLED
Py_BEGIN_CRITICAL_SECTION2(self, so);
i = si->si_pos;
if (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);
si->si_pos = i + 1;
si->len--;
}
else {
si->si_pos = -1;
si->len = 0;
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
}
}
Py_END_CRITICAL_SECTION2();
return key;
#else
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.

i = si->si_pos;
assert(i>=0);
entry = so->table;
mask = so->mask;
while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) {
while (i <= mask &&
(entry[i].key == NULL || entry[i].key == dummy)) {
i++;
}
if (i <= mask) {
key = Py_NewRef(entry[i].key);
si->si_pos = i + 1;
si->len--;
}
Py_END_CRITICAL_SECTION();
si->si_pos = i+1;
if (key == NULL) {
else {
si->si_set = NULL;
decref_so = 1;
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
}
Py_END_CRITICAL_SECTION();

if (decref_so) {
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.

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

PyTypeObject PySetIter_Type = {
Expand Down
Loading