# Buggy code
def init_ida_rpc_client():
    global _ida, _ida_last_exception
    addr = "http://{host}:{port}".format(host=ida_rpc_host, port=ida_rpc_port)

    _ida = xmlrpclib.ServerProxy(addr)
    socket.setdefaulttimeout(3)

    exception = None  # (type, value, traceback)
    try:
        _ida.here()
        print(
            message.success(
                "Pwndbg successfully connected to Ida Pro xmlrpc: %s" % addr
            )
        )
    except socket.error as e:
        if e.errno != errno.ECONNREFUSED:
            exception = sys.exc_info()
        _ida = None
    except socket.timeout:
        exception = sys.exc_info()
        _ida = None
    except xmlrpclib.ProtocolError:
        exception = sys.exc_info()
        _ida = None

    if exception:
        if (
            not isinstance(_ida_last_exception, exception[0])
            or _ida_last_exception.args != exception[1].args
        ):
            print(message.error("[!] Ida Pro xmlrpc error"))
            traceback.print_exception(*exception)

    _ida_last_exception = exception and exception[1]


# Traceback (InterruptedError)
[!] Ida Pro xmlrpc error
Traceback (most recent call last):
File "/mnt/d/Uni/SS18/exploits/pwndbg/pwndbg/pwndbg/ida.py", line 64, in init_ida_rpc_client
_ida.here()
File "/usr/lib/python3.4/xmlrpc/client.py", line 1098, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python3.4/xmlrpc/client.py", line 1437, in __request
verbose=self.__verbose
File "/usr/lib/python3.4/xmlrpc/client.py", line 1140, in request
return self.single_request(host, handler, request_body, verbose)
File "/usr/lib/python3.4/xmlrpc/client.py", line 1152, in single_request
http_conn = self.send_request(host, handler, request_body, verbose)
File "/usr/lib/python3.4/xmlrpc/client.py", line 1264, in send_request
self.send_content(connection, request_body)
File "/usr/lib/python3.4/xmlrpc/client.py", line 1294, in send_content
connection.endheaders(request_body)
File "/usr/lib/python3.4/http/client.py", line 1121, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.4/http/client.py", line 951, in _send_output
self.send(msg)
File "/usr/lib/python3.4/http/client.py", line 886, in send
self.connect()
File "/usr/lib/python3.4/http/client.py", line 863, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.4/socket.py", line 512, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 503, in create_connection
sock.connect(sa)
InterruptedError: [Errno 4] Unterbrechung während des Betriebssystemaufrufs

# Fixed code
def init_ida_rpc_client():
    global _ida, _ida_last_exception, _ida_last_connection_check

    if not ida_enabled:
        return

    now = time.time()
    if _ida is None and (now - _ida_last_connection_check) < int(ida_timeout) + 5:
        return

    addr = "http://{host}:{port}".format(host=ida_rpc_host, port=ida_rpc_port)

    _ida = xmlrpclib.ServerProxy(addr)
    socket.setdefaulttimeout(int(ida_timeout))

    exception = None  # (type, value, traceback)
    try:
        _ida.here()
        print(
            message.success(
                "Pwndbg successfully connected to Ida Pro xmlrpc: %s" % addr
            )
        )
    except socket.error as e:
        if e.errno != errno.ECONNREFUSED:
            exception = sys.exc_info()
        _ida = None
    except socket.timeout:
        exception = sys.exc_info()
        _ida = None
    except xmlrpclib.ProtocolError:
        exception = sys.exc_info()
        _ida = None

    if exception:
        if (
            not isinstance(_ida_last_exception, exception[0])
            or _ida_last_exception.args != exception[1].args
        ):
            if (
                hasattr(pwndbg.config, "exception_verbose")
                and pwndbg.config.exception_verbose
            ):
                print(message.error("[!] Ida Pro xmlrpc error"))
                traceback.print_exception(*exception)
            else:
                exc_type, exc_value, _ = exception
                print(
                    message.error(
                        "Failed to connect to IDA Pro ({}: {})".format(
                            exc_type.__qualname__, exc_value
                        )
                    )
                )
                if exc_type is socket.timeout:
                    print(
                        message.notice("To increase the time to wait for IDA Pro use `")
                        + message.hint("set ida-timeout <new-timeout-in-seconds>")
                        + message.notice("`")
                    )
                else:
                    print(
                        message.notice("For more info invoke `")
                        + message.hint("set exception-verbose on")
                        + message.notice("`")
                    )
                print(
                    message.notice("To disable IDA Pro integration invoke `")
                    + message.hint("set ida-enabled off")
                    + message.notice("`")
                )

    _ida_last_exception = exception and exception[1]
    _ida_last_connection_check = now