Commit 6f6f5892 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

[trac420] Fix some portability issues

* kqueue maybe doesn't survive fork on MacOS
* add_kqueue_socket wanted socket, not fileno
* Some systems don't have MSG_DONTWAIT, so we set the socket nonblocking
parent a2a812ed
......@@ -323,12 +323,18 @@ class MsgQ:
def __send_data(self, sock, data):
try:
return sock.send(data, socket.MSG_DONTWAIT)
# We set the socket nonblocking, MSG_DONTWAIT doesn't exist
# on some OSes
sock.setblocking(0)
return sock.send(data)
except socket.error as e:
if e.errno == errno.EAGAIN or e.errno == errno.EWOULDBLOCK:
return 0
else:
raise e
finally:
# And set it back again
sock.setblocking(1)
def send_prepared_msg(self, sock, msg):
# Try to send the data, but only if there's nothing waiting
......@@ -355,7 +361,7 @@ class MsgQ:
self.poller.register(fileno, select.POLLIN |
select.POLLOUT)
else:
self.add_kqueue_socket(fileno, True)
self.add_kqueue_socket(sock, True)
self.sendbuffs[fileno] = (last_sent, buff)
def __process_write(self, fileno):
......@@ -370,7 +376,7 @@ class MsgQ:
if self.poller:
self.poller.register(fileno, select.POLLIN)
else:
self.add_kqueue_socket(fileno)
self.add_kqueue_socket(sock)
del self.sendbuffs[fileno]
else:
self.sendbuffs[fileno] = (time.clock(), msg)
......
......@@ -183,7 +183,6 @@ class SendNonblock(unittest.TestCase):
Tries that sending a command many times and getting an answer works.
"""
msgq = MsgQ()
msgq.setup_poller()
# msgq.run needs to compare with the listen_socket, so we provide
# a replacement
class DummySocket:
......@@ -196,6 +195,7 @@ class SendNonblock(unittest.TestCase):
queue_pid = os.fork()
if queue_pid == 0:
signal.alarm(30)
msgq.setup_poller()
msgq.register_socket(queue)
msgq.run()
else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment