sshtunnel: Hangs with Python 3.7
I’m using SSHTunnelForwarder with Python 3.7. After finishing my work, I run server.stop() and hangs.
I believe it caused by socketserver.ThreadingMixIn
https://docs.python.org/3.7/library/socketserver.html#socketserver.ThreadingMixIn
Changed in version 3.7: socketserver.ForkingMixIn.server_close() and socketserver.ThreadingMixIn.server_close() now waits until all child processes and non-daemonic threads complete. Add a new socketserver.ForkingMixIn.block_on_close class attribute to opt-in for the pre-3.7 behaviour.
Here’s my work around. I changed the attribute server._server_list[0].block_on_close = False, and it behave like usual. It closed normally and not being hang.
I’m not sure if setting block_on_close = False is a good way or not.
Issue #135 may have the same cause.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 31 (5 by maintainers)
Hi, I am still getting this issue with python 3.8 using v0.1.5.
EDIT: specifically it happens to me only if I execute any kind of query through the ssh tunnel (I’m using django 2.2.9)
Well maybe it’s much easier just setting
tunnel.daemon_forward_servers = Truebefore starting the tunnel rather than patching the constant.updated PYPI
So are we getting a release with this fix or not? sshtunnel currently doesn’t work properly in Python 3.7+
Still hangs when using as context manager:
Env: Python 3.7.3 SSHTunnelForwarder 0.1.5 OS: macOS Catalina
Gist example: https://gist.github.com/youngsoul/a02e45ef48e405c148f63bcfaea5ad8f
I have found that SSHTunnelForwarder will hang if I use it as a context manager using ‘with’. In the gist above see the function, ‘run_query_with’. If I create the SSHTunnelForwarder and manage the start/stop myself then it does not hang. In the gist see the function, ‘run_query’.
I did not have to set the flags as suggested above in ‘run_query’ and it is a little bit of mystery as to why that would be.
As far as I can tell, you definitely cannot set those properties if using as a context manager, because the enter setups up the transport and the flags are already set with _DAEMON.
For Python 3.8, sshtunnel 0.1.5, I found setting
daemon_forward_servers = Trueanddaemon_transport = Trueon aSshTunnelForwarderinstance helps in my case.It has been patched with v0.1.5 at https://github.com/pahaz/sshtunnel/commit/fa85471f1b178004567ae7fd7dd3a3a7e9ebffa (#149) which is not on pypi yet. It has been merged into master branch, so you can simply install through GitHub by using
pip install git+https://github.com/pahaz/sshtunnel.git