Browse Source

[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
Michal 'vorner' Vaner 14 years ago
parent
commit
6f6f589204
2 changed files with 10 additions and 4 deletions
  1. 9 3
      src/bin/msgq/msgq.py.in
  2. 1 1
      src/bin/msgq/tests/msgq_test.py

+ 9 - 3
src/bin/msgq/msgq.py.in

@@ -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)

+ 1 - 1
src/bin/msgq/tests/msgq_test.py

@@ -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: