Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
16 changes: 16 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2908,6 +2908,22 @@ def check(funcs, it):
check([iter_next] + [iter_reduce] * 10, iter(ba)) # for tsan
check([iter_next] + [iter_setstate] * 10, iter(ba)) # for tsan

@unittest.skipUnless(support.Py_GIL_DISABLED, 'this test can only possibly fail with GIL disabled')
@threading_helper.reap_threads
@threading_helper.requires_working_threading()
def test_free_threading_bytearray_resize(self):
def resize_stress(ba):
for _ in range(100_000):
try:
ba.resize(10_000)
ba.resize(1)
except (BufferError, ValueError):
pass

ba = bytearray(100)
threads = [threading.Thread(target=resize_stress, args=(ba,)) for _ in range(4)]
with threading_helper.start_threads(threads):
pass

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Make :meth:`bytearray.resize` thread-safe in the free-threaded build by
using a critical section and calling the lock-held variant of the resize
function.
5 changes: 3 additions & 2 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,7 @@ bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix)


/*[clinic input]
@critical_section
bytearray.resize
size: Py_ssize_t
New size to resize to.
Expand All @@ -1515,10 +1516,10 @@ Resize the internal buffer of bytearray to len.

static PyObject *
bytearray_resize_impl(PyByteArrayObject *self, Py_ssize_t size)
/*[clinic end generated code: output=f73524922990b2d9 input=6c9a260ca7f72071]*/
/*[clinic end generated code: output=f73524922990b2d9 input=116046316a2b5cfc]*/
{
Py_ssize_t start_size = PyByteArray_GET_SIZE(self);
int result = PyByteArray_Resize((PyObject *)self, size);
int result = bytearray_resize_lock_held((PyObject *)self, size);
if (result < 0) {
return NULL;
}
Expand Down
4 changes: 3 additions & 1 deletion Objects/clinic/bytearrayobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading