Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Lib/test/test_free_threading/test_bytes_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when constructing a :class:`bytearray` from a list that is
modified concurrently in a free-threaded build.
47 changes: 46 additions & 1 deletion Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading