Skip to content
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

Issue #153: Aio write_callback to handle EAGAIN #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
55 changes: 31 additions & 24 deletions dbus_next/aio/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import socket
from copy import copy
from typing import Optional
import errno


def _future_set_exception(fut, exc):
Expand Down Expand Up @@ -43,32 +44,38 @@ def __init__(self, bus):
def write_callback(self):
try:
while True:
if self.buf is None:
if self.messages.qsize() == 0:
# nothing more to write
self.loop.remove_writer(self.fd)
try:
if self.buf is None:
if self.messages.qsize() == 0:
# nothing more to write
self.loop.remove_writer(self.fd)
return
buf, unix_fds, fut = self.messages.get_nowait()
self.unix_fds = unix_fds
self.buf = memoryview(buf)
self.offset = 0
self.fut = fut

if self.unix_fds and self.negotiate_unix_fd:
ancdata = [(socket.SOL_SOCKET, socket.SCM_RIGHTS,
array.array("i", self.unix_fds))]
self.offset += self.sock.sendmsg([self.buf[self.offset:]], ancdata)
self.unix_fds = None
else:
self.offset += self.sock.send(self.buf[self.offset:])

if self.offset >= len(self.buf):
# finished writing
self.buf = None
_future_set_result(self.fut, None)
else:
# wait for writable
return
buf, unix_fds, fut = self.messages.get_nowait()
self.unix_fds = unix_fds
self.buf = memoryview(buf)
self.offset = 0
self.fut = fut

if self.unix_fds and self.negotiate_unix_fd:
ancdata = [(socket.SOL_SOCKET, socket.SCM_RIGHTS,
array.array("i", self.unix_fds))]
self.offset += self.sock.sendmsg([self.buf[self.offset:]], ancdata)
self.unix_fds = None
else:
self.offset += self.sock.send(self.buf[self.offset:])
except OSError as e:
if e.errno == errno.EAGAIN:
return
raise

if self.offset >= len(self.buf):
# finished writing
self.buf = None
_future_set_result(self.fut, None)
else:
# wait for writable
return
except Exception as e:
_future_set_exception(self.fut, e)
self.bus._finalize(e)
Expand Down