diff --git a/Lib/test/test_free_threading/test_bytes_object.py b/Lib/test/test_free_threading/test_bytes_object.py index a9017bac6de029..3af36a4524d224 100644 --- a/Lib/test/test_free_threading/test_bytes_object.py +++ b/Lib/test/test_free_threading/test_bytes_object.py @@ -32,6 +32,43 @@ def work(ii): barrier.reset() + def test_bytearray_from_list_item_replaced(self): + # gh-162822: bytearray() must keep list items alive while converting + # them when another thread replaces them. + source = [1 << 1000] + + def writer(): + value = 1 << 1000 + for _ in range(10_000): + source[0] = value + value += 1 + + def reader(): + for _ in range(10_000): + with self.assertRaises(ValueError): + bytearray(source) + + threading_helper.run_concurrently([writer] + [reader] * 4) + + def test_bytearray_from_resizing_list(self): + # gh-162822: bytearray() must safely access a list while another + # thread changes its size. + small = [1] * 100 + large = [1] * 10_000 + source = small.copy() + + def writer(): + for i in range(10_000): + source[:] = large if i % 2 else small + + def reader(): + for _ in range(10_000): + result = bytearray(source) + self.assertIn(len(result), (100, 10_000)) + self.assertEqual(result.count(1), len(result)) + + threading_helper.run_concurrently([writer] + [reader] * 4) + def test_racing_join_replace(self): # gh-158803: join() must not use a list item that another thread # replaces (and frees) concurrently. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-00-00-00.gh-issue-162822.bytearray-list-race.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-00-00-00.gh-issue-162822.bytearray-list-race.rst new file mode 100644 index 00000000000000..f15d6abd85e75a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-00-00-00.gh-issue-162822.bytearray-list-race.rst @@ -0,0 +1,2 @@ +Fix a crash when constructing a :class:`bytearray` from a list that is +modified concurrently in a free-threaded build. diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index b9d547575bd2eb..1f6fa3975b2165 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1113,7 +1113,52 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, return -1; } - if (PyList_CheckExact(arg) || PyTuple_CheckExact(arg)) { + if (PyList_CheckExact(arg)) { + int result = 0; + int use_slowpath = 0; + + Py_BEGIN_CRITICAL_SECTION2(self, arg); + Py_ssize_t size = PyList_GET_SIZE(arg); + if (bytearray_resize_lock_held((PyObject *)self, size) < 0) { + result = -1; + } + else { + char *s = PyByteArray_AS_STRING(self); + for (Py_ssize_t i = 0; i < size; i++) { + PyObject *item = PyList_GET_ITEM(arg, i); + int value; + if (!PyLong_CheckExact(item)) { + /* Resize to 0 and go through slowpath */ + if (bytearray_resize_lock_held((PyObject *)self, 0) < 0) { + result = -1; + } + else { + use_slowpath = 1; + } + break; + } + int rc = _getbytevalue(item, &value); + if (!rc) { + /* Don't expose the uninitialized bytes: reset to empty */ + (void)bytearray_resize_lock_held((PyObject *)self, 0); + result = -1; + break; + } + s[i] = value; + } + } + Py_END_CRITICAL_SECTION2(); + + if (result < 0) { + return -1; + } + if (use_slowpath) { + goto slowpath; + } + return 0; + } + + if (PyTuple_CheckExact(arg)) { Py_ssize_t size = PySequence_Fast_GET_SIZE(arg); if (PyByteArray_Resize((PyObject *)self, size) < 0) { return -1;