Search code examples
pythonmultithreadingasyncsocket

Catch Interrupted system call in threading


I have a client-server application using envisage framework, I'm using threads to handle the connection, here is a token from the code

    ....
    SocketServer.TCPServer.allow_reuse_address = True        
    self.server = TCPFactory( ( HOST, PORT ), TCPRequestHandler, self.application)               

    self.server_thread = threading.Thread( target = self.server.serve_forever )        

    self.server_thread.setDaemon( True )       
    self.server_thread.start() 

class TCPFactory( SocketServer.ThreadingTCPServer ):    
    def __init__( self, server_address, RequestHandlerClass, application ):
        SocketServer.ThreadingTCPServer.__init__( self, server_address, RequestHandlerClass )
        self.application = application

class TCPRequestHandler( SocketServer.BaseRequestHandler ):
    """"""
    def setup( self ):
        .....

In the envisage framework I call the open_file( ) function, which give us a popup window, but when this window appear than I'm receiving the following error

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
    r, w, e = select.select([self], [], [], poll_interval)
error: (4, 'Interrupted system call')

How can I handle this error?


Solution

  • After Armin Rigo comment, I modified the SockeServer.py

    def serve_forever(self, poll_interval=0.5):
            """Handle one request at a time until shutdown.
    
            Polls for shutdown every poll_interval seconds. Ignores
            self.timeout. If you need to do periodic tasks, do them in
            another thread.
            """
            self.__is_shut_down.clear()
            try:
                while not self.__shutdown_request:
                    # XXX: Consider using another file descriptor or
                    # connecting to the socket to wake this up instead of
                    # polling. Polling reduces our responsiveness to a
                    # shutdown request and wastes cpu at all other times.
                    try:
                            r, w, e = select.select([self], [], [], poll_interval)
                    except select.error  as ex:
                            #print ex
                            if ex[0] == 4:
                                    continue
                            else:
                                    raise  
                    if self in r:
                        self._handle_request_noblock()
            finally:
                self.__shutdown_request = False
                self.__is_shut_down.set()