Skip to content

Commit 47d75fe

Browse files
committed
Fix data race in setiter_len() under no-gil
setiter_len() was reading so->used without atomic access while concurrent mutations update it atomically under Py_GIL_DISABLED. Use an atomic load for so->used to avoid a data race. This preserves the existing semantics of __length_hint__ while making the access thread-safe. Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
1 parent 96e4cd6 commit 47d75fe

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Objects/setobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,8 @@ setiter_len(PyObject *op, PyObject *Py_UNUSED(ignored))
10561056
{
10571057
setiterobject *si = (setiterobject*)op;
10581058
Py_ssize_t len = 0;
1059-
if (si->si_set != NULL && si->si_used == si->si_set->used)
1059+
PySetObject *so = si->si_set;
1060+
if (so != NULL && si->si_used == FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used))
10601061
len = si->len;
10611062
return PyLong_FromSsize_t(len);
10621063
}

0 commit comments

Comments
 (0)