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
27 changes: 27 additions & 0 deletions Lib/test/test_free_threading/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ def sizeof(barrier, b, *ignore):
class CBytesIOTest(ThreadSafetyMixin, TestCase):
ioclass = io.BytesIO

@threading_helper.requires_working_threading()
@threading_helper.reap_threads
def test_concurrent_whole_buffer_read_and_resize(self):
shared = self.ioclass(b"x" * 64)
writers = 2
readers = 8
loops = 2000
barrier = threading.Barrier(writers + readers)

def writer():
barrier.wait()
for i in range(loops):
shared.seek(0)
shared.write(b"a" * (64 + (i & 63)))

def reader():
barrier.wait()
for _ in range(loops):
shared.seek(0)
shared.read()
shared.getvalue()

threads = [threading.Thread(target=writer) for _ in range(writers)]
threads += [threading.Thread(target=reader) for _ in range(readers)]
with threading_helper.start_threads(threads):
pass

class PyBytesIOTest(ThreadSafetyMixin, TestCase):
ioclass = pyio.BytesIO

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a data race in :class:`io.BytesIO` in free-threaded builds when
whole-buffer reads or :meth:`~io.BytesIO.getvalue` share the internal buffer
with concurrent writes.
7 changes: 7 additions & 0 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ _io_BytesIO_getvalue_impl(bytesio *self)
/*[clinic end generated code: output=b3f6a3233c8fd628 input=c91bff398df0c352]*/
{
CHECK_CLOSED(self);
#ifdef Py_GIL_DISABLED
return PyBytes_FromStringAndSize(PyBytes_AS_STRING(self->buf),
self->string_size);
#else
if (self->string_size <= 1 || FT_ATOMIC_LOAD_SSIZE_RELAXED(self->exports) > 0)
return PyBytes_FromStringAndSize(PyBytes_AS_STRING(self->buf),
self->string_size);
Expand All @@ -386,6 +390,7 @@ _io_BytesIO_getvalue_impl(bytesio *self)
}
}
return Py_NewRef(self->buf);
#endif
}

/*[clinic input]
Expand Down Expand Up @@ -429,12 +434,14 @@ read_bytes_lock_held(bytesio *self, Py_ssize_t size)

assert(self->buf != NULL);
assert(size <= self->string_size);
#ifndef Py_GIL_DISABLED
if (size > 1 &&
self->pos == 0 && size == PyBytes_GET_SIZE(self->buf) &&
FT_ATOMIC_LOAD_SSIZE_RELAXED(self->exports) == 0) {
self->pos += size;
return Py_NewRef(self->buf);
}
#endif

/* gh-141311: Avoid undefined behavior when self->pos (limit PY_SSIZE_T_MAX)
is beyond the size of self->buf. Assert above validates size is always in
Expand Down
Loading