# Buggy code
def _check_all_hosts_ssh_successful(host_addresses, ssh_port=None):
    """
    checks if ssh can successfully be performed to all the hosts.
    :param host_addresses: list of addresses to ssh into. for example,
        ['worker-0','worker-1']
        ['10.11.11.11', '10.11.11.12']
    :type host_addresses: list(strings)
    :return: Returns True if all ssh was successful into all the addresses.
    """

    def exec_command(command):
        exit_code = 1
        output_msg = ""

        # Try ssh 5 times
        for i in range(SSH_ATTEMPTS):
            output = io.StringIO()
            try:
                exit_code = safe_shell_exec.execute(
                    command, stdout=output, stderr=output
                )
                if exit_code == 0:
                    break
                output_msg = output.getvalue()
            finally:
                output.close()
        return exit_code, output_msg

    ssh_port_arg = "-p {ssh_port}".format(ssh_port=ssh_port) if ssh_port else ""

    ssh_command_format = "ssh -o StrictHostKeyChecking=no {host} {ssh_port_arg} date"

    args_list = [
        [ssh_command_format.format(host=host_address, ssh_port_arg=ssh_port_arg)]
        for host_address in host_addresses
    ]
    ssh_exit_codes = threads.execute_function_multithreaded(exec_command, args_list)

    ssh_successful_to_all_hosts = True
    for index, ssh_status in ssh_exit_codes.items():
        exit_code, output_msg = ssh_status[0], ssh_status[1]
        if exit_code != 0:
            print(
                "ssh not successful for host {host}:\n{msg_output}".format(
                    host=host_addresses[index], msg_output=output_msg
                )
            )

            ssh_successful_to_all_hosts = False
    if not ssh_successful_to_all_hosts:
        exit(1)
    return True


# Traceback (struct.error)
root@ip-172-31-37-52:/# horovodrun -np 2 -H localhost:1,172.31.35.37:1 -p 12345 --verbose ls
Filtering local host names.
Remote host found: 172.31.35.37
Checking ssh on all remote hosts.
SSH was successful into all the remote hosts.
Testing interfaces on all the hosts.
Launched horovod server.
Attempted to launch horovod task servers.
Waiting for the hosts to acknowledge.
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 37436)
Exception happened during processing of request from ('172.31.37.52', 52632)
----------------------------------------
Exception happened during processing of request from ('172.31.37.52', 52636)
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
Traceback (most recent call last):
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('172.31.37.52', 55358)
----------------------------------------
Exception happened during processing of request from ('172.31.37.52', 52642)
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
Exception happened during processing of request from ('127.0.0.1', 37442)
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
struct.error: unpack requires a buffer of 4 bytes
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
----------------------------------------
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
Exception happened during processing of request from ('172.31.37.52', 55364)
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 37448)
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
struct.error: unpack requires a buffer of 4 bytes
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
----------------------------------------
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('172.31.37.52', 55368)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('172.31.35.37', 39790)
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('172.31.35.37', 39796)
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
----------------------------------------
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
Exception happened during processing of request from ('172.31.35.37', 39798)
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/opt/conda/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/conda/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 105, in handle
req = server._wire.read(self.rfile)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 79, in read
message_len = struct.unpack('i', rfile.read(4))[0]
struct.error: unpack requires a buffer of 4 bytes
----------------------------------------
Launching horovod task function was not successful:
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/opt/conda/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/task_fn.py", line 67, in <module>
_task_fn(index, driver_addresses, settings)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/task_fn.py", line 27, in _task_fn
driver_addresses, settings.key, settings.verbose)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/driver/driver_service.py", line 44, in __init__
match_intf=match_intf)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/service/driver_service.py", line 159, in __init__
match_intf=match_intf)
File "/opt/conda/lib/python3.6/site-packages/horovod/run/common/util/network.py", line 172, in __init__
'Linux.'.format(service_name=service_name, addresses=addresses))
horovod.run.common.util.network.NoValidAddressesFound: Horovod was unable to connect to horovod driver service on any of the following addresses: {'lo': [('127.0.0.1', 4548)], 'ens3': [('172.31.37.52', 4548)], 'docker0': [('172.17.0.1', 4548)]}.

One possible cause of this problem is that horovod currently requires every host to have at least one routable network interface with the same name across all of the hosts. You can run "ifconfig -a" on every host and check for the common routable interface. To fix the problem, you can rename interfaces on Linux.

# Fixed code
def _check_all_hosts_ssh_successful(host_addresses, ssh_port=None):
    """
    checks if ssh can successfully be performed to all the hosts.
    :param host_addresses: list of addresses to ssh into. for example,
        ['worker-0','worker-1']
        ['10.11.11.11', '10.11.11.12']
    :type host_addresses: list(strings)
    :return: Returns True if all ssh was successful into all the addresses.
    """

    def exec_command(command):
        exit_code = 1
        output_msg = ""

        # Try ssh 5 times
        for i in range(SSH_ATTEMPTS):
            output = io.StringIO()
            try:
                exit_code = safe_shell_exec.execute(
                    command, stdout=output, stderr=output
                )
                if exit_code == 0:
                    break
                output_msg = output.getvalue()
            finally:
                output.close()
        return exit_code, output_msg

    ssh_port_arg = "-p {ssh_port}".format(ssh_port=ssh_port) if ssh_port else ""

    ssh_command_format = (
        "ssh -o PasswordAuthentication=no -o StrictHostKeyChecking=no"
        " {host} {ssh_port_arg} true"
    )

    args_list = [
        [ssh_command_format.format(host=host_address, ssh_port_arg=ssh_port_arg)]
        for host_address in host_addresses
    ]
    ssh_exit_codes = threads.execute_function_multithreaded(exec_command, args_list)

    ssh_successful_to_all_hosts = True
    for index, ssh_status in ssh_exit_codes.items():
        exit_code, output_msg = ssh_status[0], ssh_status[1]
        if exit_code != 0:
            print(
                "ssh not successful for host {host}:\n{msg_output}".format(
                    host=host_addresses[index], msg_output=output_msg
                )
            )

            ssh_successful_to_all_hosts = False
    if not ssh_successful_to_all_hosts:
        return None  # we could return False here but do not want it to be cached
    return True