# Buggy code
def run_command(
    self,
    command,
    user=None,
    stop_on_errors=True,
    host_args=None,
    use_pty=False,
    shell=None,
    encoding="utf-8",
    *args,
    **kwargs,
):
    greenlet_timeout = kwargs.pop("greenlet_timeout", None)
    output = {}
    if host_args:
        try:
            cmds = [
                self.pool.spawn(
                    self._run_command,
                    host,
                    command % host_args[host_i],
                    user=user,
                    encoding=encoding,
                    use_pty=use_pty,
                    shell=shell,
                    *args,
                    **kwargs,
                )
                for host_i, host in enumerate(self.hosts)
            ]
        except IndexError:
            raise HostArgumentException(
                "Number of host arguments provided does not match number of hosts "
            )
    else:
        cmds = [
            self.pool.spawn(
                self._run_command,
                host,
                command,
                user=user,
                encoding=encoding,
                use_pty=use_pty,
                shell=shell,
                *args,
                **kwargs,
            )
            for host in self.hosts
        ]
    for cmd in cmds:
        try:
            self.get_output(cmd, output, timeout=greenlet_timeout)
        except Exception:
            if stop_on_errors:
                raise
    self.cmds = cmds
    return output


# Traceback (ValueError)
Traceback (most recent call last):
File "./aws-instances", line 172, in <module>
main()
File "./aws-instances", line 73, in main
facts = remote_facts(ips)
File "./aws-instances", line 146, in remote_facts
out_kern = client.run_command('uname -r')
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/pssh/clients/native/parallel.py", line 223, in run_command
greenlet_timeout=greenlet_timeout)
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/pssh/clients/base_pssh.py", line 96, in run_command
self.get_output(cmd, output, timeout=greenlet_timeout)
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/pssh/clients/base_pssh.py", line 141, in get_output
(channel, host, stdout, stderr, stdin) = cmd.get(timeout=timeout)
File "src/gevent/greenlet.py", line 709, in gevent._greenlet.Greenlet.get
File "src/gevent/greenlet.py", line 317, in gevent._greenlet.Greenlet._raise_exception
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/gevent/_compat.py", line 47, in reraise
raise value.with_traceback(tb)
File "src/gevent/greenlet.py", line 766, in gevent._greenlet.Greenlet.run
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/pssh/clients/native/parallel.py", line 237, in _run_command
raise ex
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/pssh/clients/native/parallel.py", line 233, in _run_command
use_pty=use_pty, encoding=encoding, timeout=timeout)
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/pssh/clients/native/single.py", line 442, in run_command
channel = self.execute(_command, use_pty=use_pty)
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/pssh/clients/native/single.py", line 324, in execute
channel = self.open_session() if channel is None else channel
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/pssh/clients/native/single.py", line 297, in open_session
wait_select(self.session)
File "pssh/native/_ssh2.pyx", line 171, in pssh.native._ssh2.wait_select
File "/home/user/do-aws-ec2-facts/aws/lib64/python3.6/site-packages/gevent/select.py", line 145, in select
sel_results = _original_select(rlist, wlist, xlist, 0)
ValueError: filedescriptor out of range in select()

# Fixed code
def run_command(
    self,
    command,
    user=None,
    stop_on_errors=True,
    host_args=None,
    use_pty=False,
    shell=None,
    encoding="utf-8",
    return_list=False,
    *args,
    **kwargs,
):
    greenlet_timeout = kwargs.pop("greenlet_timeout", None)
    if host_args:
        try:
            cmds = [
                self.pool.spawn(
                    self._run_command,
                    host_i,
                    host,
                    command % host_args[host_i],
                    user=user,
                    encoding=encoding,
                    use_pty=use_pty,
                    shell=shell,
                    *args,
                    **kwargs,
                )
                for host_i, host in enumerate(self.hosts)
            ]
        except IndexError:
            raise HostArgumentException(
                "Number of host arguments provided does not match number of hosts "
            )
    else:
        cmds = [
            self.pool.spawn(
                self._run_command,
                host_i,
                host,
                command,
                user=user,
                encoding=encoding,
                use_pty=use_pty,
                shell=shell,
                *args,
                **kwargs,
            )
            for host_i, host in enumerate(self.hosts)
        ]
    self.cmds = cmds
    joinall(cmds, raise_error=False, timeout=greenlet_timeout)
    if not return_list:
        warn(_output_depr_notice)
        output = {}
        return self._get_output_dict(
            cmds, output, stop_on_errors=stop_on_errors, timeout=greenlet_timeout
        )
    return [
        self._get_output_from_greenlet(cmd, timeout=greenlet_timeout) for cmd in cmds
    ]