Fix TcpInServiceOut shutdown when the thread is still waiting for the port to become...
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 22 Apr 2011 18:56:08 +0000 (18:56 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 22 Apr 2011 18:56:08 +0000 (18:56 +0000)
bin/inputs/outpeer.py

index 138ab4925d8c024c1eb7b95385207b0f43913e0f..089b20c330bdefd095aee3e4265de37817121cdf 100644 (file)
@@ -126,6 +126,9 @@ class TcpInOutPeer:
 
 
 class TcpInServiceOut(Thread):
+    '''
+    Service that listen to TCP port and create TcpInOutPeer on the fly.
+    '''
     def __init__(self, port):
         Thread.__init__(self)
         self.port = port
@@ -142,6 +145,9 @@ class TcpInServiceOut(Thread):
                 if err.errno == 98: # Address already in use
                     logging.error('TCP port %s already in use. Waiting...', self.port)
                     sleep(1)
+                elif err.errno == 9: # Bad file descriptor
+                    logging.error('TcpInServiceOut socket is not opened. Can\'t bind. Exiting service.')
+                    return
                 else:
                     raise
         logging.info('Bound to socket on port %s for listeners', self.port)
@@ -154,17 +160,19 @@ class TcpInServiceOut(Thread):
                 conn, addr = self.sock.accept()
             except socket.error, err:
                 if err.errno == 22: # Invalid argument
-                    logging.info('TcpInListen socket was closed. Exiting thread.')
+                    logging.info('TcpInServiceOut socket was closed. Exiting thread.')
                     return
-                raise
             logging.info('Incoming TcpInOutPeer %s:%s', formataddr(addr[0]), addr[1])
             tcpin_outpeers.add_peer(TcpInOutPeer(conn, addr[0], addr[1]))
     
     def shutdown(self):
         if self.sock:
-            logging.info('Shuting down tcpinoutpeerlisten socket')
-            self.sock.shutdown(socket.SHUT_RDWR)
-        
+            try:
+                logging.info('Shuting down tcpinoutpeerlisten socket')
+                self.sock.shutdown(socket.SHUT_RDWR)
+            except socket.error, err:
+                logging.info('Ignoring error %s while shuting down TcpInServiceOut socket.', err.errno)
+            self.sock.close()       
 
 class OutPeers:
     '''