# Buggy code
def _make_ssh_client(self, host):
    auth_thread_pool = True
    if self.proxy_host is not None and self._tunnel is None:
        self._start_tunnel_thread()
    logger.debug(
        "Make client request for host %s, host in clients: %s",
        host,
        host in self.host_clients,
    )
    with self._clients_lock:
        if host not in self.host_clients or self.host_clients[host] is None:
            _user, _port, _password, _pkey = self._get_host_config_values(host)
            proxy_host = None if self.proxy_host is None else "127.0.0.1"
            if proxy_host is not None:
                auth_thread_pool = False
                _wait = 0.0
                max_wait = self.timeout if self.timeout is not None else 60
                with self._tunnel_lock:
                    self._tunnel_in_q.append((host, _port))
                while True:
                    if _wait >= max_wait:
                        raise Timeout(
                            "Timed out waiting on tunnel to open listening port"
                        )
                    try:
                        _port = self._tunnel_out_q.pop()
                    except IndexError:
                        logger.debug("Waiting on tunnel to open listening port")
                        sleep(0.5)
                        _wait += 0.5
                    else:
                        break
            self.host_clients[host] = SSHClient(
                host,
                user=_user,
                password=_password,
                port=_port,
                pkey=_pkey,
                num_retries=self.num_retries,
                timeout=self.timeout,
                allow_agent=self.allow_agent,
                retry_delay=self.retry_delay,
                proxy_host=proxy_host,
                _auth_thread_pool=auth_thread_pool,
                forward_ssh_agent=self.forward_ssh_agent,
                keepalive_seconds=self.keepalive_seconds,
            )


# 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 _make_ssh_client(self, host_i, host):
    auth_thread_pool = True
    if self.proxy_host is not None and self._tunnel is None:
        self._start_tunnel_thread()
    logger.debug(
        "Make client request for host %s, host in clients: %s",
        host,
        host in self.host_clients,
    )
    with self._clients_lock:
        if (host_i, host) not in self._host_clients or self._host_clients[
            (host_i, host)
        ] is None:
            _user, _port, _password, _pkey = self._get_host_config_values(host)
            proxy_host = None if self.proxy_host is None else "127.0.0.1"
            if proxy_host is not None:
                auth_thread_pool = False
                _wait = 0.0
                max_wait = self.timeout if self.timeout is not None else 60
                with self._tunnel_lock:
                    self._tunnel_in_q.append((host, _port))
                while True:
                    if _wait >= max_wait:
                        raise Timeout(
                            "Timed out waiting on tunnel to open listening port"
                        )
                    try:
                        _port = self._tunnel_out_q.pop()
                    except IndexError:
                        logger.debug("Waiting on tunnel to open listening port")
                        sleep(0.5)
                        _wait += 0.5
                    else:
                        break
            _client = SSHClient(
                host,
                user=_user,
                password=_password,
                port=_port,
                pkey=_pkey,
                num_retries=self.num_retries,
                timeout=self.timeout,
                allow_agent=self.allow_agent,
                retry_delay=self.retry_delay,
                proxy_host=proxy_host,
                _auth_thread_pool=auth_thread_pool,
                forward_ssh_agent=self.forward_ssh_agent,
                keepalive_seconds=self.keepalive_seconds,
            )
            self.host_clients[host] = _client
            self._host_clients[(host_i, host)] = _client