# Buggy code
def rsh(
    driver_addresses,
    key,
    settings,
    host_hash,
    command,
    env,
    local_rank,
    background=True,
    events=None,
):
    """
    Method to run a command remotely given a host hash, local rank and driver addresses.

    This method connects to the SparkDriverService running on the Spark driver,
    retrieves all information required to connect to the task with given local rank
    of that host hash and invoke the command there.

    The method returns immediately after launching the command if background is True (default).
    When background is set to False, this method waits for command termination and returns
    command's result. If there is an exception while waiting for the result (i.e. connection reset)
    it returns -1.

    :param driver_addresses: driver's addresses
    :param key: used for encryption of parameters passed across the hosts
    :param settings: settings
    :param host_hash: host hash to connect to
    :param command: command and arguments to invoke
    :param env: environment to use
    :param local_rank: local rank on the host of task to run the command in
    :param background: run command in background if True, returns command result otherwise
    :param events: events to abort the command, only if background is True
    """
    if ":" in host_hash:
        raise Exception("Illegal host hash provided. Are you using Open MPI 4.0.0+?")

    driver_client = driver_service.SparkDriverClient(
        driver_addresses, key, verbose=settings.verbose
    )
    task_indices = driver_client.task_host_hash_indices(host_hash)
    task_index = task_indices[local_rank]
    task_addresses = driver_client.all_task_addresses(task_index)
    task_client = task_service.SparkTaskClient(
        task_index, task_addresses, key, verbose=settings.verbose
    )
    task_client.run_command(command, env)

    if not background:
        stop = None
        events = events or []
        for event in events:
            stop = threading.Event()
            on_event(event, task_client.abort_command, stop=stop)

        try:
            return task_client.wait_for_command_exit_code()
        except:
            traceback.print_exc()
            return -1
        finally:
            if stop is not None:
                stop.set()


# 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 rsh(
    driver_addresses,
    key,
    host_hash,
    command,
    env,
    local_rank,
    verbose,
    background=True,
    events=None,
):
    """
    Method to run a command remotely given a host hash, local rank and driver addresses.

    This method connects to the SparkDriverService running on the Spark driver,
    retrieves all information required to connect to the task with given local rank
    of that host hash and invoke the command there.

    The method returns immediately after launching the command if background is True (default).
    When background is set to False, this method waits for command termination and returns
    command's result. If there is an exception while waiting for the result (i.e. connection reset)
    it returns -1.

    :param driver_addresses: driver's addresses
    :param key: used for encryption of parameters passed across the hosts
    :param host_hash: host hash to connect to
    :param command: command and arguments to invoke
    :param env: environment to use
    :param local_rank: local rank on the host of task to run the command in
    :param verbose: verbosity level
    :param background: run command in background if True, returns command result otherwise
    :param events: events to abort the command, only if background is True
    """
    if ":" in host_hash:
        raise Exception("Illegal host hash provided. Are you using Open MPI 4.0.0+?")

    driver_client = driver_service.SparkDriverClient(
        driver_addresses, key, verbose=verbose
    )
    task_indices = driver_client.task_host_hash_indices(host_hash)
    task_index = task_indices[local_rank]
    task_addresses = driver_client.all_task_addresses(task_index)
    task_client = task_service.SparkTaskClient(
        task_index, task_addresses, key, verbose=verbose
    )
    task_client.run_command(command, env)

    if not background:
        stop = None
        events = events or []
        for event in events:
            stop = threading.Event()
            on_event(event, task_client.abort_command, stop=stop)

        try:
            return task_client.wait_for_command_exit_code()
        except:
            traceback.print_exc()
            return -1
        finally:
            if stop is not None:
                stop.set()