]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-129643: fix thread safety of `PyList_SetItem` (#129644) (#129677)
authorKumar Aditya <kumaraditya@python.org>
Wed, 5 Feb 2025 08:09:37 +0000 (13:39 +0530)
committerGitHub <noreply@github.com>
Wed, 5 Feb 2025 08:09:37 +0000 (08:09 +0000)
gh-129643: fix thread safety of `PyList_SetItem` (#129644)

Misc/NEWS.d/next/Core and Builtins/2025-02-04-12-42-40.gh-issue-129643.K24Zow.rst [new file with mode: 0644]
Objects/listobject.c

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 (file)
index 0000000..27dd3b7
--- /dev/null
@@ -0,0 +1 @@
+Fix thread safety of :c:func:`PyList_SetItem` in free-threading builds. Patch by Kumar Aditya.
index 31ec8d5e05cf5066501697d2336782f7e991de36..d581fc8b0b290cc0e2ddd7ab66727d80cca3f4e9 100644 (file)
@@ -451,7 +451,6 @@ int
 PyList_SetItem(PyObject *op, Py_ssize_t i,
                PyObject *newitem)
 {
-    PyObject **p;
     if (!PyList_Check(op)) {
         Py_XDECREF(newitem);
         PyErr_BadInternalCall();
@@ -467,8 +466,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();