https://github.com/python/cpython/commit/ec3aa6ab4847ebd6f8f3e9b2b7023c5ab3a6e39a
commit: ec3aa6ab4847ebd6f8f3e9b2b7023c5ab3a6e39a
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-05-16T09:06:14Z
summary:

[3.15] gh-149816: Fix a race condition in `_PyBytes_FromList` with 
free-threading (GH-149909) (#149911)

gh-149816: Fix a race condition in `_PyBytes_FromList` with free-threading 
(GH-149909)
(cherry picked from commit 46afba7b9324bc9492c3527d0fe47dd74f1f598c)

Co-authored-by: sobolevn <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst
M Objects/bytesobject.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst
new file mode 100644
index 00000000000000..d35f0857a1aefe
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst
@@ -0,0 +1 @@
+Fix a race condition in ``_PyBytes_FromList`` in free-threading mode.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 8a9d1b133affb3..2d694922557429 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -11,6 +11,7 @@
 #include "pycore_global_objects.h"// _Py_GET_GLOBAL_OBJECT()
 #include "pycore_initconfig.h"    // _PyStatus_OK()
 #include "pycore_long.h"          // _PyLong_DigitValue
+#include "pycore_list.h"          // _PyList_GetItemRef
 #include "pycore_object.h"        // _PyObject_GC_TRACK
 #include "pycore_pymem.h"         // PYMEM_CLEANBYTE
 #include "pycore_strhex.h"        // _Py_strhex_with_sep()
@@ -2991,8 +2992,10 @@ _PyBytes_FromList(PyObject *x)
     size = _PyBytesWriter_GetAllocated(writer);
 
     for (Py_ssize_t i = 0; i < PyList_GET_SIZE(x); i++) {
-        PyObject *item = PyList_GET_ITEM(x, i);
-        Py_INCREF(item);
+        PyObject *item = _PyList_GetItemRef((PyListObject *)x, i);
+        if (item == NULL) {
+            goto error;
+        }
         Py_ssize_t value = PyNumber_AsSsize_t(item, NULL);
         Py_DECREF(item);
         if (value == -1 && PyErr_Occurred())

_______________________________________________
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