Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tunnel thread leakage with Python 3.6 #170

Closed
kkrasovskii opened this issue Dec 5, 2019 · 1 comment
Closed

Tunnel thread leakage with Python 3.6 #170

kkrasovskii opened this issue Dec 5, 2019 · 1 comment

Comments

@kkrasovskii
Copy link

kkrasovskii commented Dec 5, 2019

If we try to establish SSH tunnel with connectivity checking to some service that is unavailable we will get thread leaking.

Here is the thing:

When sshtunnel.start() function is called it creates new thread that uses socketserver package serve_forever function as infinite loop that can be broken by setting special flag - __shutdown_request. This flag is set by sshtunnel module when we call it stop() function. The stop function sets __shutdown_request flag in the case when the tunnel_is_up flag are set. If we check remote service availability and it's not available the tunnel_is_up flag is in False state and early created thread is not closed by stop method. The problem can be easily fixed this way:

    def _stop_transport(self):
        """ Close the underlying transport when nothing more is needed """
        try:
            self._check_is_started()
        except (BaseSSHTunnelForwarderError,
                HandlerSSHTunnelForwarderError) as e:
            self.logger.warning(e)
        for _srv in self._server_list:
            tunnel = _srv.local_address
            if self.tunnel_is_up[tunnel]:
                self.logger.info('Shutting down tunnel {0}'.format(tunnel))
            _srv.shutdown()
            _srv.server_close()
            # clean up the UNIX domain socket if we're using one
            if isinstance(_srv, _UnixStreamForwardServer):
                try:
                    os.unlink(_srv.local_address)
                except Exception as e:
                    self.logger.error('Unable to unlink socket {0}: {1}'
                                      .format(self.local_address, repr(e)))

The problem can be easily reproduced by a code in the file:

thread_leaks_example.txt

@kkrasovskii kkrasovskii changed the title Tunnel thread leakage with Python 3 Tunnel thread leakage with Python 3.6 Dec 5, 2019
@eddie-chiang
Copy link

eddie-chiang commented Jun 27, 2020

The issue is still happening on v0.1.5, with Python 3.7.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants