Skip to content

Commit 3992fba

Browse files
committed
gh-162822: fix bytearray construction from concurrently modified lists
1 parent 31ed3ef commit 3992fba

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

‎Lib/test/test_free_threading/test_bytes_object.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,43 @@ def work(ii):
3232

3333
barrier.reset()
3434

35+
def test_bytearray_from_list_item_replaced(self):
36+
# gh-162822: bytearray() must keep list items alive while converting
37+
# them when another thread replaces them.
38+
source = [1 << 1000]
39+
40+
def writer():
41+
value = 1 << 1000
42+
for _ in range(10_000):
43+
source[0] = value
44+
value += 1
45+
46+
def reader():
47+
for _ in range(10_000):
48+
with self.assertRaises(ValueError):
49+
bytearray(source)
50+
51+
threading_helper.run_concurrently([writer] + [reader] * 4)
52+
53+
def test_bytearray_from_resizing_list(self):
54+
# gh-162822: bytearray() must safely access a list while another
55+
# thread changes its size.
56+
small = [1] * 100
57+
large = [1] * 10_000
58+
source = small.copy()
59+
60+
def writer():
61+
for i in range(10_000):
62+
source[:] = large if i % 2 else small
63+
64+
def reader():
65+
for _ in range(10_000):
66+
result = bytearray(source)
67+
self.assertIn(len(result), (100, 10_000))
68+
self.assertEqual(result.count(1), len(result))
69+
70+
threading_helper.run_concurrently([writer] + [reader] * 4)
71+
3572
def test_racing_join_replace(self):
3673
# gh-158803: join() must not use a list item that another thread
3774
# replaces (and frees) concurrently.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when constructing a :class:`bytearray` from a list that is
2+
modified concurrently in a free-threaded build.

‎Objects/bytearrayobject.c‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,52 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
11131113
return -1;
11141114
}
11151115

1116-
if (PyList_CheckExact(arg) || PyTuple_CheckExact(arg)) {
1116+
if (PyList_CheckExact(arg)) {
1117+
int result = 0;
1118+
int use_slowpath = 0;
1119+
1120+
Py_BEGIN_CRITICAL_SECTION2(self, arg);
1121+
Py_ssize_t size = PyList_GET_SIZE(arg);
1122+
if (bytearray_resize_lock_held((PyObject *)self, size) < 0) {
1123+
result = -1;
1124+
}
1125+
else {
1126+
char *s = PyByteArray_AS_STRING(self);
1127+
for (Py_ssize_t i = 0; i < size; i++) {
1128+
PyObject *item = PyList_GET_ITEM(arg, i);
1129+
int value;
1130+
if (!PyLong_CheckExact(item)) {
1131+
/* Resize to 0 and go through slowpath */
1132+
if (bytearray_resize_lock_held((PyObject *)self, 0) < 0) {
1133+
result = -1;
1134+
}
1135+
else {
1136+
use_slowpath = 1;
1137+
}
1138+
break;
1139+
}
1140+
int rc = _getbytevalue(item, &value);
1141+
if (!rc) {
1142+
/* Don't expose the uninitialized bytes: reset to empty */
1143+
(void)bytearray_resize_lock_held((PyObject *)self, 0);
1144+
result = -1;
1145+
break;
1146+
}
1147+
s[i] = value;
1148+
}
1149+
}
1150+
Py_END_CRITICAL_SECTION2();
1151+
1152+
if (result < 0) {
1153+
return -1;
1154+
}
1155+
if (use_slowpath) {
1156+
goto slowpath;
1157+
}
1158+
return 0;
1159+
}
1160+
1161+
if (PyTuple_CheckExact(arg)) {
11171162
Py_ssize_t size = PySequence_Fast_GET_SIZE(arg);
11181163
if (PyByteArray_Resize((PyObject *)self, size) < 0) {
11191164
return -1;

0 commit comments

Comments
 (0)