# Buggy code
def run(self):
    self._init_tunnel_client()
    self._init_tunnel_sock()
    logger.debug("Hub in run function: %s", get_hub())
    try:
        while True:
            logger.debug("Tunnel waiting for connection")
            self.tunnel_open.set()
            self.forward_sock, forward_addr = self.socket.accept()
            logger.debug(
                "Client connected, forwarding %s:%s on remote host to local %s",
                self.fw_host,
                self.fw_port,
                forward_addr,
            )
            self.session.set_blocking(1)
            self.channel = self.session.direct_tcpip_ex(
                self.fw_host, self.fw_port, "127.0.0.1", forward_addr[1]
            )
            if self.channel is None:
                self.forward_sock.close()
                self.socket.close()
                raise Exception(
                    "Could not establish channel to %s:%s", self.fw_host, self.fw_port
                )
            self.session.set_blocking(0)
            source = spawn(self._read_forward_sock)
            dest = spawn(self._read_channel)
            joinall((source, dest))
            self.channel.close()
            self.forward_sock.close()
    finally:
        if not self.socket.closed:
            self.socket.close()


# Traceback (SessionError)
Exception in thread Thread-360:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/Users/PATH_TO_APP/venv/lib/python2.7/site-packages/pssh/tunnel.py", line 123, in run
self._init_tunnel_client()
File "/Users/PATH_TO_APP/venv/lib/python2.7/site-packages/pssh/tunnel.py", line 119, in _init_tunnel_client
timeout=self.timeout)
File "/Users/PATH_TO_APP/venv/lib/python2.7/site-packages/pssh/clients/native/single.py", line 120, in __init__
THREAD_POOL.apply(self._init)
File "/Users/PATH_TO_APP/venv/lib/python2.7/site-packages/gevent/pool.py", line 324, in apply
return func(*args, **kwds)
File "/Users/PATH_TO_APP/venv/lib/python2.7/site-packages/pssh/clients/native/single.py", line 158, in _init
return self._connect_init_retry(retries)
File "/Users/PATH_TO_APP/venv/lib/python2.7/site-packages/pssh/clients/native/single.py", line 148, in _connect_init_retry
return self._init(retries=retries)
File "/Users/PATH_TO_APP/venv/lib/python2.7/site-packages/pssh/clients/native/single.py", line 160, in _init
raise SessionError(msg, self.host, self.port, ex)
SessionError: ('Error connecting to host %s:%s - %s', 'proxy.example.com', 22, Timeout())

# Fixed code
def run(self):
    try:
        self._init_tunnel_client()
        self._init_tunnel_sock()
    except Exception as ex:
        logger.error("Tunnel initilisation failed with %s", ex)
        self.exception = ex
        return
    logger.debug("Hub in run function: %s", get_hub())
    try:
        while True:
            logger.debug("Tunnel waiting for connection")
            self.tunnel_open.set()
            self.forward_sock, forward_addr = self.socket.accept()
            logger.debug(
                "Client connected, forwarding %s:%s on remote host to local %s",
                self.fw_host,
                self.fw_port,
                forward_addr,
            )
            self.session.set_blocking(1)
            self.channel = self.session.direct_tcpip_ex(
                self.fw_host, self.fw_port, "127.0.0.1", forward_addr[1]
            )
            if self.channel is None:
                self.forward_sock.close()
                self.socket.close()
                raise Exception(
                    "Could not establish channel to %s:%s", self.fw_host, self.fw_port
                )
            self.session.set_blocking(0)
            source = spawn(self._read_forward_sock)
            dest = spawn(self._read_channel)
            joinall((source, dest))
            self.channel.close()
            self.forward_sock.close()
    finally:
        if not self.socket.closed:
            self.socket.close()