Skip to content

bpo-35589: Prevent buffer copy in sock_sendall() #11418

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

Merged
merged 3 commits into from
May 16, 2019
Merged
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
17 changes: 10 additions & 7 deletions Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,32 +428,35 @@ async def sock_sendall(self, sock, data):
if n == len(data):
# all data sent
return
else:
data = bytearray(memoryview(data)[n:])

fut = self.create_future()
fd = sock.fileno()
fut.add_done_callback(
functools.partial(self._sock_write_done, fd))
self.add_writer(fd, self._sock_sendall, fut, sock, data)
# use a trick with a list in closure to store a mutable state
self.add_writer(fd, self._sock_sendall, fut, sock,
memoryview(data), [n])
return await fut

def _sock_sendall(self, fut, sock, data):
def _sock_sendall(self, fut, sock, view, pos):
if fut.done():
# Future cancellation can be scheduled on previous loop iteration
return
start = pos[0]
try:
n = sock.send(data)
n = sock.send(view[start:])
except (BlockingIOError, InterruptedError):
return
except Exception as exc:
fut.set_exception(exc)
return

if n == len(data):
start += n

if start == len(view):
fut.set_result(None)
else:
del data[:n]
pos[0] = start

async def sock_connect(self, sock, address):
"""Connect to a remote socket at address.
Expand Down