diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Aw61LR.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Aw61LR.rst new file mode 100644 index 00000000000000..5de64814b1ca55 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Aw61LR.rst @@ -0,0 +1 @@ +Fix a race in ``list.__sizeof__()`` when a list is concurrently resized on a free-threaded build. diff --git a/Objects/listobject.c b/Objects/listobject.c index b6ebb2b53fc304..c09d4618551e12 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -3602,10 +3602,12 @@ list___sizeof___impl(PyListObject *self) { size_t res = _PyObject_SIZE(Py_TYPE(self)); #ifdef Py_GIL_DISABLED - PyObject **ob_item = _Py_atomic_load_ptr(&self->ob_item); + Py_BEGIN_CRITICAL_SECTION(self); + PyObject **ob_item = self->ob_item; if (ob_item != NULL) { res += list_capacity(ob_item) * sizeof(PyObject *); } + Py_END_CRITICAL_SECTION(); #else res += (size_t)self->allocated * sizeof(PyObject *); #endif