]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129732: Fix race on `shared->array` in qsbr code under free-threading (gh-129738)
authorPeter Hawkins <phawkins@google.com>
Thu, 6 Feb 2025 18:49:29 +0000 (13:49 -0500)
committerGitHub <noreply@github.com>
Thu, 6 Feb 2025 18:49:29 +0000 (18:49 +0000)
The read of `shared->array` should happen under the lock to avoid a race.

Misc/NEWS.d/next/Core_and_Builtins/2025-02-06-17-57-33.gh-issue-129732.yl97oq.rst [new file with mode: 0644]
Python/qsbr.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-02-06-17-57-33.gh-issue-129732.yl97oq.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-06-17-57-33.gh-issue-129732.yl97oq.rst
new file mode 100644 (file)
index 0000000..a4b104a
--- /dev/null
@@ -0,0 +1 @@
+Fixed a race in ``_Py_qsbr_reserve`` in the free threading build.
index a40219acfe2c29463b45b8a49902f59c30760eeb..0df1285cc8e063148fca510bf3761bb843255195 100644 (file)
@@ -205,15 +205,15 @@ _Py_qsbr_reserve(PyInterpreterState *interp)
         }
         _PyEval_StartTheWorld(interp);
     }
-    PyMutex_Unlock(&shared->mutex);
-
-    if (qsbr == NULL) {
-        return -1;
-    }
 
     // Return an index rather than the pointer because the array may be
     // resized and the pointer invalidated.
-    return (struct _qsbr_pad *)qsbr - shared->array;
+    Py_ssize_t index = -1;
+    if (qsbr != NULL) {
+        index = (struct _qsbr_pad *)qsbr - shared->array;
+    }
+    PyMutex_Unlock(&shared->mutex);
+    return index;
 }
 
 void