Skip to content
Open
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
Next Next commit
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>
  • Loading branch information
hyongtao-code committed Jan 31, 2026
commit 47d75fee96b929343e8713e98e3ce9400b34a15c
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;
Comment thread
hyongtao-code marked this conversation as resolved.
if (so != NULL && si->si_used == FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used))
len = si->len;
return PyLong_FromSsize_t(len);
}
Expand Down