From: Kumar Aditya Date: Wed, 5 Feb 2025 07:38:02 +0000 (+0530) Subject: gh-129643: fix thread safety of `PyList_SetItem` (#129644) X-Git-Tag: v3.14.0a5~106 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fb5d1c923677e7982360bad934d70cf9ad3366ca;p=thirdparty%2FPython%2Fcpython.git gh-129643: fix thread safety of `PyList_SetItem` (#129644) --- diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-02-04-12-42-40.gh-issue-129643.K24Zow.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-04-12-42-40.gh-issue-129643.K24Zow.rst new file mode 100644 index 000000000000..27dd3b7f652a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-04-12-42-40.gh-issue-129643.K24Zow.rst @@ -0,0 +1 @@ +Fix thread safety of :c:func:`PyList_SetItem` in free-threading builds. Patch by Kumar Aditya. diff --git a/Objects/listobject.c b/Objects/listobject.c index f4a269e4d7b2..86fa21495564 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -419,7 +419,6 @@ int PyList_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem) { - PyObject **p; if (!PyList_Check(op)) { Py_XDECREF(newitem); PyErr_BadInternalCall(); @@ -435,8 +434,9 @@ PyList_SetItem(PyObject *op, Py_ssize_t i, ret = -1; goto end; } - p = self->ob_item + i; - Py_XSETREF(*p, newitem); + PyObject *tmp = self->ob_item[i]; + FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item[i], newitem); + Py_XDECREF(tmp); ret = 0; end:; Py_END_CRITICAL_SECTION();