From c15a0ba540d5a180f78bc830bfd882696154b664 Mon Sep 17 00:00:00 2001 From: Peter Hawkins Date: Thu, 6 Feb 2025 17:12:01 +0000 Subject: [PATCH 1/2] gh-129732: Fix race in on shared->array in qsbr code under free-threading. The read of shared->array should happen under the lock to avoid a race. Fixes https://github.com/python/cpython/issues/129732 --- .../2025-02-06-17-57-33.gh-issue-129732.yl97oq.rst | 1 + Python/qsbr.c | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-02-06-17-57-33.gh-issue-129732.yl97oq.rst 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..89291a5c636299 --- /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 under free-threading mode. 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 From c773ef6bb1ae92e0ed49dd5c6b0bec021d6659b0 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 6 Feb 2025 18:25:52 +0000 Subject: [PATCH 2/2] Edit blurb --- .../2025-02-06-17-57-33.gh-issue-129732.yl97oq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 89291a5c636299..a4b104af61692a 100644 --- 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 @@ -1 +1 @@ -Fixed a race in _Py_qsbr_reserve under free-threading mode. +Fixed a race in ``_Py_qsbr_reserve`` in the free threading build.