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

def test_free_threading_bytearray_resize(self):
Comment thread
kumaraditya303 marked this conversation as resolved.
def resize_stress(ba):
for _ in range(100_000):
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
try:
ba.resize(10_000)
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
ba.resize(1)
except (BufferError, ValueError):
pass

ba = bytearray(100)
threads = [threading.Thread(target=resize_stress, args=(ba,)) for _ in range(4)]
Comment thread
KowalskiThomas marked this conversation as resolved.
for t in threads: t.start()
for t in threads: t.join()

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