Skip to content

gh-135862: fix asyncio socket partial writes of non-1d-binary arrays #135974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

duaneg
Copy link
Contributor

@duaneg duaneg commented Jun 26, 2025

The asyncio code is writing binary data to a socket.socket, and advancing through the buffer after a partial write by doing memoryview(data)[n:], where n is the number of bytes written.

This assumes the memoryview is a one dimensional buffer of bytes, which is not the case e.g. for a memoryview of an array.array of non-bytes, or an ndarray with more than one dimension. Partial writes of such memoryviews will corrupt the stream, repeating or omitting some data.

When creating a memoryview from a memoryview, first convert it to a one-dimensional array of bytes before taking the remaining slice, thus avoiding these issues.

Add an assertion that the remaining data is bytes, to guard against possible regressions if additional types of data are allowed in the future.

…rrays

The :mod:`asyncio` code is writing binary data to a :class:`socket.socket`, and
advancing through the buffer after a partial write by doing
``memoryview(data)[n:]``, where ``n`` is the number of bytes written.

This assumes the :class:`memoryview` is a one dimensional buffer of bytes,
which is not the case e.g. for a :class:`memoryview` of an :class:`array.array`
of non-bytes, or an ``ndarray`` with more than one dimension. Partial writes of
such :class:`memoryview`s will corrupt the stream, repeating or omitting some
data.

When creating a :class:`memoryview` from a :class:`memoryview`, first convert
it to a one-dimensional array of bytes before taking the remaining slice, thus
avoiding these issues.

Add an assertion that the remaining data is bytes, to guard against possible
regressions if additional types of data are allowed in the future.
duaneg added 2 commits June 26, 2025 23:26
Hopefully this will fix the windows compile failures...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant