From 06f5dde2b0d0941dc1a5f82b175fb8a527a4377c Mon Sep 17 00:00:00 2001 From: Jake Huneau Date: Sun, 29 Nov 2026 00:28:30 +0000 Subject: [PATCH 1/2] gh-162822: Fix bytearray(list) race in the free-threaded build bytearray.__init__() read the items of an exact list through PySequence_Fast_ITEMS() without holding the list's lock, so another thread could replace and free an item while it was being converted. Run the fast path under Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST, as bytes(list) already does. --- .../test_free_threading/test_bytes_object.py | 21 ++++++ ...-11-29-00-26-15.gh-issue-162822.JDV2qA.rst | 3 + Objects/bytearrayobject.c | 69 ++++++++++++------- 3 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-00-26-15.gh-issue-162822.JDV2qA.rst diff --git a/Lib/test/test_free_threading/test_bytes_object.py b/Lib/test/test_free_threading/test_bytes_object.py index a9017bac6de0295..b534b5ece8aaad4 100644 --- a/Lib/test/test_free_threading/test_bytes_object.py +++ b/Lib/test/test_free_threading/test_bytes_object.py @@ -54,6 +54,27 @@ def reader(): threading_helper.run_concurrently([writer] + [reader] * 4) + def test_racing_bytearray_from_list_replace(self): + # gh-162822: bytearray(list) must not use a list item that another + # thread replaces (and frees) concurrently. + big = 1 << 1000 + lst = [big] * 10 + done = Event() + + def writer(): + try: + for n in range(100000): + lst[n % len(lst)] = big + n + finally: + done.set() + + def reader(): + while not done.is_set(): + with self.assertRaises(ValueError): + bytearray(lst) + + threading_helper.run_concurrently([writer] + [reader] * 4) + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-00-26-15.gh-issue-162822.JDV2qA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-00-26-15.gh-issue-162822.JDV2qA.rst new file mode 100644 index 000000000000000..c2b69b3626f0ad5 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-00-26-15.gh-issue-162822.JDV2qA.rst @@ -0,0 +1,3 @@ +Fix a crash in :class:`bytearray` construction from a :class:`list` in the +:term:`free-threaded build` when another thread concurrently replaces an item +of the list. diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index b9d547575bd2eba..482c410fdead471 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -984,6 +984,42 @@ bytearray_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return op; } +/* Fill self from an exact list or tuple of exact ints. Return 0 on success, + -1 on error and 1 if an item is not an exact int, in which case self is left + empty and the caller falls back to the iterator. For a list, the caller + must hold its critical section so that no other thread can replace (and + free) an item while it is read. */ +static int +bytearray_init_from_sequence_lock_held(PyByteArrayObject *self, PyObject *arg) +{ + Py_ssize_t size = PySequence_Fast_GET_SIZE(arg); + if (PyByteArray_Resize((PyObject *)self, size) < 0) { + return -1; + } + PyObject **items = PySequence_Fast_ITEMS(arg); + char *s = PyByteArray_AS_STRING(self); + for (Py_ssize_t i = 0; i < size; i++) { + int value; + if (!PyLong_CheckExact(items[i])) { + /* Resize to 0 and go through the slow path */ + if (Py_SIZE(self) != 0) { + if (PyByteArray_Resize((PyObject *)self, 0) < 0) { + return -1; + } + } + return 1; + } + int rc = _getbytevalue(items[i], &value); + if (!rc) { + /* Don't expose the uninitialized bytes: reset to empty */ + (void)PyByteArray_Resize((PyObject *)self, 0); + return -1; + } + s[i] = value; + } + return 0; +} + /*[clinic input] @vectorcall bytearray.__init__ @@ -1114,34 +1150,15 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, } if (PyList_CheckExact(arg) || PyTuple_CheckExact(arg)) { - Py_ssize_t size = PySequence_Fast_GET_SIZE(arg); - if (PyByteArray_Resize((PyObject *)self, size) < 0) { - return -1; + int rc; + Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(arg); + rc = bytearray_init_from_sequence_lock_held(self, arg); + Py_END_CRITICAL_SECTION_SEQUENCE_FAST(); + if (rc <= 0) { + return rc; } - PyObject **items = PySequence_Fast_ITEMS(arg); - char *s = PyByteArray_AS_STRING(self); - for (Py_ssize_t i = 0; i < size; i++) { - int value; - if (!PyLong_CheckExact(items[i])) { - /* Resize to 0 and go through slowpath */ - if (Py_SIZE(self) != 0) { - if (PyByteArray_Resize((PyObject *)self, 0) < 0) { - return -1; - } - } - goto slowpath; - } - int rc = _getbytevalue(items[i], &value); - if (!rc) { - /* Don't expose the uninitialized bytes: reset to empty */ - (void)PyByteArray_Resize((PyObject *)self, 0); - return -1; - } - s[i] = value; - } - return 0; + /* An item is not an exact int: go through the slow path */ } -slowpath: /* Get the iterator */ it = PyObject_GetIter(arg); if (it == NULL) { From 6439ee444a8c7eb04ad82aeb1bef96939286f58a Mon Sep 17 00:00:00 2001 From: Jake Huneau Date: Sun, 29 Nov 2026 07:09:18 -0500 Subject: [PATCH 2/2] Update bytearrayobject.c Co-authored-by: sobolevn --- Objects/bytearrayobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 482c410fdead471..f74cf7d9fc0197f 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1157,7 +1157,7 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, if (rc <= 0) { return rc; } - /* An item is not an exact int: go through the slow path */ + // This is not a sequence of exact ints, going to slow path } /* Get the iterator */ it = PyObject_GetIter(arg);