https://github.com/python/cpython/commit/f7cc8623457ed0f79bddd32dd680303584684641
commit: f7cc8623457ed0f79bddd32dd680303584684641
branch: 3.13
author: Sam Gross <[email protected]>
committer: colesbury <[email protected]>
date: 2025-02-06T14:30:16-05:00
summary:

[3.13] gh-129732: Fix race on `shared->array` in qsbr code under free-threading 
(gh-129738) (gh-129747)

The read of `shared->array` should happen under the lock to avoid a race.
(cherry picked from commit b4ff8b22b3066b814c3758f87eaddfa923e657ed)

Co-authored-by: Peter Hawkins <[email protected]>

files:
A Misc/NEWS.d/next/Core and 
Builtins/2025-02-06-17-57-33.gh-issue-129732.yl97oq.rst
M 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
index 00000000000000..a4b104af61692a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2025-02-06-17-57-33.gh-issue-129732.yl97oq.rst 
@@ -0,0 +1 @@
+Fixed a race in ``_Py_qsbr_reserve`` in the free threading build.
diff --git a/Python/qsbr.c b/Python/qsbr.c
index a40219acfe2c29..0df1285cc8e063 100644
--- a/Python/qsbr.c
+++ b/Python/qsbr.c
@@ -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

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to